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/id_rsa.pub.
The key fingerprint is:
e7:97:c7:e2:0e:f9:0e:4c:d6:d7:cb:e5:31:11:92:14 alex@example.com.
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
Working in IT for more than 15 years now, I’ve ran into and solved problems that are very specific to network and system administration.
I have implemented my expertise in designing, developing and executing technical solutions that have passed the test of time, managing simple and complex systems, and administrating networks.