How To Setup And Use SSH For Remote Connections

Sergio Pietri
5 min readMay 5, 2020

--

Little Billy managing a cluster of servers using SSH. Look how focused he is.

If you want to handle remote servers or maybe you just want to set up a connection to your local computer this is your spot. If you keep reading you will know enough about SSH to establish a secure connection to a remote machine.

This can be useful for your home computer in case you forgot something while you’re away and need to connect to it to retrieve it.

I will show you in a few steps how to set up an SSH connection through your terminal. The process is quite simple but first let me set YOU up with the basics.

What you need:

  • Your favorite Shell.
  • Make sure you have SSH installed, it comes by default on Linux and MacOS systems. If not just run the following commands:

sudo apt-get install openssh-server

sudo systemctl enable ssh

sudo systemctl start ssh

What is SSH?

Simply put, Secure Shell is a protocol designed to transfer data between a client and a server (two computers basically) over an untrusted network.

The way SSH works is it encrypts the connection using a pair of keys and the server, which is the computer you would connect to, is usually waiting for an SSH connection on Port 22.

Now let’s see how to create the key pair and then connect:

How to create an SSH-key pair:

1) Type ssh-keygen -t rsa in your shell. This command will create the keys with the rsa version 2 protocol (recommended).

2) You will be asked for a location. If you decide to enter one manually then that will be the pair’s location, if you leave the default one it will be inside the .ssh hidden folder in your home directory.

3) Now you will be prompted for a password. If you enter one you will be asked for it every time you use the key, this works for added security. If you don’t want a password just press enter and continue without one.

4) There is no 4! That’s it! Your keys have been saved in the specified folder and you will see something weird like this:

As you may have noticed from the image in point #3 two files were created. One file ends with the ‘.pub’ extension and the other one doesn’t.

The file that ends with ‘.pub’ is your public key. This key needs to be in the computer you want to connect to (the server) inside a file called authorized_keys . You can accomplish this with the following command:

ssh-copy-id username@ip

For example in my case to send the key to my computer it would be:

ssh-copy-id sergiop@132.132.132.132

If you have MacOS there’s a chance you don’t have ssh-copy-id installed, in that case you can install it using brew install ssh-copy-id.

The file that has no extension is your private key. You must cherish it, save it, take care of it. There is only one and you can’t generate another one if you lose it so I recommend you send it to your email or something like that.

Remember both keys have the same name the only difference is one ends with ‘.pub’ and the other one doesn’t.

How to connect to a remote machine using SSH:

Transferring to the remote server using ssh.

For this final trick I would like you to use the following command:

ssh -i ~/private/key/location remote_username@remote_ip

This command uses the -i flag for you to specify where the private key is located. Remember where it is? Now type its location there. Where it says username type the one you are going to connect as and then type the remote machine’s ip address after the ‘@’ symbol. Just like we did earlier to transfer the key.

You will be asked if you’re sure you want to continue connecting since the ‘authenticity of host can’t be established’. Type ‘yes’ and hit Enter. This is just because this is your first time connecting to that machine.

Now you will be asked to enter a password if you set one with the key or if the user you’re connecting to has one set up.

Once you successfully do it you will see something like this:

  • Note: it doesn’t have to look exactly like this; your configuration and specs will vary. The important thing is you can tell you are now connected to a remote machine.

--

--

Sergio Pietri

Backend Developer. I like to write about tech and other stuff. Follow me on twitter for more content!