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.
1 |
ssh-keygen -t dsa -b 1024 -f id_dsa_something -C 'Client' |
- id_dsa_something – This holds your client’s PRIVATE Key.
- id_dsa_something.pub – This holds your server’s PUBLIC key.
Now, let’s place the key id_dsa_something.pub 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
1 |
rsync -av -e ssh id_dsa_something.pub SERVERSIP:/root/.ssh/ |
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:
1 2 3 |
cd /root/.ssh cat id_dsa_something.pub >> authorized_keys chmod 600 authorized_hosts |
The 2nd command copies the contents of id_dsa_something.pub 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:
1 2 3 4 |
cd /root/.ssh eval `ssh-agent` ssh-add id_dsa_something ssh-add -l |
2nd command: Starts the SSH agent program.
3rd and 4th command: Adds your private key into memory.
Simply SSH into the server.
1 |
ssh serversIP |
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.