DEV Community

Cover image for SSH in Linux
Nabin Ale
Nabin Ale

Posted on

SSH in Linux

What is SSH in Linux?

SSH means Secure Shell. It is a safe way to connect to another
computer over a network.

Why do we use SSH?

We use SSH to:

  • Logging into a remote Linux server
  • Run commands from far away
  • Send and receive files safely (using scp or sftp)
  • Managing cloud servers (AWS, Azure, DigitalOcean, etc.)
  • Setting up secure tunnels (port forwarding)

Does SSH Only Work on Linux?

No. SSH works on many operating systems.
✅ Linux
✅ Windows
✅ macOS

For example, you can use a Windows laptop to connect to a Linux server by opening PowerShell or Command Prompt and typing:

ssh alice@192.168.1.10
Enter fullscreen mode Exit fullscreen mode

You can also connect from macOS to Linux. If Windows has an SSH server installed, you can even connect to a Windows computer using SSH.

This means SSH is a cross-platform tool that works on different operating systems.

Real-Life Example

One of the use case:

Imagine you are at home, but your office computer is turned on.
Without driving to the office, you can use SSH to connect to that
computer and do your work. It is like using a secure remote control for
your computer.

Installing SSH

To use SSH, you need to install the OpenSSH package. It provides both the SSH client and the SSH server.

On Ubuntu or Debian

sudo apt update
sudo apt install openssh-client openssh-server
Enter fullscreen mode Exit fullscreen mode

To start the SSH service, use:

sudo systemctl start sshd
Enter fullscreen mode Exit fullscreen mode

Note: If you see this error while starting the SSH service:

Failed to start sshd.service: Unit sshd.service not found.

To fix this issue, see this guide: click here

To make the SSH service start automatically every time the computer boots:

sudo systemctl enable sshd
Enter fullscreen mode Exit fullscreen mode

Check the SSH Service Status

To check if the SSH service is running, use:

sudo systemctl status sshd
Enter fullscreen mode Exit fullscreen mode

If the service is running, you should see active (running) in the output.

Basic SSH Command

ssh username@server_ip
Enter fullscreen mode Exit fullscreen mode

Example:

ssh alice@192.168.1.10
Enter fullscreen mode Exit fullscreen mode

After connecting, you'll usually be prompted for a password (unless you're using SSH keys).

Example session

$ ssh alice@192.168.1.10
alice@192.168.1.10's password:
Welcome to Ubuntu!
alice@server:~$
Enter fullscreen mode Exit fullscreen mode

Now you can run commands on the remote machine as if you were sitting in front of it.

SSH key authentication (optional)

Instead of using a password every time, you can use an SSH key pair.

  1. Generate a key:
ssh-keygen
Enter fullscreen mode Exit fullscreen mode
  1. Copy the public key to the remote server:
ssh-copy-id username@hostname
Enter fullscreen mode Exit fullscreen mode
  1. Then log in without entering a password:
ssh username@hostname
Enter fullscreen mode Exit fullscreen mode

Some useful SSH options:

  • Connect using a specific port:
ssh -p 2222 username@hostname
Enter fullscreen mode Exit fullscreen mode
  • Use a specific private key:
ssh -i ~/.ssh/my_key username@hostname
Enter fullscreen mode Exit fullscreen mode
  • Run a single command remotely:
ssh username@hostname "ls -l /home"
Enter fullscreen mode Exit fullscreen mode

Secure file transfer

  • Copy a file to a remote server:
scp file.txt username@hostname:/home/username/
Enter fullscreen mode Exit fullscreen mode
  • Copy a file from a remote server:
scp username@hostname:/home/username/file.txt 
Enter fullscreen mode Exit fullscreen mode

How SSH works

  1. The SSH client (your computer) connects to the SSH server (remote computer).
  2. They authenticate each other using a password or SSH keys.
  3. An encrypted connection is established.
  4. All commands and data are securely transmitted.

Why is SSH Safe?

✅ Can transfer files securely
✅ Supports password and key-based authentication
✅ Widely used on Linux and cloud servers
✅ SSH encrypts your data and communication. This means other people cannot easily read your passwords or commands while they travel over the internet.

Summary

SSH is a simple and secure tool for connecting to another
computer. It helps you work from anywhere while keeping your data safe.

Top comments (0)