Skip to main content

Linux and servers · Beginner · 10 min

How to Log In to a Linux Server with SSH Keys

By Padding · Updated 15 Nov 2026
Ubuntu 26.04.1 LTS · OpenSSH

You will create an Ed25519 SSH key, install its public half on a server, log in without a password, and save the connection as a short name.

On this page

An SSH key is a pair of files. The private key stays on your computer and proves who you are. The public key goes on the server. When you connect, the server checks that you hold the private key, and nothing secret crosses the network. A password can be guessed by a bot trying thousands of times an hour. A key cannot.

In this tutorial you will make a key pair, install the public key on a server, log in with it, and add a short alias for the connection. The commands use OpenSSH, the SSH client and server that ships with Ubuntu 26.04 (version 10.2).

Two computers are involved, and each step says where its commands run: your own computer (the client) and the server.

Step 1 · Create a key pair

Run this on your own computer, not on the server.

Bash
ssh-keygen -t ed25519 -C "you@example.com"
  • -t ed25519 chooses the key type. Ed25519 keys are short, fast and supported by every current OpenSSH version.
  • -C adds a comment to the public key. An email address or a machine name helps you recognise the key later.

ssh-keygen asks where to save the file. Press Enter to accept ~/.ssh/id_ed25519. Then it asks for a passphrase. Set one: it encrypts the private key on disk, so a stolen laptop does not hand over your servers. Step 5 shows how to avoid retyping it.

Check it worked

Bash
ls ~/.ssh
ssh-keygen -lf ~/.ssh/id_ed25519.pub

The directory holds id_ed25519 (private) and id_ed25519.pub (public). The second command prints the key's fingerprint, which looks like 256 SHA256:... you@example.com (ED25519).

Never send or copy id_ed25519. Only the .pub file leaves your computer.

Step 2 · Install the public key on the server

The server reads allowed keys from ~/.ssh/authorized_keys in the home directory of the user you log in as. Each line is one public key.

The easiest way, from your own computer, is ssh-copy-id. It logs in with your password one last time and adds the key for you.

Bash
ssh-copy-id deploy@203.0.113.10

Replace deploy with your user and the address with your server's. If the server already has several keys for that user, ssh-copy-id -i ~/.ssh/id_ed25519.pub deploy@203.0.113.10 names the one to copy.

If password login is already off and you cannot use ssh-copy-id, append the key by hand: print it on your computer with cat ~/.ssh/id_ed25519.pub, then, on the server as that user, add it:

Bash
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo 'ssh-ed25519 AAAA...your-key... you@example.com' >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

The permissions matter. The SSH server ignores authorized_keys if other users can write to it or to the directory that holds it.

Check it worked

On the server, as that user, list the folder:

Bash
ls -l ~/.ssh

The file authorized_keys is mode -rw------- and owned by your user.

Step 3 · Log in with the key

A real Ubuntu server already runs the SSH server as the ssh service, so there is nothing to start. From your own computer, connect:

Bash
ssh deploy@203.0.113.10

The first connection to a server asks you to trust its fingerprint. Compare it with the one your provider shows, then type yes. SSH then asks for your key's passphrase, if you set one, and logs you in.

To run one command and come back, put it after the address: ssh deploy@203.0.113.10 hostname. With -o BatchMode=yes, SSH never asks for a password or passphrase, which makes it useful in scripts: if the key does not work, the command fails at once.

Check it worked

You are logged in as deploy with no password prompt. If you set a passphrase, SSH asks for that instead. It decrypts the key on your computer and is not sent to the server.

Troubleshooting

When the key is ignored, SSH falls back to asking for a password, or fails with Permission denied (publickey). Run the client in verbose mode:

Bash
ssh -v deploy@203.0.113.10

Among the output, lines beginning Offering public key show which key SSH tried, and Authenticated to appears on success. Most failures come from one of three causes:

  • The permissions on ~/.ssh (should be 700) or ~/.ssh/authorized_keys (should be 600) are too open, or the files belong to another user.
  • The public key was pasted onto two lines. It must be one line.
  • You installed the key for a different user than the one you are logging in as.

On the server, the log says why. On Ubuntu 26.04 read it with sudo journalctl -u ssh --since "10 minutes ago".

Step 4 · Save the connection as a short name

Typing the user, address and key each time is slow. Put them in ~/.ssh/config on your computer.

Bash
cat >> ~/.ssh/config <<'CONF'
Host web1
    HostName 203.0.113.10
    User deploy
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes
CONF
chmod 600 ~/.ssh/config

HostName is the server's IP address or domain name.

IdentitiesOnly yes tells SSH to offer only the key named in the file. Without it, SSH tries every key your agent holds before the one you named, and a server that limits attempts (MaxAuthTries, 6 by default) can answer Too many authentication failures before the right key is tried.

Check it worked

Run ssh web1 whoami. It prints deploy. From now on, ssh web1 is enough.

Step 5 · Use ssh-agent so you type the passphrase once

If your key has a passphrase, ssh-agent holds the decrypted key in memory for the length of your session. Start it and add the key:

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

ssh-add prompts for the passphrase when the key has one. ssh-add -l lists the keys the agent holds, with fingerprints. On macOS, ssh-add --apple-use-keychain ~/.ssh/id_ed25519 also stores the passphrase in the Keychain. Most Linux desktops start an agent for you.

Next steps

You can now log in without a password. The final step of that work is to switch passwords off, so the keys are the only way in.

  • Turn off password and root logins in /tutorials/harden-ssh-on-ubuntu-26-04.
  • Make sure the account you log in with has sudo: /tutorials/create-a-sudo-user-on-ubuntu-26-04.
  • Slow down guessing attempts with /tutorials/block-brute-force-logins-with-fail2ban.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.