Accessing a remote server using SSH

Do not miss this exclusive book on Binary Tree Problems. Get it now for free.

Secure Shell(ssh) is a cryptographic protocol for networking that encrypts communication between two remote hosts. In this article, we learn how to safely access a remote server using ssh.

Table of contents.

  1. Introduction.
  2. Prerequisites.
  3. Initial setup.
  4. Server configuration.
  5. Client configuration.
  6. Terminating the connection.
  7. Summary.
  8. References.

Introduction.

SSH(Secure Shell) is a networking protocol that is used to encrypt communication between two hosts. In the following sections, we will see how to perform a client-side and server-side configuration of hosts and access the remote server using ssh.

Prerequisites.

The following are requirements for this to work.

  • Both hosts must be on and connected to the internet and be reachable
  • Permissions and authentication details for the user account on the remote server.
  • A SSH client should be installed on the client's computer.
  • A SSH server should be installed on the server.
  • Firewall rules allowing the ssh connection on the server.

Initial setup.

We have two hosts, the client, and the server.
The IP address of the server is 192.168.100.62 - this is where we will connect to using the client and it is also where we will install the openssh-server server software package.

$ ifconfig | grep inet

Now to test its reachability from our localhost.

$ ping 192.168.100.62

Its file system is as follows:

$ ls

Server configuration.

First, we update the remote repositories and the system. For this we write:

$ sudo apt-get update && sudo apt-get upgrade

Once we have updated and upgraded the system, we install openssh-server package as follows:

$ sudo apt-get install openssh-server

Now, let's confirm that it was successfully installed by checking its status:

$ sudo systemctl status ssh

We can also check its status by writing:

$ sudo service ssh status

We have confirmed that it was successfully installed, the next step is to start it to check if there are any issues. If successful we can confirm its status and expect to see the value of Active to be running as shown below, highlighted in green.

$ sudo systemctl start ssh

$ sudo systemctl status ssh

We also have to update the firewall rules and allow ssh through, for this we write:

$ sudo ufw allow ssh

After this we should restart ssh to avoid any issues later:

$ sudo systemctl restart ssh

And we are done, now we install ssh on the client and connect to this server.

Client configuration.

We update the package repositories and upgrade packages by writing:

$ sudo apt-get update && sudo apt-get upgrade

The OpenSSH client is a utility for remote logins that uses ssh networking protocol to encrypt communication between to host. We install it as follows:

$ sudo apt-get install openssh-client

To confirm a successful installation we write the ssh command and analyze the output:

$ ssh

We should expect output as shown below, this means that we have ssh client installed.

To connect to the remote host we write:

$ user@192.168.100.62

Where user is the host username of the remote host whose IP address is 192.168.100.62. We can also use a hostname since it is easier to remember.

If we encounter such an error, it means that we have an incorrect host key in the ~/.ssh/known_hosts file in the home directory.

As the error states, to solve it we execute the highlighted command above:

$ ssh-keygen -f "[known hosts file path]" -R "[hostname or ip address]"

Now when we try to connect again, we will be prompted for a password. Note that this password is the password of the remote host.

After the password has been authenticated, we will be connected to the remote host via ssh.

Notice how the prompt changed? Also, we have the remote file system on our local machine.

We can also check the remote host's IP address:

$ ifconfig | grep inet

Closing the connection.

After we are done we can close the ssh connection on the client-side by using exit as follows:

$ exit

On the server we stop the ssh server using systemctl command and confirm it is closed by checking its status:

$ sudo systemctl stop ssh

$ sudo systemctl status ssh

Summary.

Secure Shell(SSH) is a cryptographic networking protocol used for encrypting communication between two hosts. In this article, we have seen how to access a remote server securely using ssh. We have also seen how the server is configured with ssh and how the client connects to the server.

References.

  1. systemctl
  2. We can execute the command man ssh for the command's manual.

Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.