Authorized keys and permissions
Where the host stores your public key, the file permissions it insists on, and why a key login can fail.
For key login the host must hold your public key. It looks in ~/.ssh/authorized_keys of the account you log in as.
Adding a key
ssh-copy-id -i ~/.ssh/id_ed25519.pub jsmith@host
If the server is only reachable through a bastion, see ssh-copy-id through a jump host (ProxyJump).
Or paste the public key line into the file by hand. One key per line. Lines starting with # are comments.
Permissions
The host refuses keys if the files are open to others (StrictModes yes is the default):
chmod 700 ~
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
The home folder must not be writable by anyone but you. The same applies to the owner: the files must belong to the user. These rules are for the host's files. On your own computer, ssh also ignores a private key that others can read; see SSH "WARNING: UNPROTECTED PRIVATE KEY FILE!" and bad permissions.
Options on a key line
from="10.0.0.0/8",no-port-forwarding ssh-ed25519 AAAA... jsmith@laptop
command="/usr/bin/backup",restrict ssh-ed25519 AAAA... backup
from= limits where the key may log in from. command= forces one command. restrict turns off forwarding, agent, PTY and more.
"Permission denied (publickey)"
This is the host saying no key worked. Common causes:
- The public key is not on the host or is in the wrong account.
- Permissions are too open (see above).
- The key type is not accepted (an RSA key with a SHA-1 signature, an old DSA key, or a type the host does not allow in
PubkeyAcceptedAlgorithms). - The client offered the wrong key. Select the one that matches.
- The account is locked or its shell is not allowed. On IBM i, also check the home directory, the key file owner and the PASE shell; see IBM i SSH "Permission denied": home, key permissions, PASE shell.
The host's log tells the real reason: /var/log/auth.log (Debian, Ubuntu) or /var/log/secure (Red Hat), or journalctl -u ssh / journalctl -u sshd. On the client, ssh -v prints every step.
Related
- Key types
- Certificates
- SSH problems
- SSH "WARNING: UNPROTECTED PRIVATE KEY FILE!" and bad permissions
- ssh-copy-id through a jump host (ProxyJump)
- IBM i SSH "Permission denied": home, key permissions, PASE shell
- SFTP chroot user gets "Permission denied (publickey)" with the right key
- PuTTY "Server refused our key": why and how to fix it
- PuTTY "No supported authentication methods available (server sent: publickey)"
- ssh-copy-id "ERROR: No identities found" and "All keys were skipped"
- Windows OpenSSH server asks for a password for an admin user: administrators_authorized_keys