How to generate SSH keys in Linux for server logins

Create SSH keys on Ubuntu, Debian, Fedora or Rocky Linux with ssh-keygen, protect them with a passphrase and use them to log in to your servers.

6–9 minutes
Desk with a monitor showing code and small plants

To generate SSHSSH A secure way to type commands on a server from your own computer, as if you were sitting at it. More about SSH → keys in Linux you run one command, ssh-keygen, in a terminal and answer two questions. This guide is for anyone on Ubuntu, Debian, Fedora, AlmaLinux, Rocky Linux or a Raspberry Pi who wants a key to log in to their own servers, and wants to understand each step, including installing the tools, naming several keys and fixing the usual errors.

Open a terminal and run ssh-keygen -t ed25519 -C “exampleco-laptop”, press Enter to accept the default location, then type a passphrase twice. Your new key pair is saved as ~/.ssh/id_ed25519 (the private key, never share it) and ~/.ssh/id_ed25519.pub (the public key, safe to share). Install the public key on a server with ssh-copy-id, then log in without the account password.

If the idea of a key pair is new to you, read what SSH keys are and how they work first. The short version: the private key stays on your computer and proves who you are, and the public key goes on every server that should let you in.

Check that ssh-keygen is installed

ssh-keygen comes with the OpenSSH client, which almost every desktop Linux distribution installs by default. Check with:

ssh -V

Success looks like a version line such as OpenSSH_9.6p1 Ubuntu-3ubuntu13. The exact numbers do not matter. If you see command not found, install the client package. On Ubuntu and Debian:

sudo apt update
sudo apt install openssh-client

On Fedora, AlmaLinux and Rocky Linux the package name ends in an s:

sudo dnf install openssh-clients

You only need the client to create keys. The server package (openssh-server) is what runs on the machine you log in to, not the one you log in from.

Generate the key pair

1. Run ssh-keygen

ssh-keygen -t ed25519 -C "exampleco-laptop"

What each part means:

  • -t ed25519 chooses the key type. Ed25519 keys are short, fast and secure, and every current SSH server accepts them. Recent OpenSSH releases already use Ed25519 by default, but naming the type explicitly gives the same result on older systems too.
  • -C "exampleco-laptop" adds a comment to the end of the public key. It has no security role. It is a label so that you, or whoever manages the server, can tell later which computer a key belongs to. A machine name or an email address both work well.

2. Accept the default location

You will see:

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

Press Enter. Keeping the default name means ssh will find and use the key automatically, with no extra settings. If ssh-keygen warns that the file already exists and asks Overwrite (y/n)?, answer n. Overwriting destroys your existing private key, and every server that trusts it will stop letting you in. Instead, run the command again with a different file name, as shown further down.

3. Set a passphrase

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

A passphrase encrypts the private key file on your disk. If someone copies the file from a stolen laptop or a backup, it is useless to them without the passphrase. Nothing appears while you type, which is normal.

Use a passphrase on any key for a laptop or desktop. The only common reason to leave it empty is a key used by an automated job, such as a backup script, and that key should be limited to the one task it performs. The ssh-agent step below means a passphrase does not have to slow you down.

4. Check what was created

ssh-keygen prints the key’s fingerprint (a short, unique summary of the key) and a small picture called the randomart. Neither needs to be saved. List your files:

ls -l ~/.ssh

You should see:

FileWhat it isPermissions
id_ed25519Private key. Never copy it to a server or share it.-rw——- (600)
id_ed25519.pubPublic key, one line starting ssh-ed25519-rw-r–r– (644)

The ~/.ssh folder itself should be drwx------ (700). ssh-keygen sets these permissions for you. For more on every file in that folder, see where SSH keys are stored.

To look at the public key:

cat ~/.ssh/id_ed25519.pub

Options you may need

Make a second key with a different name

You can keep several key pairs, for example one for work servers and one for personal projects. Use -f to choose the file name:

ssh-keygen -t ed25519 -C "exampleco-work" -f ~/.ssh/id_ed25519_work

Because ssh only tries the default names on its own, tell it which key to use, either on the command line with ssh -i ~/.ssh/id_ed25519_work deploy@203.0.113.10 or in ~/.ssh/config:

Host work-server
    HostName 203.0.113.10
    User deploy
    IdentityFile ~/.ssh/id_ed25519_work

After that, ssh work-server uses the right key automatically.

Create an RSA key for older systems

A few old servers, network devices and some hosting control panelsControl panel A website you log into to manage your hosting with buttons and forms, instead of typing commands. cPanel and Plesk are the common ones. More about Control panel → still only accept RSA keys. For those, create a 4096-bit RSA key:

ssh-keygen -t rsa -b 4096 -C "exampleco-laptop"

It is saved as ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub, so it sits alongside an Ed25519 key without overwriting it.

Add or change a passphrase later

If you created a key without a passphrase, or want to change it, you do not need a new key:

ssh-keygen -p -f ~/.ssh/id_ed25519

It asks for the old passphrase (just press Enter if there was none) and then the new one. The public key does not change, so servers keep trusting it.

Recreate a lost public key

If you still have the private key but the .pub file has gone, rebuild it:

ssh-keygen -y -f ~/.ssh/id_ed25519 > ~/.ssh/id_ed25519.pub

Load the key into ssh-agent

ssh-agent is a small background program that holds your unlocked key in memory, so you type the passphrase once per session instead of on every connection. Most desktop Linux sessions (GNOME and KDE, for example) start an agent for you. Add your key with:

ssh-add ~/.ssh/id_ed25519

If you get Could not open a connection to your authentication agent, no agent is running in this terminal, which is common over SSH or on a minimal install. Start one first, then add the key:

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

Check what the agent is holding with ssh-add -l.

Put the public key on a server and test it

The easiest way to install your public key on a Linux server is ssh-copy-id, which logs in with your password once and appends the key to ~/.ssh/authorized_keys on the server:

ssh-copy-id -i ~/.ssh/id_ed25519.pub exampleco@203.0.113.10

Then log in:

ssh exampleco@203.0.113.10

Success means you are asked for your key’s passphrase (or nothing at all, if ssh-agent has it) instead of the account password. If you need to paste the key into a web page or a control panel instead, the guide to copying SSH keys covers each method.

Keep a working way in, such as your password or a hosting console, until you have confirmed that the key login works from a new terminal window. Only then think about turning off password logins on the server.

Common problems

SymptomCauseFix
ssh: command not foundOpenSSH client not installedInstall openssh-client (Debian, Ubuntu) or openssh-clients (Fedora, AlmaLinux, Rocky)
Overwrite (y/n)?A key with that name already existsAnswer n and use -f to pick a new file name
Permission denied (publickey)The server does not have your public key, or ssh is offering a different keyRun ssh-copy-id again, or point ssh at the right key with -i or IdentityFile
UNPROTECTED PRIVATE KEY FILE warningThe private key is readable by other usersRun chmod 600 ~/.ssh/id_ed25519 and chmod 700 ~/.ssh
Asked for the passphrase on every connectionThe key is not loaded into ssh-agentRun ssh-add ~/.ssh/id_ed25519
Could not open a connection to your authentication agentNo agent running in this shellRun eval “$(ssh-agent -s)” and then ssh-add
Server rejects an Ed25519 keyVery old SSH server or a device that only supports RSACreate an RSA key with -t rsa -b 4096

Official documentation

Get the next guide

One email when there is something worth reading: new guides, errors going around, and hosting changes that affect your site.

No tracking pixels, and one click to leave.

Something out of date? Software changes. If a step no longer works, tell us and we will check it and update the page.