How to use SSH Key..!!!

Creating SSH Keys

On your Mac or Linux machine, open Terminal.

Note: If you are using a Windows OS to SSH into a server, you will need to download third-party software as Windows does not allow SSH by default.

Verify that you have a .ssh folder in your $HOME directory. If the folder does not exist, create it:

$ mkdir ~/.ssh

Change your working directory to the .ssh directory and use the following command to generate an ED25519 SSH key pair:

$ ssh-keygen -t ed25519 -a 256

The “-t” in this command tells your computer what encryption type to use for the SSH key. If you would like to use a different encryption type, replace the “ed25519” with whichever encryption you choose.

Note: If you would like to store an SSH key in the OVHcloud Manager, you will only be able to use ED25519, RSA, or ECDSA encryption.

You will be prompted to enter a passphrase to password-protect your SSH key. This is entirely optional but recommended for added security. Your SSH keys will be created and stored in the .ssh directory. In order to read your public key, use the following command and copy the output:

$ cat ~/.ssh/id_ed25519.pub

Now that you have created your SSH key pair, use the next section to tell your server that it can authenticate the public key you have just created.

Adding SSH Keys to Your Server

Navigate to your $HOME directory and look for a .ssh. If one does not already exist, create it by entering the following:

$ mkdir ~/.ssh

Create a folder to store your authorized keys. To do this, open a file with the name authorized_keys in a text editor of your choice (we’ll use vim). Navigate to the .ssh directory that you just created and open the file in a text editor of your choice with the following command:

$ sudo vi ~/.ssh/authorized_keys

Copy and paste the public key which you created in the previous section into this new text file. Save the file and exit the text editor. Restart your server or restart OpenSSH using the following command:

$ sudo systemctl restart sshd

To test that your key has been set up properly, attempt to access your server via SSH using the following command, remembering to replace “IP_ADDRESSorHOSTNAME” with the IP address or hostname of the server you are trying to access:

$ ssh [email protected]_ADDRESSorHOSTNAME

Adding Additional Authorized Keys to Your Server

To add additional authorized SSH keys for additional users, follow this article again using the new user’s $HOME directory to create that user’s unique key.

Removing Authorized Keys from Your Server

Remove the key which corresponds to that user from your authorized_keys file. Upon removing the key, save the file and exit the text editor.

Conclusion

SSH key pairs are important to ensuring the security of your server. While the steps you took using this article should be sufficient for most use cases, it is worth noting that OpenSSH can be configured to be more secure if that extra security is needed. Regardless of what your security needs are, they are too important to not use the strong layer of security which SSH keys provide to you.