Setting up SSH Trust Between Two Servers

The SSH protocol uses encryption to secure the connection between a client and a server. All user authentication, commands, output, and file transfers are encrypted to protect against attacks in the network.

Let’s say you want to setup a cron job to copy some files to another server, like backup files. By far the most secure way is to log in to the remote server with your public key.

With OpenSSH, an SSH key is created using ssh-keygen. In the simplest form, just run ssh-keygen and answer the questions. The following example illustrates this.

Generate an RSA key pair by typing the following at a shell prompt:

~]$ ssh-keygen -t rsa 
Generating public/private rsa key pair. Enter file in which to save the key (/home/alex/.ssh/id_rsa):

Press Enter to confirm the default location (that is, ~/.ssh/id_rsa) for the newly created key.

Enter a passphrase, and confirm it by entering it again when prompted to do so. For security reasons, avoid using the same password as you use to log in to your account. After this, you will be presented with a message similar to this:

Your identification has been saved in /home/alex/.ssh/id_rsa. 
Your public key has been saved in /home/alex/.ssh/
The key fingerprint is:
The key's randomart image is:
+--[ RSA 2048]----+
| E. |
| . . |
| o . |
| . .|
| S . . |
| + o o ..|
| * * +oo|
| O +..=|
| o* o.|

Change the permissions of the ~/.ssh/ directory:

~]$ chmod 700 ~/.ssh

Now to push your key to the remote machine you want to ssh into type the following command:

~]$  ssh-copy-id remote-user@remote-host 

That’s all! Now just try to ssh into the host. You will no be prompted for a password:

~]$  ssh remote-host 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.