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.
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:
apt-get update && apt-get install -y openssh-clientdnf install -y openssh-clientspacman -Sy --noconfirm opensshGenerate 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.
mkdir -p ~/.ssh
ssh-keygen -t ed25519 -C "demo key" -N "" -f ~/.ssh/id_ed25519_demo
ls ~/.sshSet 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.
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519_demo
chmod 644 ~/.ssh/id_ed25519_demo.pub
ls -l ~/.sshInspect the key
Print the fingerprint of the public key with -l, or regenerate the public key from the private one with -y:
ssh-keygen -l -f ~/.ssh/id_ed25519_demo.pub
ssh-keygen -y -f ~/.ssh/id_ed25519_demoTo 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:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_demo
ssh-add -l
ssh-add -L
ssh-agent -kssh-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_ed25519Add 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 yesIf 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
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.