Using Secure Shell to Connect to Remote Machines

Learn how to use SSH to connect to remote machines.

SSH

Secure Shell, or SSH, is an encrypted and secure method for logging in to remote machines. We can also use it to execute commands remotely and use related tools to copy files securely.

This is the standard way to log in to remote Linux servers, either in our own data center or in the cloud. In this lesson, we’ll use our local machine to connect to our Ubuntu virtual machine.

Setting up SSH

First, we install the openssh-server package and enable SSH connections:

sudo apt install openssh-server

This installs and activates the SSH server on port 22.

We identify the IP address of our Ubuntu machine using the ip addr command. We then use grep to look for inet in the output, filtering out inet6 so we don’t see the IPv6 addresses:

ip addr | grep inet | grep -v inet6

Establishing a connection

For simplicity, we’re going to cover how to establish a connection between two users on our platform.

To use SSH, we specify the username and server address to which we wish to connect:

ssh {username}@{ip}

The first time we connect to a remote host, SSH asks us to verify the authenticity of the host. Answering yes saves the fingerprint of the remote server to a file named ~/.ssh/known-hosts. If the server’s fingerprint changes, we won’t connect to it because SSH will assume the machine has either been compromised or is a different machine. It’s just an extra security precaution; the fingerprint shouldn’t change without us expecting it.

Once we add the server to the list of known hosts, we’re prompted for our remote user’s password. Once we enter it, we’re logged in.

Here are the steps we must follow:

  1. Upon the first prompt, enter 123456 as the password.

  2. Type su sshuser.

  3. Type ssh temp@127.0.0.1.

  4. Upon the next prompt, type yes and press “Enter”.

  5. Finally, enter the password you set earlier and press “Enter”.

Run the complete code on the terminal below for practice.

service ssh start
clear
passwd temp

Get hands-on with 1400+ tech skills courses.