Sub topic 6
Getting File Permissions Right
Assume you can use a Linux or macOS command line, understand basic Unix permissions, and can log in to a remote Linux server with SSH. This chapter focuses on the permissions that protect SSH key files and the errors caused by incorrect ownership or access.
SSH checks permissions before it trusts a key. A file that another user can change could let that user add their own public key and log in as you. A private key that other users can read is also unsafe.
Which files need protection
Your key pair is stored on the client computer. The private key proves your identity and must remain secret. The public key is copied to the server, usually into ~/.ssh/authorized_keys.
| File or directory | Typical mode | Purpose |
|---|---|---|
| `~/.ssh` | `700` | Private directory for SSH files |
| `~/.ssh/id_ed25519` | `600` | Secret private key |
| `~/.ssh/id_ed25519.pub` | `644` | Public key |
| `~/.ssh/authorized_keys` | `600` | Public keys accepted for the account |
| `~/.ssh/config` | `600` | Client settings that may contain sensitive details |
A mode is a numeric representation of permissions. The first digit applies to the owner, the second to the group, and the third to everyone else. In 600, the owner can read and write, while the group and other users have no access. In 700, the owner can read, write, and enter the directory.
Set permissions and ownership
Run these commands on the client for your private key and SSH directory:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod 600 ~/.ssh/configThe chmod command changes permission bits. The private key must not be readable by your group or by other users. The public key does not need to be secret, so 644 is normally sufficient.
On the server, run the following commands as the account that will receive SSH logins:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod go-w "$HOME"chmod go-w "$HOME" removes write permission for the group and other users from your home directory. This matters because an SSH server may reject a key when the .ssh path can be altered by someone else.
Check the result with:
ls -ld "$HOME" ~/.ssh
ls -l ~/.ssh/id_ed25519 ~/.ssh/id_ed25519.pub ~/.ssh/authorized_keysThe first column shows permissions. The owner and group columns show who owns each item. The account running SSH should own the private key, .ssh, and authorized_keys.
Check ownership
Permissions alone are not enough. A file with mode 600 can still be unsafe if a different account owns it or controls its parent directory.
On the server, inspect ownership:
stat -c '%A %U:%G %n' "$HOME" ~/.ssh ~/.ssh/authorized_keysThe output should identify your login account as the owner of these paths. If an administrator created the files for you, an administrator can correct ownership:
sudo chown -R alice:alice /home/alice/.sshReplace alice with the actual account name. Do not run this command against another user's directory unless you have confirmed the intended owner.
Find an unsafe parent directory
SSH checks the whole path, not only authorized_keys. A parent directory that other users can write may allow the file to be replaced.
namei -l ~/.ssh/authorized_keysOn systems without namei, inspect each component separately:
ls -ld /home /home/alice /home/alice/.sshThe directory must be searchable, which means it needs execute permission, for its owner. It should not be writable by unrelated users.
Exercise
Exercise
On the server, ls -ld ~/.ssh shows drwxrwxrwx, and authorized_keys shows -rw-r--r--. What commands should you run as the login user to apply the usual restrictive settings?
Show answer
Run chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorized_keys. Then verify ownership and inspect the complete path with ls -ld "$HOME" ~/.ssh and ls -l ~/.ssh/authorized_keys.