Overview
This guide explains how to set up public‑key authentication so you can log in to a remote server without entering a password each time.
Why use key‑based authentication?
- Security – Keys are harder to brute‑force than passwords.
- Convenience – No need to type a password for every SSH session.
- Automation – Essential for scripts, CI/CD pipelines, and remote management.
Prerequisites
- A Unix‑like client (Linux/macOS) with
sshandssh-keygeninstalled. - Access to the remote server with a user account and password.
Step 1 – Generate an SSH key pair
ssh-keygen
- Press Enter to accept the default file location (
~/.ssh/id_rsa). - Leave the passphrase empty (press Enter twice) for password‑less usage.
You should see output similar to:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/me/.ssh/id_rsa):
Created directory '/home/me/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/me/.ssh/id_rsa
Your public key has been saved in /home/me/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:ZOW6KfiX4kGQ/Fu0Lb2hUoRZO/wEtYTa5DmlKvG2y9I me@mycomputer
The key's randomart image is:
+---[RSA 3072]----+
| oo+ |
| . . =o*.. |
| + o=O++ |
| .o.=*O |
| oo.S.= |
| .o++ = o |
| .+=.+.. |
| .oE+o |
| o=+ |
+----[SHA256]-----+
Step 2 – Verify SSH access with password
ssh remoteuser@remoteserver
You should be prompted for the remote user’s password. Once logged in, exit with exit.
Step 3 – Copy the public key to the server
scp ~/.ssh/id_rsa.pub remoteuser@remoteserver:.
After the transfer, log back in:
ssh remoteuser@remoteserver
Now you should see the prompt without a password.
Step 4 – Append the key to authorized_keys
On the server, run:
cat id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
If authorized_keys did not exist, create it first:
touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys
Step 5 – Test password‑less login
ssh remoteuser@remoteserver
You should now log in without being asked for a password.
Troubleshooting
- Permission errors – Ensure
~/.sshandauthorized_keysare owned by you and have permissions700and600respectively. - Wrong key – Verify the public key content matches the one on the server.
- Host key verification – If you see a warning about the host key changing, remove the old entry from
~/.ssh/known_hosts.
References