# How do I generate an SSH key and put it on a server?

Generate an ed25519 SSH key, install it with ssh-copy-id and fix the permissions that make sshd ignore it. Real commands, checked on Ubuntu 24.04.

Source: https://valakes.com/learn/ssh-keys/

Last updated: 2026-11-25

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.

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), 25 November 2026.

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:

```console
$ 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

```console
$ 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:

```console
$ 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:

```console
$ 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`:

```console
$ 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.

```console
$ 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](https://valakes.com/learn/ssh-config-file/).

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:

```console
$ 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](https://valakes.com/learn/disable-ssh-password-login/) 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:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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](https://valakes.com/learn/file-permissions/) 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](https://valakes.com/learn/disable-ssh-password-login/).

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](https://valakes.com/learn/sudo-for-one-command/) and with care around deletes, as in [safe rm on servers](https://valakes.com/learn/safe-rm-on-servers/).

## In Valakes

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](https://valakes.com/features/password-auto-fill/)

[Read the docs: Credentials and auto-fill](https://docs.valakes.com/credentials/)

## 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](https://manpages.ubuntu.com/manpages/noble/man1/ssh-keygen.1.html)
- [ssh-copy-id(1) on Ubuntu 24.04](https://manpages.ubuntu.com/manpages/noble/man1/ssh-copy-id.1.html)
- [sshd(8) at man7.org, the authorized_keys file and its permissions](https://man7.org/linux/man-pages/man8/sshd.8.html)
- [sshd_config(5) at man7.org, StrictModes](https://man7.org/linux/man-pages/man5/sshd_config.5.html)
- [OpenSSH manual pages](https://www.openssh.org/manual.html)

## Related articles

- [How do I disable SSH password login safely?](https://valakes.com/learn/disable-ssh-password-login/): Put PasswordAuthentication no in a file in /etc/ssh/sshd_config.d/ whose name sorts first, run sudo sshd -t, then reload ssh. Keep your current session open and test a new login in a second window before you close it.
- [How do I use rm safely on a server?](https://valakes.com/learn/safe-rm-on-servers/): List what a command will remove before you run it, with ls, find or echo. Use rm -I so that rm asks once before a recursive delete, and write variables as "${dir:?}" so that an empty one stops the command. The built-in --preserve-root protects only the path /.
- [How do I let a user run one command with sudo?](https://valakes.com/learn/sudo-for-one-command/): Write a rule that names the command with its full path and exact arguments, put it in its own file under /etc/sudoers.d/, check it with visudo -cf, and install it with mode 440. Do not use wildcards in the arguments, and do not allow editors or any program that can start a shell.
- [SSH config file (~/.ssh/config): examples that work](https://valakes.com/learn/ssh-config-file/): The file ~/.ssh/config stores the address, user, port and key of each server under a short name. After that, ssh app does the work of a long command.
