Setup public key for SSH is a secure data transfer protocol that allows users to copy data from the client to the server in an encrypted and secure way. With the SSH key users can easily identify that the connection from their system is secure and they can now pass data between the two systems.
With public and private keys users can easily eliminate the need for static passwords. With these stored keys, they don’t have to write passwords, again and again, to connect to the server. Instead, they can store the SSH keys on the Virtual Private Servers (VPS) and connect automatically.
What is public key authentication in SSH?
In general language, we use passwords to protect our files. Consider these public keys as passwords that protect the communication between the client computer and the server. Unlike 8 to 20 character passwords, the public and private keys are in bits ranging from 1024, 2048, or 4096 bits.
These keys are stored in .ssh/authorized_keys. This way when the information is passed, it is locked and can only be unlocked through your private key.
Here is how to setup public and private keys for VPS using SSH.
Create RSA key pair
First of all, you will need to create public and private key pair for SSH connectivity with the VPS server.
To create a key pair on the client side, you will have write the following commands in the SSH.
ssh-keygen -t rsa
But you will need to first create a folder where you can keep the keys. The folder will be secure with a passphrase. However, you can leave the passphrase empty if you don’t want to set one yet.
Therefore, the above command will create two keys (one public, one private).
Here you need to store them in a folder. Here is how to create on using SSH.
chmod 700 ~/.ssh
Copy the Public Key
Next, you will have to copy the public key to the VPS server so that you can use it. Or, you can copy it to the folder on the VPS server using this command.
scp ~/.ssh/id_rsa.pub firstname.lastname@example.org:~/.ssh/uploaded_key.pub
The above command has two arguments. The first argument copies the command from the location of public key on the computer. So then the second argument is the location of the remote server where the public key should be copied too.
Connect to VPS
Furthermore you can connect to the server without using a password to connect.
Type the following command in the SSH:
If you have a few keys, you can specify what key to use with –i argument.
ssh -i ~/.ssh/id_rsa_test user@ip_address
Modify SSH configuration on VPS
You have successfully added public and private keys to SSH to connect to your VPS server. Now, if you want to change the SSH configuration file to change the port or to add a new IP address you can do so by modifying the SSH configuration. Here is how to do it.
Modify the SSH configuration file using the following command.
That’s it! Now, you know how to create SSH keys for your VPS!