Setting up SSH based security to access your server is a much more effective way than the use of a manual root password. Cracking the security system of a VPS depending on SSH keys is nearly impossible since it secures your VPS in a more sophisticated way by the use of encoded keys.
How do SSH keys work:
[ht_message mstyle="danger" title="Secure the private key" " show_icon="" id="" class="" style="" ]Make sure that you add the public key to the servers and the private key is saved in a secure location on your PC.[/ht_message]
Follow the below given steps to set up SSH keys:
Open the Terminal on your PC
Enter the following command in the terminal:
ssh-keygen -t rsa
The above key generating command will follow up with a couple of questions:
Enter file in which to save the key (/home/user/.ssh/id_rsa):
You can hit Enter, which will save the keys to the user home.
Enter passphrase (empty for no passphrase):
In this step, you will be asked for a passphrase for protecting your private key. We recommend you to add a passphrase since the whole point of setting up SSH is security. You will need to enter the passphrase every time you use the key pair.
If you do not want a passphrase associated with your private key, then simply hit enter leaving the passphrase field empty.
This will complete the key generation process:
ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/demo/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/demo/.ssh/id_rsa. Your public key has been saved in /home/demo/.ssh/id_rsa.pub. The key fingerprint is: 4a:dd:0a:c6:35:4e:3f:ed:27:38:8c:74:44:4d:93:67 demo@a The key's randomart image is: +--[ RSA 2048]----+ | .oo. | | . o.E | | + . o | | . = = . | | = S = . | | o + = + | | . o + o . | | . o | | | +-----------------+
In the above example:
The private key's location: /home/demo/.ssh/id_rsa
The public key's location: /home/demo/.ssh/id_rsa.pub
Now, you can add your public key to the VPS you want to set up SSH on.
Enter the following command to copy your public key to your VPS:
ssh-copy-id user@xxx.xx.xx.xx
This will copy your public key to the authorized_keys file on your server.
[ht_message mstyle="info" title="" " show_icon="" id="" class="" style="" ]Make sure to replace xxx.xx.xx.xx with your actual Ip address and replace user with your actual username in the above command.[/ht_message]
Once you enter the ssh-copy-id command, you will see information similar to this:
The authenticity of host 'xx.xx.xx.xx (xx.xx.xx.xx)' can't be established. RSA key fingerprint is b1:2d:33:67:ce:35:4d:5f:f3:a8:cd:c0:c4:48:86:12. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '12.34.56.78' (RSA) to the list of known hosts. user@12.34.56.78's password: Now try logging into the machine, with "ssh 'user@12.34.56.78'", and check in: ~/.ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting.
Directory .ssh should have 700 permissions and authorized_keys file should have 400 or 600 permissions. To change the permissions, use the following commands:
$ cd $ mkdir .ssh && touch .ssh/authorized_keys $ chmod 700 .ssh/ && chmod 600 .ssh/authorized_keys $ cat id_rsa.pub >> .ssh/authorized_keys && rm id_rsa.pub
Now log into the remote server using ssh or scp/sftp:
$ ssh username@< server-ip >