How to Set Up SSH Keys for VPS & Dedicated Server – Add SSH Keys for VPS : Using SSH to login to VPS or Dedicated server is a secure way as it prevent our server from the brute force attack. When you generate SSH keys, it generates a pair of keys, you get two long string of character that is a
public key and a
• Private Key : If you have the Private key then you can login to the server if its public key has been added to the server (VPS or Dedicated Server)
One of immediate benefit of using SSH key authentication method over traditional password authentication method is that we can login to our server without using the server password. So if you do not know how to generate the ssh keys for the Ubuntu, CentOS VPS & dedicated server and how to add / configure ssh keys with server then we are going to discuss how to generate / setup ssh keys for VPS & Dedicated Server.
How to Generate SSH keys & Configure with VPS / Dedicated Server
It is very simple to generate the SSH keys, just follow the simple steps given below to generate & configure the SSH Keys (public key & private key) with VPS / Dedicated Server.
In the very first step we have to create the pair of SSH key on your local machine ( i.e. your computer):
ssh-keygen -t rsa
Now it will ask you for entering the file in which you want to save the key, so just enter the custom file name or just press enter for saving in the default file, in my case it is
Enter file in which to save the key (/Users/aman/.ssh/id_rsa):
Now it will ask you for the
Enter passphrase (empty for no passphrase):
It is not compulsory to enter the passphrase, you can leave this empty too. But it is recommended that you should enter the passphrase so that your Server will be secured. This passphrase will be used when you will try to log in your server from your machine (Computer).
Once you enter the passphrase just confirm it or if you want to leave it empty then just press enter. Then will will give you the following output:
Your identification has been saved in /Users/aman/.ssh/id_rsa. Your public key has been saved in /Users/aman/.ssh/id_rsa.pub. The key fingerprint is: SHA256:N8152QiumrTBMFroUYGYBCtuNMVw0ixb0WdvV+x1KMM [email protected] The key's randomart image is: +---[RSA 2048]----+ |.=**o.. .. . | | o*=.. + Eo...| |oo+ + . +o. .| |+.. o o = + + | | o o + S + = + .| |. . + + . o . | | o + . | | . = | | + | +----[SHA256]-----+
So now SSH key has been generated and
public key has been stored in file
/Users/aman/.ssh/id_rsa.pub and the private key (identification) is stored in
Now Copying the Public Key
As pair of key is generated so now print the SSH key and copy it :
SSH key will be printed and it will look like :
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC23/VxGmIGOvSTFRI4ZvGdErUburn+gdDgBvAueaLzIC+1K+Khn2KPtwxfWDRBXqWa3BuI0dJuXabgZCSZZhYOp8SFzXJwpE5EP7H7kZqCeOzOQADb3wHgmAFt2LcNtyCrWfa+oJJZE2FyZWuX5Uevce3lrcvjOkAje/vfFCR+E6O/Bi31IXO+InydPZ2mTgbc61Chy2tzqJkrBcVj0pj2mtBejdZgOjAhlY04sQR+0VSnTgvC85o3ujgvMYIFnHHt3tx7hp1PCyUX5I2I0iKtU27LcwpSOZvwKZLYJCjcARThsapuF9ogAU+t+ZFqGwk5pSppjVS2gb1bFCYzOXoP [email protected]
So just copy the
public key and save it to clipboard.
So we already have copied the public key and now we have to add the public key to the server where we want to use. Run the following command :
cat ~/.ssh/id_rsa.pub | ssh [email protected] "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
It will ask you for the password of User1 or your VPS User which you are using for login. So just enter the password
Replace user1 with your vps username & 220.127.116.11 with your
So the SSH key
public key has been added to your VPS / Dedicated server and now you can login to your VPS Server by running following command line (replace username & server IP):
It will login to your server without asking for the server password or if you have created passphrase for the ssh key then it will ask you to enter the passphrase for the SSH key. So this was all about how to generate & configure SSH for VPS / Dedicated Server.