How To's

Secure Your CentOS Server using SSH Keys - Linux

5,867 views · 1 found this helpful

Was this helpful?

Generate Our Keys

We will be generating a public and private key. The public key will be transferred to the server and the private key will remain on your desktop.

Let's begin by opening a terminal on your Linux desktop and following the instructions below.

ssh-keygen -t rsa

For extra security, you can make the key contain 4096 bits, instead of the default 2048.

ssh-keygen -t rsa -b 4096

Both the public and private key will be created in the ~/.ssh directory.

Finally, let's set the permissions on our private key.

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

Install Public Key on the Server

Next, you'll need to copy the public key to the server. SSH-copy-id will copy the public key to the server and install it.

1.2.3.4 is the IP of our hypothetical server.

ssh-copy-id -i ~/.ssh/id_rsa.pub root@1.2.3.4

All of the remaining instructions will be performed on the server

Set file permissions

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Set the SELinux contexts

restorecon -Rv ~/.ssh 

Configure OpenSSH to no Longer Accept Passwords

SSH to the server

Open up the sshd_config file using your favourite text editor and turn password authentication off.

 nano /etc/sshd_config
PasswordAuthentication no

Restart OpenSSH

service sshd restart

Connect to your Server Using SSH Keys

You should now be able to login without being prompted for a password

 ssh root@1.2.3.4
  • Updated

← All articles

Still stuck?

Our engineers answer tickets directly, 24/7.