SSH key authentication lets you log in from one Linux server to another without typing a password, which is essential for scripts, backups, and automation. Here’s how to set it up between server1 and server2.
1. Generate a Key Pair on server1
ssh myusername@server1
ssh-keygen -t rsa
Press Enter at each prompt to accept the defaults (leave the passphrase empty for fully passwordless logins). You’ll see output like this:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/local/myusername/.ssh/id_rsa):
Created directory '/home/local/myusername/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/local/myusername/.ssh/id_rsa.
Your public key has been saved in /home/local/myusername/.ssh/id_rsa.pub.
The key fingerprint is:
15:68:47:67:0d:40:e1:7c:9a:1c:25:18:be:ab:f1:3a myusername@server1
Display the public key and copy it. Make sure it stays on one line when you copy it; line breaks will stop the key from working.
cd .ssh
cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAyFS7Ykakcjdy...== myusername@server1
2. Add the Public Key on server2
ssh myusername@server2
mkdir .ssh
cd .ssh
vi authorized_keys
# paste the public key, then save
chmod 600 authorized_keys
3. Test It
ssh myusername@server1
ssh myusername@server2
# you should not be prompted for a password!
Tip: on most modern systems, ssh-copy-id myusername@server2 does step 2 for you in one command.
