Terminal showing ssh-keygen generating an ed25519 key pair and the resulting public key

How to Set Up SSH Key Authentication on Ubuntu (Keygen to Login)

To set up SSH key authentication on Ubuntu, generate a key pair on the machine you connect from with ssh-keygen -t ed25519, copy the public half to the server with ssh-copy-id user@server, then connect with ssh user@server. Your keys live in ~/.ssh/id_ed25519 (private) and ~/.ssh/id_ed25519.pub (public) on the client. On the server, the public key goes into ~/.ssh/authorized_keys in the home directory of the user you log in as, with ~/.ssh set to 700 and authorized_keys to 600.

Password SSH is fine for a homelab. SSH keys are better. They’re harder to brute-force, you don’t have to type a password every time, and once you set up an SSH agent you can log into a dozen servers with a single passphrase entered once per session.

This is the companion to the Enable SSH on Ubuntu Server guide. Once you have SSH working, this is the next step. Commands and defaults below are checked against the OpenSSH 9.6p1 man pages that ship with Ubuntu 24.04 LTS and the Ubuntu Server OpenSSH documentation.

Tested on: Ubuntu Server 22.04 LTS, Ubuntu Server 24.04 LTS, Windows 11. Same instructions work on Raspberry Pi OS, Debian, and most Linux distributions.

Terminal showing ssh-keygen generating an ed25519 key pair and the resulting public key
Real command output from a test machine, not a mock-up.

The short version

Step Where Command
1. Generate a key pair Client ssh-keygen -t ed25519 -C "you@laptop"
2. Install the public key Client ssh-copy-id user@server
2 (alternative). Pull keys from GitHub Server ssh-import-id gh:your-github-username
3. Log in with the key Client ssh user@server
4. Load the key once per session Client ssh-add ~/.ssh/id_ed25519
5. Fix permissions if login fails Server chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys

What SSH keys actually are (in 60 seconds)

An SSH key is a pair of files: a private key (kept secret on your client machine) and a public key (copied to the server). When you connect, the server challenges the client to prove it has the private key. If the maths works out, you’re in. The server never sees your private key, only the proof that you have it.

The advantage over passwords is length and guessability. A key cannot be guessed from a wordlist, and a stolen server-side authorized_keys file gives an attacker nothing they can log in with, because it only holds public keys.

Step 1: Generate the key (on your client, not the server)

Ed25519 is the type to use. Since OpenSSH 9.5 it is also the default: on Ubuntu 24.04, running ssh-keygen with no arguments generates an Ed25519 key, per the ssh-keygen man page. Spell it out anyway, so the command does the same thing on older systems:

ssh-keygen -t ed25519 -C "you@laptop"

The -C comment is just a label that ends up at the end of the public key. Make it say which machine the key belongs to, so you can tell keys apart in authorized_keys later.

You’ll be asked:

Enter file in which to save the key (/home/you/.ssh/id_ed25519):

Press Enter to accept the default. Multiple keys for different purposes go in different files (e.g. ~/.ssh/id_ed25519_homelab) but for your first key, the default is fine.

Enter passphrase (empty for no passphrase):

Set a passphrase. It encrypts the private key on disk. If your laptop is stolen, the attacker has to crack the passphrase before they can use the key. Without a passphrase, theft of the laptop means immediate access to every server the key can reach.

You can avoid typing the passphrase repeatedly with ssh-agent (covered below), so don’t skip it because you can’t be bothered.

Example output (your fingerprint and randomart will differ):

Generating public/private ed25519 key pair.
Your identification has been saved in /home/you/.ssh/id_ed25519
Your public key has been saved in /home/you/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:uX1dDpYsIsBQLxmvDNKsiJQh1fHKA+zvUKga38kNono you@laptop
The key's randomart image is:
+--[ED25519 256]--+
|      ...        |
+----[SHA256]-----+

Where Ubuntu stores SSH keys

File Machine What it is
~/.ssh/id_ed25519 Client Your private key. Never copy it anywhere it doesn’t need to be.
~/.ssh/id_ed25519.pub Client Your public key. Safe to share; this is what goes on servers.
~/.ssh/authorized_keys Server Public keys allowed to log in as this user, one per line.
~/.ssh/known_hosts Client Host keys of servers you’ve connected to (not your keys).
~/.ssh/config Client Per-host settings: user, port, which key to use.
/etc/ssh/ssh_host_*_key Server The server’s own host keys. Nothing to do with user login keys.

