How do I generate an SSH key and put it on a server?
Run ssh-keygen -t ed25519, then ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host. The private key stays on your computer, the public key goes into ~/.ssh/authorized_keys on the server, and you log in without a password.
Last updated
Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b
container image), .
An SSH key is a pair of files. The private key stays on your computer. The public key goes on every server you want to log in to. Create the pair with ssh-keygen -t ed25519, install the public half with ssh-copy-id, and the server lets you in because you can prove you hold the private half. No password crosses the network.
Look for an existing key
Look for the default file names before you make a new key. ssh-keygen asks before it overwrites a file, but a key you replace by mistake is gone:
$ ls ~/.ssh/id_*
ls: cannot access '/home/dev/.ssh/id_*': No such file or directory
No id_ed25519 and no id_rsa, so this machine has no default key yet.
Generate the key
$ ssh-keygen -t ed25519 -C "dev@laptop" -f ~/.ssh/id_ed25519 -N '' -q
-t ed25519 picks the key type. -C writes a comment at the end of the public key, so you can tell later whose key a line is. -f is the file name. -N '' sets an empty passphrase and -q keeps the command quiet. Both are there only because this check runs without a person at the keyboard. On your laptop leave them out: ssh-keygen then asks for a passphrase twice and prints the fingerprint of the new key. Use a passphrase. It encrypts the private key file, so a stolen copy of the file does nothing on its own. To add one to an existing key, run ssh-keygen -p -f ~/.ssh/id_ed25519.
Two files now exist, and their modes matter:
$ stat -c '%a %n' ~/.ssh/id_ed25519 ~/.ssh/id_ed25519.pub
600 /home/dev/.ssh/id_ed25519
644 /home/dev/.ssh/id_ed25519.pub
The private key is 600, readable by you alone. ssh refuses to use a private key that other users can read. The .pub file can be public. You can paste it into a form, a chat or a ticket:
$ cut -d' ' -f1,3 ~/.ssh/id_ed25519.pub
ssh-ed25519 dev@laptop
I cut out the middle field, the key itself, to keep the line short. The first field is the type and the last is your comment. To see the size and the type of a key, ask ssh-keygen -l:
$ ssh-keygen -lf ~/.ssh/id_ed25519.pub | awk '{print $1, $3, $4}'
256 dev@laptop (ED25519)
The field I left out is the fingerprint, SHA256: and a short string. It is how a key is named in dialogs and logs, and it changes with every new key.
Copy the public key to the server
ssh-copy-id logs in once some other way, with a password or an older key, and appends your public key to ~/.ssh/authorized_keys on the server. It creates the folder and the file if they are missing.
$ ssh-copy-id -i ~/.ssh/id_ed25519.pub app
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/dev/.ssh/id_ed25519.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'app'"
and check to make sure that only the key(s) you wanted were added.
On a server where you have no key yet, the line “if you are prompted now” is where ssh asks for the account’s password, once. In this run an older key from ssh-agent already let ssh in, so nothing was asked. The alias app comes from ~/.ssh/config; see the config file article.
Now test the new key alone. -F /dev/null ignores your config file and IdentitiesOnly=yes stops ssh from trying anything but the key you name:
$ ssh -F /dev/null -i ~/.ssh/id_ed25519 -o IdentitiesOnly=yes deploy@app.example.test 'whoami; hostname'
deploy
app
It works. Check every new key like this, in a second window. Only when the check passes is it safe to switch passwords off, as the next article shows.
What the server stores
The file authorized_keys holds one public key per line. After the copy it has two lines, the key that was already there and the new one. The modes on the server look like this:
$ ssh app 'stat -c "%a %U %n" ~ ~/.ssh ~/.ssh/authorized_keys'
750 deploy /home/deploy
700 deploy /home/deploy/.ssh
600 deploy /home/deploy/.ssh/authorized_keys
$ ssh app 'cut -d" " -f1,3 ~/.ssh/authorized_keys'
ssh-ed25519 valakes-learn-test-key
ssh-ed25519 dev@laptop
The comment is the only thing that tells the lines apart, which is why -C is worth using.
When the server ignores your key
The most common failure is a mode that is too open. By default sshd has StrictModes yes. It checks the owner and the modes of the user’s home directory, of ~/.ssh and of authorized_keys, and it ignores keys in files that other users can write to. Here the server has a second account, broken, whose authorized_keys is world-writable. The right key is in it, and the login still fails:
$ ssh -i ~/.ssh/old_key -o IdentitiesOnly=yes broken@app.example.test true
broken@app.example.test: Permission denied (publickey).
The client gets no reason, on purpose. The reason is in the server’s log:
$ ssh app 'grep -o "Authentication refused.*" /var/log/sshd.log'
Authentication refused: bad ownership or modes for file /home/broken/.ssh/authorized_keys
On most servers this line is in /var/log/auth.log or in journalctl -u ssh. Look at the mode, set it to 600, and try again:
$ ssh app 'sudo stat -c "%a %U %n" /home/broken/.ssh/authorized_keys'
666 broken /home/broken/.ssh/authorized_keys
$ ssh app 'sudo chmod 600 /home/broken/.ssh/authorized_keys'
$ ssh -i ~/.ssh/old_key -o IdentitiesOnly=yes broken@app.example.test whoami
broken
The same check covers ~/.ssh (use 700) and the home directory, which other users must not be able to write to. Running chmod -R 777 on a home directory is a classic way to lock yourself out. The permissions article explains the numbers.
Mistakes people make
You copy the private key to the server. Only the .pub file goes there. If a private key ever left your computer, treat it as stolen: remove its public half from every authorized_keys and make a new pair.
You skip the passphrase on a laptop. A key without a passphrase is a password written in a file. Anyone who copies the file can log in as you.
You turn off password login before the key works. Test the key in a second window first, as above. The lockout steps are in the password login article.
You hold several keys in your agent and install the wrong one. Name the file with -i, as above, and read the “Number of key(s) added” line.
You give a key more power than a login needs. A key decides who gets in, and nothing about what they can do afterwards. Limit that with a sudo rule for one command and with care around deletes, as in safe rm on servers.
Valakes keeps the passwords and key passphrases you save in an encrypted vault that your operating system protects, and it can answer a program's password prompt from the vault without showing the value. Feature: Password auto-fill
Questions
Should I use ed25519 or RSA?
Use ed25519. The keys are short and fast, and every OpenSSH since 6.5 understands them. Use ssh-keygen -t rsa -b 4096 only for an old server or device that does not know ed25519.
Do I need a passphrase?
Yes, on a laptop. The passphrase encrypts the private key file, so a copy of the file is useless without it. ssh-agent keeps the unlocked key in memory, so you type the passphrase once per session instead of once per connection.
Can I use one key for every server?
Yes. The public key can sit on any number of servers and the private key never leaves your computer. If you want to revoke access for one laptop without touching the others, give each laptop its own key.
How do I remove a key from a server?
Open ~/.ssh/authorized_keys on the server and delete the line of that key. Each key is one line, and the comment at the end of the line, such as dev@laptop, tells you whose it is.
What if ssh-copy-id is not installed?
Append the one line of your .pub file to ~/.ssh/authorized_keys on the server yourself. Create ~/.ssh with mode 700 and the file with mode 600 if they do not exist.
References
- ssh-keygen(1) on Ubuntu 24.04 manpages.ubuntu.com
- ssh-copy-id(1) on Ubuntu 24.04 manpages.ubuntu.com
- sshd(8) at man7.org, the authorized_keys file and its permissions man7.org
- sshd_config(5) at man7.org, StrictModes man7.org
- OpenSSH manual pages www.openssh.org
Related articles
The app is free.
Get notified at launchmacOS is coming to the Mac App Store. iOS and Android later.