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 needing 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 your 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:

How to Set Up SSH Keys with PuTTY
How to Set Up SSH Keys with Linux Commandline

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 make sure they key is contained on one line.

Example should look like this:

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

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).

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

Wayne Egerer

Leave a Reply