How to add your SSH public key to CentOS

In this tutorial, we are going to add our public key for authentication through SSH. There are many benefits to this type of setup which include:

  • No longer need to enter a password (unless you encrypt your keys with password protection *recommended*)
  • Once public/private key pair authentication is configured, you can disable password authentication completely and only allow access via the key. Stops brute force attacks in its tracks.

Once you have the public key that you generated via PuTTY and/or Linux Commandline, you will need to add it to the destination server that you are trying to connect to.

If you have not generated your public and private key yet you can follow either of the below howtos:

To do this we will first create the hidden .ssh directory:

mkdir ~/.ssh

Now we will create/edit the authorized_keys that will be located inside the .ssh directory we just created.

vi ~/.ssh/authorized_keys

You should now paste your public key into this file. Please note making sure the key is contained on one line.

An example should look like this:

# cat .ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAt+PzzOFcecabwsXnjPPd+eqrqF5d2qh6kRIbnInSgwqWlPvnyfxa2Ye1xhGjCssbYdPWA7epJ/42yMFQfg6RGynW9XjWMomWeA/1+2LGY4B7JBJQfuTdDB/AimJRQvlSmxklfktmuqx0S8u67mLdkRWY+uQD8Ec7TCxWC9pU5Hv3Hq4Rfg5KLZl/gcJyMCr3nhKXXnL65pAM0EdCmkefHxvHJ4InVuzXmDru7GVQXH1bd3Uy9UIRhIs9wORlTvwesUzWEH/eTCjGSTgGRaEguo9FISRcilODuYQrcrvN8eILZxXNsiprw0azMibonkb3yuQ6mfVxtRoB4JL3vsFIbw== vps

Let’s now set permissions on the key files:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa

The above permissions are required if StrictModes is set to yes in /etc/ssh/sshd_config (the default).

Run service sshd reload to make sure the settings are active

You should now be able to SSH to your server using your SSH Key authentication.

 
 

 

 

Was this answer helpful? 0 Users Found This Useful (0 Votes)