How to Use SSH to Connect to Remote Server
This tutorial demonstrates using ssh
to connect to a remote Linux server securely.
Use SSH (Secure Socket Shell) to Access a Remote Linux Server Securely
SSH is short for Secure Socket Shell. It is a protocol that is used to access remote Linux machines securely.
Once the SSH connection has been established between your computer and the remote Linux machine, a shell is started on the remote Linux machine, and you access this shell on your computer. You can run all commands on a remote Linux machine like your local computer using this shell.
The SSH protocol is very secure for accessing remote Linux machines, and the system and network administrators commonly use it to maintain and configure remote Linux machines.
SSH uses the client-server architecture. It has the client component and the server-side component. The person who wants to access a remote Linux machine needs to have the SSH client installed on their machine.
The SSH client is the program to access the remote Linux machine. The person will input the information of the remote Linux machine and credentials into the SSH client, and if the details are valid, a secure SSH connection is started.
The server-side component runs on the remote Linux machine. The remote Linux machine needs to have the SSH service installed and up and running.
The SSH service constantly listens for incoming connections at a specific port like a server. If an SSH client initiates a connection, the SSH service will respond with the protocol versions it supports, and the two will exchange the data used for identification. Once the credentials are valid, an SSH connection is established.
There are many SSH tools that one can use; OpenSSH is one of them and is mostly used on Linux distributions. This tutorial will use OpenSSH for demonstration purposes.
Install OpenSSH Client in Linux
Many Linux distributions come with the SSH client already installed. In Linux, it is good practice to check if a program is already installed before installing it.
We check if ssh
is already installed by typing ssh
and pressing Enter.
The ssh
command displays its usage to the standard output. It means the SSH client is already installed. If the SSH client is not installed, install it first before continuing this tutorial.
Type the command below to install the OpenSSH client on your machine. It will ask you for the superuser password, provide the password to continue the installation.
sudo apt-get install openssh-client
Windows users can use the famous PuTTY
program as an SSH client.
Install OpenSSH Server on the Remote Linux Machine
Ensure that the OpenSSH server is installed on the remote Linux machine before accessing it. If it’s not installed, make sure to install it.
To install the OpenSSH server on Linux, run the following command.
sudo apt-get install openssh-server
We try to install the OpenSSH server in the image below, but the machine says that we already have the OpenSSH server installed on our machine.
Start the OpenSSH Server on the Remote Linux Machine
After successfully installing the OpenSSH server on the remote Linux machine, we need to start the OpenSSH service to listen to incoming connections from SSH clients.
First, we check the status of the OpenSSH service by typing sudo service ssh status
. The output below says the service is inactive.
We use the sudo service ssh start
command to start the OpenSSH service. The command starts the ssh
daemon running.
We use the sudo service ssh status
command to check the status of the service. The output says that the service is now active (running)
.
The remote Linux machine has an OpenSSH server installed, and it is up and running. It is now ready to accept incoming connections from SSH clients.
Use ssh
Command to Access the Remote Linux Machine
We create a new user on the remote Linux machine that we will use for demonstration purposes in this tutorial. Using the useradd
command, we create a new user, delftstack
.
We use the passwd
command to set the password for the user, delftstack
.
Now, we go to our machine where we have the OpenSSH client installed and try to access the remote Linux machine using the newly created user, delftstack
.
SSH uses the following notations to login into a remote Linux machine.
ssh username@remotehostname
ssh username@remoteIPaddress
SSH will ask for the user’s password, delftstack
, enter the password, and press Enter. Once you see the output like the one below, you have successfully logged in to the remote Linux machine.