~ is the home directory of the user in question, so for a user called alex the server file is /home/alex/.ssh/authorized_keys, and for root it is /root/.ssh/authorized_keys. On Windows the client files live in C:\Users\YOUR-USER\.ssh\.

Step 2: Copy the public key to the server

Option A: ssh-copy-id (the normal way)

ssh-copy-id username@server-ip

This logs in with your password (the last time you’ll need it), creates ~/.ssh/authorized_keys if needed, and appends your public key. Typical output ends with:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh username@server-ip"
and check to make sure that only the key(s) you wanted were added.

Useful variations, all from the ssh-copy-id man page:

# Copy a specific key rather than whatever the agent holds
ssh-copy-id -i ~/.ssh/id_ed25519_homelab.pub username@server-ip

# Server listens on a non-standard port
ssh-copy-id -p 2222 username@server-ip

# Dry run: show which keys would be copied, change nothing
ssh-copy-id -n username@server-ip

Without -i, ssh-copy-id copies the keys your agent currently holds (ssh-add -L), falling back to your default key file. If you have several keys loaded, use -i so you only install the one you meant.

Option B: ssh-import-id (pull your keys from GitHub)

Ubuntu ships ssh-import-id, which runs on the server and fetches your published public keys from GitHub or Launchpad, appending them to ~/.ssh/authorized_keys:

# From GitHub (uses https://github.com/USERNAME.keys)
ssh-import-id gh:your-github-username

# From Launchpad (the default if you give no prefix)
ssh-import-id lp:your-launchpad-id

# Remove keys that were imported this way
ssh-import-id -r gh:your-github-username

Each imported line is tagged with a comment such as # ssh-import-id gh:your-github-username, which is how -r finds them again (ssh-import-id man page). This is the same mechanism as the Import SSH key option on the Ubuntu Server installer’s SSH screen: pick GitHub or Launchpad, enter your username, and the keys are in place on first boot. It only works if you’ve already added the public key to that account.

Option C: by hand

Useful when ssh-copy-id isn’t available (Windows PowerShell has no ssh-copy-id) or you’re working through a jump host:

cat ~/.ssh/id_ed25519.pub | ssh username@server-ip 'mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys'

All three achieve the same end state: your public key on its own line in the server’s authorized_keys.

Step 3: Test it

ssh username@server-ip

If your key worked, you’ll be prompted for the passphrase you set on the key, not for the server password. That’s the difference: you’re decrypting your local key, not authenticating against a remote password.

If you’re prompted for the server password instead of the key passphrase, the key wasn’t accepted and SSH fell back to password login. Skip to the troubleshooting section below.

Step 4: Use ssh-agent so you don’t type the passphrase every time

ssh-agent is a small background process that holds decrypted private keys in memory. Once you’ve entered the passphrase once per session, the agent answers SSH’s auth challenges without prompting you again.

Ubuntu Desktop and macOS start an agent for you at login. On Ubuntu Server over SSH, there usually isn’t one, so start it manually:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

You’ll be prompted for the passphrase once. After that, every SSH command in that shell uses the agent. Check what it holds with ssh-add -l.

To start an agent automatically in new shells, add to ~/.bashrc or ~/.zshrc:

if [ -z "$SSH_AUTH_SOCK" ]; then
  eval "$(ssh-agent -s)" >/dev/null
fi

Or use a desktop’s built-in keychain integration (GNOME Keyring, KDE Wallet, macOS Keychain). If you’d rather not start agents at all, add AddKeysToAgent yes to ~/.ssh/config on a desktop and the key is loaded the first time you use it.

Permissions: 600 or 644?

SSH checks file permissions on both ends, and gets it wrong in silence on the server side, which is why this causes more failed logins than anything else.

# On the client
chmod 700 ~/.ssh                          # directory: only you can enter it
chmod 600 ~/.ssh/id_ed25519               # private key: only you can read or write
chmod 644 ~/.ssh/id_ed25519.pub           # public key: anyone can read

# On the server
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod go-w ~                              # home directory must not be group/world-writable

