# How to login securely to a Linux server using SSH keys

The Secure Shell(SSH) is a networking protocol that enables computers to communicate and exchange data. SSH keys enable secure remote login without a password through public and private keys. The private key is kept on the client machine, while the public key is stored on the server machine. Once the ssh client verifies the identity of the ssh server, a secure connection is established. The SSH protocol ensures the privacy and integrity of data exchanged between the two machines using symmetric encryption and hashing algorithms. 

 This guide uses Ubuntu 22.04.1 LTS(my laptop as the client machine)  and a Vultr Ubuntu 22.04 LTS (server machine) to demonstrate the use of the SSH protocol. A summary of what to expect is as shown on the diagram below.

![ssh.drawio (4).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669837332619/VThLmjfEh.png align="left")


## Generating SSH Keys with OpenSSH

OpenSSH ships on  Ubuntu 22.04.1 LTS by default. Launch a terminal using Ctrl+Alt+t  and follow the following steps:

Create a folder that will store the public and private keys. Create the folder *vultrKeyFolder* on the root folder using the command
```sh
mkdir ~/.ssh/vultrkeysfolder 
```
  
 Check if the folder was created successfully.
  

```sh
ls ~/.ssh/
```

  ![one.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669817802504/cG3U6SedJ.png align="left")

Generate the keys using ssh-keygen. The -t flag specifies the type of key to generate. Options for ssh protocol 2 are dsa, ecdsa, ed25519, rsa, and rsa1. The -C flag stands for comment. Using the ed25519 generate the key using the command below

```sh
ssh-keygen -t ed25519 -C "Key for the vultr ssh demo on my hashnode blog"
```


![two.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669835360914/3Fcp5NLp-.png align="left")


The output prompts a location to save the key. Save the key on the folder we created on step 1 and give the keys a name; vultrkey for my case.

```sh
/home/user/.ssh/vultrkeysfolder/vultrkey
 ``` 
![three.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669835754197/ecgiTYisE.png align="left")


The next step asks for a passphrase. A passphrase protects the private key from someone who doesn't know the passphrase. If an attacker were to get hold of the key, they would not be able to use it without the passphrase. Use an easy-to-remember passphrase. If you do not wish to have a passphrase, press enter. A random art based on the key will be generated on success.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669836033430/UK-4Hz46o.png align="left")


Two files will be generated. The file with the .pub extension is the public key, while the other is the private key. Your private key should always be kept a secret. The .pub key will be used on the server. You can check for the files generated using

```sh
ls ~/.ssh/vultrkeysfolder
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669836161689/zYPLboSN7.png align="left")

Head over to [Vultr](https://vultr.com) and create an account. Get [$100 Free to follow along](https://www.vultr.com/?ref=9301631-8H).  Click on  *Products*  on the left panel. Click on *Instances*, then click on *Deploy Server*.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669838074510/ATYUb_bDv.png align="left")

Choose the *Cloud Compute shared vCPU*  and the *AMD High Performance *option for the *CPU & Storage Technology*. 


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669879697033/R0PTuKRab.png align="left")

Choose a location that is closest to you. For the *Server Image*, choose  *Ubuntu 22.04 LTS x64*

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669838799298/n7D-Krges.png align="left")

Select the least server size possible; however, you are free to choose whichever configurations that work for you. 


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669880283296/nV7Li-0Yp.png align="left")

Scrolling down, there is an *SSH Keys* section. Click on the *Add New* button.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669838967554/fAE_mIh4T.png align="left")

This opens a new window as shown below

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669839066875/48nARYjoC.png align="left")

Going back to the terminal and open the file containing the public key using cat .

```sh
cat ~/.ssh/vultrkeysfolder/vultrkey.pub 
```
Copy the public key.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669839589566/qci_1EfEl.png align="left")
Paste the key on Vultr and click on *Add SSH Key*.
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669839630112/3oL5oeAmL.png align="left")

The SSH Keys section will show an added key on the dashboard.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669839741239/45yxkb4SQ.png align="left")

Click on the *Deploy Now *button to deploy the instance.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669839815174/lAEJTo1E6.png align="left")

Once the instance deploys, the status will change to Running. You may need to give the instance around 10 minutes to boot fully. 

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669880351795/tUcQbsDEb.png align="left")


Click on the instance and copy the IP Address.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669880425590/HakZ-EyZj.png align="left")

On your terminal connect to the server using the command

```sh
ssh -i ~/.ssh/vultrkeysfolder/vultrkey root@202.182.111.47
  ```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669880647165/rnGjJdZB7.png align="left")

Answer Yes to the prompt asking if you are sure you want to connect

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669880784658/SVXFlq9hP.png align="left")
Enter your passphrase, and you should be logged in to the Vultr instance.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669880937066/9h0BdX5jR.png align="left")

Resources

[https://www.ssh.com/academy/ssh/protocol](https://www.ssh.com/academy/ssh/protocol)

[https://www.commandlinux.com/man-page/man1/ssh-keygen.1.html](https://www.commandlinux.com/man-page/man1/ssh-keygen.1.html)
