How to Set Up SSH Keys: A Step-by-Step Guide

fidilen
2 min readApr 9, 2023

--

ssh_key

SSH keys are a secure form of authentication used to access remote computers. They are generated on the client side and stored on the server side to verify the user’s identity and allow them to securely access the server or computer. SSH keys also provide an additional layer of security by encrypting and decrypting data sent over the network, preventing any malicious third parties from intercepting the data.

SSH keys are much more convenient than passwords, as they don’t need to be remembered or changed frequently. Additionally, SSH keys are more difficult to brute-force than passwords, making them a much safer option for authenticating to a server.

Step 1: Generate the SSH Keys

Open your terminal and run the ssh-keygen command.

You can use the -t option to specify the type of key to create.

For example, to create an RSA key, run:

ssh-keygen -t rsa

For Ed25519 algorithm, run:

ssh-keygen -t ed25519 -C "your_email@example.com"

Once you have entered the command, you will encounter two questions, in which you can simply leave them blank by pressing Enter. This way, you won’t need to enter passphrase every time you connect to your server.

Note that there are two files generated namely id_rsa and id_rsa.pub or if you generated Ed25519, id_ed25519 and id_ed25519.pub.

Private keys are written in id_rsa or id_ed25519. Never share this to anyone!

Public keys are generated in id_rsa.pub or id_ed25519.pub. This shall also be saved to your Server.

Step 2: Save the SSH Keys as Authorized Keys

After creating the key pair, we must upload our public key to the remote virtual server.

From your local machine, open the terminal and connect to your remote server by entering this command:

ssh username@serverip

A few questions will popup:

  • Are you sure you want to continue connecting (yes/no)? — yes
  • username@serverip’s password

Once you have connected successfully, run this command and check if your public key is written:

sudo nano ~/.ssh/authorized_keys

If the command returns missing directory ~/.ssh means the key is not uploaded yet.

To save the public key, first create a directory:

mkdir -p ~/.ssh

Next is to create the authorized_keys file and paste your public key:

sudo nano ~/.ssh/authorized_keys

Shorten example of the content: ssh-rsa EXAMPLEzaC1yc2E…GvaQ== username@serverip

Conclusion

In conclusion, setting up SSH keys to a server is a straightforward process. With the help of this guide, you can easily configure SSH keys and use them to securely access your server. With the right SSH key setup, you can ensure that your server is secure and accessible only to authorized users.

Need Assistance?

Contact us via Discord.

--

--

fidilen
fidilen

Written by fidilen

0 Followers

Full Stack Developer · Bot Developer · Web Developer · API Developer · SQL Developer https://fidilen.com

No responses yet