For authorized_keys, both 600 and 644 work. What sshd actually rejects (with the default StrictModes yes) is a file, ~/.ssh directory or home directory that is writable by the group or other users, or owned by someone else. The sshd man page recommends read/write for the user and not accessible by others, so use 600.

The private key is stricter. If it’s readable by others, the client refuses to use it:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/home/you/.ssh/id_ed25519' are too open.
...
This private key will be ignored.

Fix it with chmod 600 ~/.ssh/id_ed25519. This usually happens after copying keys from a backup, a USB stick or a Windows filesystem.

The authorized_keys file on the server

Ubuntu’s sshd reads .ssh/authorized_keys (and the legacy .ssh/authorized_keys2) relative to the user’s home directory; that is the documented default for AuthorizedKeysFile. Each line is the entire content of one .pub file: key type, the key itself, and the comment.

cat ~/.ssh/authorized_keys

Example:

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5...XYZ you@laptop
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5...ABC you@desktop
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5...DEF # ssh-import-id gh:your-github-username

One key per line, one line per authorised client. Two rules catch people out: each key must be on a single line (a key pasted from a web page often picks up line breaks), and the file name is authorized_keys with American spelling and no extension.

To create the file by hand as the user who will log in:

mkdir -p ~/.ssh && chmod 700 ~/.ssh
touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys
nano ~/.ssh/authorized_keys     # paste the public key on one line

If you set it up for another user with sudo, finish with sudo chown -R alex:alex /home/alex/.ssh, or sshd will reject the file because root owns it.

To revoke a key, delete its line. The next attempt by that key fails with Permission denied (publickey), or falls back to a password prompt if passwords are still allowed.

A line can also start with options that restrict what the key may do. Two useful ones from the sshd man page’s AUTHORIZED_KEYS FILE FORMAT section:

# Only accept this key from the home LAN
from="192.168.1.0/24" ssh-ed25519 AAAAC3...XYZ you@laptop

# Backup key that can only run one command, no shell or forwarding
restrict,command="/usr/local/bin/backup.sh" ssh-ed25519 AAAAC3...ABC backup@nas

Check the server allows key logins

