SSH Keys on Linux: Generate an Ed25519 Key and Use ssh-agent

Create an Ed25519 SSH key pair with ssh-keygen, set safe permissions on ~/.ssh, and load the key into ssh-agent so you type your passphrase once.

Guides Published 4 min read

Contents

An SSH key pair lets you log in to servers without sending a password. This guide shows how to generate an Ed25519 SSH key pair on Linux with ssh-keygen, lock down the permissions on ~/.ssh, and load the key into ssh-agent so you enter the passphrase once per session.

Install the OpenSSH client

If ssh-keygen is missing, install the OpenSSH client:

✓ tested · debian,ubuntu
apt-get update && apt-get install -y openssh-client
✓ tested · fedora
dnf install -y openssh-clients
✓ tested · arch
pacman -Sy --noconfirm openssh

Generate an Ed25519 SSH key pair

According to the ssh-keygen manual, Ed25519 is the default key type, so -t ed25519 is mostly there to be explicit. The -C option sets the comment, which otherwise defaults to user@host.

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

Accept the default file name and choose a passphrase when prompted. The manual lists ~/.ssh/id_ed25519 as the private key file and ~/.ssh/id_ed25519.pub as its public half. Use a real passphrase: it protects the private key if the file is ever copied. The -a option sets the number of key-derivation rounds applied to the passphrase; the manual lists 32 as the default.

The next block is the non-interactive form used for testing. It writes a separate demo key with an empty passphrase, which you should not do for a key you rely on.

✓ tested · debian,ubuntu,fedora,arch
mkdir -p ~/.ssh
ssh-keygen -t ed25519 -C "demo key" -N "" -f ~/.ssh/id_ed25519_demo
ls ~/.ssh

Set permissions

The sshd manual says ~/.ssh should be accessible only by its owner, and that authorized_keys should be readable and writable only by the owner. Unless StrictModes is set to no, sshd refuses to use authorized_keys if the file, the directory, or your home directory is writable by other users.

✓ tested · debian,ubuntu,fedora,arch
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519_demo
chmod 644 ~/.ssh/id_ed25519_demo.pub
ls -l ~/.ssh

Inspect the key

Print the fingerprint of the public key with -l, or regenerate the public key from the private one with -y:

✓ tested · debian,ubuntu,fedora,arch
ssh-keygen -l -f ~/.ssh/id_ed25519_demo.pub
ssh-keygen -y -f ~/.ssh/id_ed25519_demo

To change the passphrase later, run ssh-keygen -p -f ~/.ssh/id_ed25519.

Load the key into ssh-agent

ssh-agent holds your private keys and answers authentication requests for ssh. The manual states that passphrases and private keys never go over the network. The agent prints shell commands that set SSH_AUTH_SOCK (the socket path) and SSH_AGENT_PID; eval applies them to your current shell. ssh-add needs SSH_AUTH_SOCK to be set, so run the steps in the same shell:

✓ tested · debian,ubuntu,fedora,arch
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_demo
ssh-add -l
ssh-add -L
ssh-agent -k

ssh-add -l lists fingerprints of loaded keys and -L shows the public key parameters. ssh-agent -k stops the agent that SSH_AGENT_PID points to. Without a file argument, ssh-add tries the default key names, including ~/.ssh/id_ed25519.

If SSH_AUTH_SOCK already points to a running agent in your shell, skip the eval line and just run ssh-add.

Limit how long keys stay loaded

Keys stay in the agent indefinitely unless you set a lifetime. Use -t with ssh-add (seconds or sshd_config time format) to cap one key, or ssh-agent -t to set a default for all keys. Remove everything with ssh-add -D.

ssh-add -t 4h ~/.ssh/id_ed25519

Add keys automatically

In ~/.ssh/config, AddKeysToAgent yes makes ssh add a key loaded from a file to the running agent. The default is no. Other accepted values are ask, confirm, or a time interval after which the key is removed.

Host *
    AddKeysToAgent yes

If your agent holds many keys, IdentitiesOnly yes together with an IdentityFile line makes ssh offer only the keys named in the configuration.

Install the public key on a server

Append only the contents of the .pub file to the server’s ~/.ssh/authorized_keys. Never share the private key.

To see which ports a server listens on, including SSH, read ss: list listening ports. For network details on your own machine, see the ip command guide.

Sources

  1. ssh-keygen(1) manual page (OpenBSD)
  2. ssh-add(1) manual page (OpenBSD)
  3. ssh-agent(1) manual page (OpenBSD)
  4. ssh_config(5) manual page (OpenBSD)
  5. sshd(8) manual page, FILES section (OpenBSD)

#Ssh #Ssh-Keygen #Ssh-Agent #Ed25519 #Security

This article was drafted with AI assistance, fact-checked against the sources listed above and reviewed by a human editor before publication. Spotted an error? See our corrections policy.