How do I set up SSH key authentication?

SSH packets being sent from the SSH client to the server are encrypted with a form of shared-key cryptography, using a random key which is generated for each new connection and thrown away when that connection is over. The client and the server use public-key cryptography to agree on the session key, and either party may request a re-keying of the session at any time.

Once you become familiar with SSH keys, communication and file copying between servers / clients will be secure, quicker, and more convenient.

Here’s an example on setting it up between a CentOS Client and CentOS Server:

On the client, do the following:

  • Goto the .ssh directory, which is located under /root – full path is /root/.ssh
  • Now let’s create our private and public keys and put them into a file.
This created a 1024 bit key, and creates 2 files.

  1. id_dsa_something – This holds your client’s PRIVATE Key.
  2. – This holds your server’s PUBLIC key.

Now, let’s place the key into the servers authorized_keys file. Located at: /root/.ssh/authorized_keys, If this file is not already there, we will create it.

Next you need to copy the key to your system. We’ll copy the key over via a file copying program called rsync

Make sure to change SERVERSIP to the servers IP address. After doing this command, you will be prompted for the root password of the server, type it and press enter.

Now, on the server, do the following:

The 2nd command copies the contents of into authorized_keys file. The 3rd command gives it the correct permissions to be run by the system.

Now, back on the client, do the following:

2nd command: Starts the SSH agent program.

3rd and 4th command: Adds your private key into memory.

Simply SSH into the server.

When prompted, type in the root password. Now exit out and try to SSH into the server from the client once more. This time – you shouldn’t be prompted for a password.


Rate This Article

(183 out of 352 people found this article helpful)

About The Author