How to SSH into a remote server


What is SSH?

SSH stands for Secure Shell or also known as Secure Socket Shell. SSH allows us to access a remote computer/server in a secure way over an insecure network such as the internet.

It provides authentication and an encrypted communication between two computers.

Windows 10 Users

You can use Command Prompt (CMD) or Git Bash. Download Git Bash from here.

Windows (older version) Users

If you are using an older version than Windows 10 then you should use an open source SSH client called PuTTY.

You can download PuTTY from here for Windows.

Linux and MacOS Users

You can use their terminal which comes pre-installed.


Authentication

For authentication there are two common ways:

1. Password

Authenticating using a Password is less secure and is generally not recommended way to access your server.

You need to type the following command in your terminal such as git bash if using Windows:

 ssh username@ipaddress
  • username is the username of any user on the server. Generally the username is root on most systems
  • ipaddress is the IP address of the server

    Once you execute this command, it will prompt you for the user's password.

    That's it! You can then access your server, create files and folders, install programs etc.


2. Public/Private Key Pair

Using Public/Private Key Pair for authentication is a more robust and secure way of authentication.

There are two ways for generating public/private keys and connecting to the server via SSH

  1. Windows (older ver than 10) - using PuTTY
  2. Linux / MacOs / Windows 10 - terminal / CMD / Git Bash

Using PuTTY

For generating public/private key and connecting to the server via SSH we will use an open source SSH client called Putty on Windows.

You can download PuTTY from here for Windows.

Once it is downloaded install the program.

Generation of public/private keys:

  1. Search for PuTTYgen - PuTTY Key Generator and open it.

  2. Click on Generate

  3. Move your mouse randomly within the box to generate a random key

  4. Once keys are generated, provide a key passphrase (recommended) for further security

  5. Save both the private and public key to any secure location you wish

  6. Also copy the public key that is shown in the box as we will need it later

Uploading of public key onto the server:

Now we need to store the public key on the server so that we can connect to the server as we already have the private key with us.

  1. Paste the public key into the authorized_keys file on the root of the server.

    Generally your server provider will provide you some easy user interface to paste the public key eg. In Digital Ocean you can go to Account section in the navigation menu then select Security. Under the SSH keys section simply click Add SSH Key and paste your public key.

    Note: Public key should be in one line format, key starts with ssh-rsa

Connecting to the server:

Now that everything is ready, we will connect to the server:

  1. Open PuTTY

  2. Click SSH in the side menu, then click Auth

  3. Now browse your private key by clicking Browse button and after selecting your private key click open

  4. Click Session in the side menu. Provide the hostname/IP address of your server

  5. Click open

    As you are connecting to the server for the first time, PuTTY will prompt you to ask "if you trust this server". Click yes to save it in PuTTY's cache or No to continue without saving the identity of server.

  6. Enter your server's username eg. root

  7. Enter the passphrase if you had provided it during generation of keys

That's it! If you have followed all the steps correctly then you should be able access your server


Linux / MacOs / Windows 10 - Terminal / CMD / Git Bash

We will see how we can generate keys and upload public key to the server and finally connect to our server using SSH.

Generating public/private keys:

  1. To generate the ssh public/private keys, Open your terminal and execute the following command:

    ssh-keygen
  2. Enter the location where you want to save the keys and also provide name for the files. You can leave it blank and just click enter, it will save bothe keys in /.ssh/ with names Private key - id_rsa and Public key - id_rsa.pub

  3. You can also enter a key passphrase (recommended)

This will generate the public/private keys in the specified location

Uploading public key to Server:

We now need to upload the public key to the server. Public key must be in authorized_keys file which should be in .ssh folder.

The following command will copy the public key from /.ssh/id_rsa.pub and paste it on the server at the location - ~/.ssh/authorized_keys

cat ~/.ssh/id_rsa.pub | ssh username@192.168.*.* "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >>  ~/.ssh/authorized_keys"

In the above command, specify the correct path and name of your public key eg. ~/.ssh/id_rsa.pub, specify the username for the server's user and also specify the IP address of your server

Once you execute the following command, you would be asked - "Are you sure you want to continue connecting (yes/no)?", type yes and then you would be asked for the passphrase (if provided).

That's it. You will be able to access your server directly from your terminal with SSH as follows:

ssh username@192.168.*.*

If you had provided the passphrase, it would ask for it.

That's it! You will be connected to your server and can create files and folders, install programs etc


From this post we learned:

  • How to Generate keys with SSH from PuTTY and terminal
  • How to Upload public key to the server
  • How to Connect to the server using SSH from PuTTY and terminal

I hope this post was useful for you and you learned something out of this post.


Tags: