If you connect to a remote server regularly, typing your password every time gets old fast. Key-based authentication is the standard way around this — it is more secure and more convenient once set up.
Generate a key pair
On your local machine, run:
ssh-keygen -t rsa
This creates a 2048-bit RSA key pair in ~/.ssh/. The public key is saved as id_rsa.pub, the private key as id_rsa. Keep the private key safe — anyone who has it can log into any server that trusts the matching public key.
Copy the public key to the server
Copy the public key to the remote server and append it to the authorized keys file:
scp ~/.ssh/id_rsa.pub my_username@mySshServer:~/.ssh/id_rsa_local.pub
cd ~/.ssh/
cat id_rsa_local.pub >> authorized_keys2
Make sure the permissions are correct on the server (~/.ssh should be 700, authorized_keys2 should be 600). If the permissions are too loose, SSH will ignore the key and fall back to password authentication.
Connect using sftp
You can also connect using SFTP with the same key pair:
sftp://my_username@mySshServer/home/my_username
Using ssh-agent to avoid typing your passphrase
If you set a passphrase on your key (recommended), you will be prompted for it the first time you use the key each session. You can avoid this by adding the key to ssh-agent:
eval $(ssh-agent)
ssh-add ~/.ssh/id_rsa
Enter your passphrase once, and ssh-agent will remember it for the rest of your session. On most desktop Linux distributions, ssh-agent starts automatically when you log in.
Troubleshooting
If SSH still asks for a password, check these in order:
- Server file permissions: ~/.ssh must be 700, authorized_keys must be 600
- You appended the correct public key (compare with cat ~/.ssh/id_rsa.pub on your local machine)
- The server’s /etc/ssh/sshd_config has PubkeyAuthentication set to yes
- Check /var/log/auth.log on the server for specific error messages
Once it works, you can SSH into the server without being prompted for a password.
Enjoy!
“ssh-keygen -t dsa” is more secure and should be used instead of “rsa”…