Ubuntu’s default is PubkeyAuthentication yes, so this normally needs no change. On Ubuntu 24.04, the main /etc/ssh/sshd_config starts with Include /etc/ssh/sshd_config.d/*.conf, and for most settings the first value sshd reads wins, so a drop-in file can override the main file. Rather than grepping files, ask sshd what it is actually using:

sudo sshd -T | grep -Ei 'pubkeyauthentication|authorizedkeysfile|passwordauthentication'

Expected on a default install (example output):

pubkeyauthentication yes
passwordauthentication yes
authorizedkeysfile .ssh/authorized_keys .ssh/authorized_keys2

If you change anything, put it in a drop-in such as /etc/ssh/sshd_config.d/10-keys.conf, check the syntax with sudo sshd -t and apply it with sudo systemctl restart ssh. On Ubuntu the service is called ssh, not sshd.

Which key type: Ed25519, RSA or ECDSA?

Type Recommendation Why
Ed25519 Use this Small, fast, secure. The ssh-keygen default since OpenSSH 9.5 and the first recommendation in Ubuntu’s docs.
RSA 4096 Fallback for old kit For devices that don’t support Ed25519 (some older network gear and appliances). ssh-keygen -t rsa -b 4096.
RSA 2048 Avoid for new keys Still accepted, but there’s no reason to create one now.
ECDSA Works, rarely needed Uses the NIST curves; some people distrust them. Ed25519 is the simpler choice. Use ECDSA (or ecdsa-sk) only if a hardware token or policy requires it.
DSA Gone Disabled by default since 2015 and removed entirely in OpenSSH 10.0.

Troubleshooting

The single most useful command is a verbose connection from the client. Read it from the bottom up:

ssh -v username@server-ip
debug1: Offering public key: /home/you/.ssh/id_ed25519 ED25519 SHA256:...
debug1: Authentications that can continue: publickey,password

If your key is offered but rejected, the problem is on the server (permissions, ownership, wrong user, wrong file). If your key isn’t offered at all, the client can’t see it: check the file name, path and permissions in ~/.ssh/ on the client. The server’s side of the story is in its log:

sudo journalctl -u ssh -f

“Permission denied (publickey)”

The server accepts only keys and none of yours matched. Most common causes, in order:

  1. Permissions or ownership on the server. Run chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys && chmod go-w ~. The server log will say Authentication refused: bad ownership or modes for directory /home/alex (or for file) when this is the cause.
  2. Wrong username. The key is in one user’s authorized_keys and you’re logging in as another. ssh alex@server reads /home/alex/.ssh/authorized_keys, nothing else.
  3. Wrong file name. authorized_keys, plural, US spelling. Common typos: authorized_key, authorised_keys.
  4. Broken key line. A key split across two lines, or the private key pasted instead of the .pub.
  5. Wrong key offered. You have several keys and the right one isn’t being tried. Use ssh -i ~/.ssh/id_ed25519_homelab user@server or a ~/.ssh/config entry (below).

The key still asks for a password

The server isn’t accepting your key, so it falls back to password auth. Work through the list above; the server log will tell you which one it is.

“/usr/bin/ssh-copy-id: ERROR: No identities found”

ssh-copy-id couldn’t find a key to copy: none in the agent and no default key file. Generate one first (Step 1), or point at it with -i ~/.ssh/yourkey.pub.

“ssh-import-id-gh: command not found” or ssh-import-id missing

On Ubuntu it comes in as a recommended package of openssh-server, so a minimal install, a system built with --no-install-recommends or a Debian box may not have it. Install it with sudo apt install ssh-import-id. If it runs but imports nothing, check https://github.com/your-username.keys in a browser: an empty page means there’s no public key on that GitHub account.

“Too many authentication failures”

Your agent offered several keys, each counted as a failed attempt, and the server hung up before reaching the right one. Add IdentitiesOnly yes and an IdentityFile line for that host in ~/.ssh/config, so only the right key is offered.

“Could not open a connection to your authentication agent”

ssh-add can’t find a running agent in this shell. Run eval "$(ssh-agent -s)" first, then ssh-add.

“sign_and_send_pubkey: no mutual signature supported”

The client and server can’t agree on a signature algorithm for an RSA key. It usually means a very old server or device that only speaks the SHA-1 ssh-rsa algorithm, which OpenSSH disabled by default in 8.8. The clean fix is an Ed25519 key, or an upgrade on the old end. The OpenSSH 8.8 release notes document the per-host PubkeyAcceptedAlgorithms +ssh-rsa workaround if you must keep talking to it.

“Agent admitted failure to sign”

The agent has the key but the signing operation is failing, usually because of a stale or corrupt key in the agent.

ssh-add -D                    # remove all keys from the agent
ssh-add ~/.ssh/id_ed25519     # re-add

Multiple keys: which one does SSH use?

By default SSH tries the keys in your agent and the default files (~/.ssh/id_ed25519, ~/.ssh/id_rsa and so on) until one works. To force a specific key for a specific host, use ~/.ssh/config:

Host homelab
  HostName 192.168.1.50
  User pi
  IdentityFile ~/.ssh/id_ed25519_homelab
  IdentitiesOnly yes

Then ssh homelab connects with the right key, no IP needed.

What’s next

With keys working, the next step is to disable password authentication entirely. Once that’s done, brute-force attempts can’t get in regardless of how weak someone’s password is.

That’s covered in the SSH Hardening guide. Don’t disable passwords until your keys are working from every client you care about, and keep a second session open while you test, or you’ll lock yourself out.

Lab Skill, Production Requirement

Outside the homelab, key-based auth is the normal way in on cloud platforms: AWS EC2 Linux instances use key pairs for access, Azure recommends SSH keys for Linux VMs, and GitHub dropped password authentication for Git operations in 2021. If you work in IT and haven’t set up SSH keys yet, it’s one of the cheapest gaps to close.

Learn it on your Pi or your Proxmox node, and you already know how it works on every cloud platform. The ssh-keygen command, the authorized_keys file, the permissions dance. All identical. The enterprise version adds a certificate authority and a key rotation policy on top of what you already know.


Companion guides: Enable SSH on Ubuntu Server, Enable SSH on Raspberry Pi, SSH Hardening, Common SSH Errors.

Enjoyed this guide?

New articles on Linux, homelab, cloud, and automation every 2 days. No spam, unsubscribe anytime.

Scroll to Top