Sub topic 6
Getting File Permissions Right
This chapter assumes you can use a Linux or macOS shell, read ls -l output, and have logged in to a server with a password using ssh. You should also know what a public/private key pair is. The previous chapters covered key generation and passphrases. The next chapter installs the public key on the server.
Why SSH cares about permissions
SSH authenticates you with files. The private key on your laptop proves who you are. The authorized_keys file on the server lists which public keys may log in. If another user could change either file, they could impersonate you or add their own key to your account.
So both programs check permissions before trusting a file:
- The
sshclient refuses to use a private key that other users can read. - The
sshdserver, with its defaultStrictModes yes, ignoresauthorized_keysif your home directory,~/.ssh, or the file itself is writable by anyone other than you or root.
These checks fail quietly on the server side. The client only sees "Permission denied (publickey)" and gets no hint about the cause. The real reason appears only in the server log. Most "my key doesn't work" problems come from permissions, so it pays to set them correctly the first time.
Reading permission modes
A quick review of the notation. Each file has three permission sets: owner (user), group, and others. Each set can include read (r, 4), write (w, 2), and execute (x, 1). The octal mode adds these per set.
| Mode | Symbolic | Meaning |
|---|---|---|
| 700 | `rwx------` | Owner can read, write, enter. No one else has access. |
| 600 | `rw-------` | Owner can read and write. No one else has access. |
| 644 | `rw-r--r--` | Owner reads and writes. Everyone else reads. |
| 755 | `rwxr-xr-x` | Owner has full access. Everyone else reads and enters. |
| 664 | `rw-rw-r--` | Owner and group write. Everyone reads. |
For a directory, x means "may enter and access items inside". Without it, you cannot reach files in the directory even if you can read them.
The required permissions
Here are the rules you need to remember. The first four are files and directories on the server. The last two are on your client.
| Path | Where | Recommended mode | Rule enforced |
|---|---|---|---|
| `~` (home directory) | Server | 755 or 700 | Must not be writable by group or others |
| `~/.ssh` | Server | 700 | Must not be writable by group or others |
| `~/.ssh/authorized_keys` | Server | 600 (644 also accepted) | Must not be writable by group or others |
| `~/.ssh/id_ed25519` (private key) | Client | 600 | Must not be accessible by group or others |
| `~/.ssh/id_ed25519.pub` (public key) | Client | 644 | Not checked, but keep it non-writable by others |
| `~/.ssh` | Client | 700 | Good practice, and some tools complain otherwise |
Use these commands to set them:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/id_ed25519Run the first two on the server, inside your account there. Run the first and third on your client. If a file does not exist yet, chmod reports an error for it. That is harmless at this stage.
The home directory is the rule people forget. A common trap is a home directory with mode 775 or 777, which happens on shared servers or when someone ran chmod -R 777 to fix an unrelated problem. A home directory of 755 is fine, because group and others cannot write to it. A mode of 775 or 777 makes sshd ignore your keys, even when ~/.ssh is perfect.
chmod go-w ~This removes write permission for group and others on your home directory and leaves everything else unchanged.
Ownership matters as much as mode
Permissions are checked together with ownership. sshd requires that ~/.ssh/authorized_keys and its parent directories are owned by the target user or by root. If you created the file as root while setting up an account for someone else, it will be owned by root. In that case the user may fail to log in or, depending on the version and mode, may not be able to maintain the file.
Check ownership with ls -ld and ls -l:
ls -ld ~ ~/.ssh
ls -l ~/.sshA healthy result looks like this:
drwxr-xr-x 6 alice alice 4096 Nov 27 10:12 /home/alice
drwx------ 2 alice alice 4096 Nov 27 10:15 /home/alice/.ssh
total 12
-rw------- 1 alice alice 100 Nov 27 10:15 authorized_keys
-rw------- 1 alice alice 411 Nov 27 10:02 id_ed25519
-rw-r--r-- 1 alice alice 96 Nov 27 10:02 id_ed25519.pubIf you are an administrator preparing the account for someone else, fix ownership recursively:
sudo chown -R alice:alice /home/alice/.sshUse the user's primary group name in place of the second alice if it differs. Check with id alice.
Why `umask 077` appears in the install command
The manual install command in the next chapter begins with umask 077. The umask is a per-process mask that removes permission bits from newly created files and directories. With 077, new directories get mode 700 and new files get mode 600, so group and others get nothing.
You can see the effect directly:
umask 077
mkdir demo_dir
touch demo_file
ls -ld demo_dir demo_fileThe output shows drwx------ for the directory and -rw------- for the file. Running umask 077 inside a one-line remote command affects only that remote shell session, so your normal defaults are unchanged afterwards.
A typical default umask is 022 (Debian, Ubuntu, Fedora) or 002 on some systems that give each user a private group. With 022, a freshly created directory is 755 and a file is 644. That is acceptable for a public key, but not for a private one. ssh-keygen sets mode 600 on the private key itself, so you do not depend on the umask for that step.
What the client does when the private key is too open
If your private key is readable by others, the ssh client prints a loud warning and refuses to use it:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/home/alice/.ssh/id_ed25519' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.The fix is chmod 600 ~/.ssh/id_ed25519. After that, ssh uses the key again. If you copied the key from a USB drive formatted as FAT or exFAT, or from a Windows share, the file will often show up as 755 or 777, because those file systems have no Unix permissions. Always re-run chmod 600 after copying a key.
What the server does when permissions are wrong
The server does not warn you. It simply skips authorized_keys, and the client falls through to the next authentication method. With passwords disabled, you see:
[email protected]: Permission denied (publickey).To find the cause, read the server log. On systemd-based systems, you can use journalctl. The unit is ssh on Debian and Ubuntu, and sshd on most other distributions.
sudo journalctl -u ssh -n 20 --no-pagerOn older systems or those that log to files, look at /var/log/auth.log (Debian, Ubuntu) or /var/log/secure (RHEL, Fedora, and relatives).
The relevant messages look like this:
sshd[2210]: Authentication refused: bad ownership or modes for directory /home/alice
sshd[2210]: Authentication refused: bad ownership or modes for file /home/alice/.ssh/authorized_keysThe message names the exact path that failed the check. Fix that path and try again. You do not need to restart sshd, because it reads these files on every login.
A one-pass audit script
If you want to check every relevant path at once, namei is a useful tool. With -l, it prints the owner, group, and mode for each component of a path, from / down to the file:
namei -l ~/.ssh/authorized_keysExample output:
f: /home/alice/.ssh/authorized_keys
dr-xr-xr-x root root /
drwxr-xr-x root root home
drwxrwxr-x alice alice alice
drwx------ alice alice .ssh
-rw------- alice alice authorized_keysHere the home directory alice is mode 775, so it is group-writable. This is the cause of a silent failure. Running chmod go-w ~ fixes it. This single command shows problems in ancestor directories that are easy to miss. StrictModes checks every directory in the path of the home directory too, though in practice system directories such as / and /home are owned by root and already safe.
Special cases and edge cases
- Group-writable home directories on purpose. Some environments use shared group directories. If you need this, you can set
StrictModes noinsshd_config, but that removes a safety check for every user on the machine. Prefer fixing the permissions instead. - Encrypted or network home directories. With NFS, or with eCryptfs-encrypted home directories, the server may not be able to read
authorized_keysbefore you log in. A common workaround is to setAuthorizedKeysFileinsshd_configto a path outside the home directory, such as/etc/ssh/authorized_keys/%u. Those files must be owned by root and not writable by others. - SELinux. On RHEL, Fedora, and similar systems,
authorized_keysalso needs the right SELinux label,ssh_home_t. If you create the file by copying it from somewhere else, the label may be wrong. Symptoms are the same as for permission errors, but the log mentions SELinux denials in/var/log/audit/audit.log. The commandrestorecon -R -v ~/.sshresets the labels to the policy defaults. - Symbolic links. If
~/.sshis a symlink, for example to a dotfiles repository, the checks apply to the target path. Make sure the target has the right owner and mode. - Root account. The same rules apply to
/root/.ssh. Root's home directory is 700 by default on most systems, which satisfies the check.
Verifying the whole setup
After you set permissions, confirm with a forced key-only login. This is the same command from the troubleshooting chapter, shown here as a quick check:
ssh -v -o PreferredAuthentications=publickey -o PasswordAuthentication=no [email protected]In the verbose output, a line such as Offering public key: /home/alice/.ssh/id_ed25519 means the client found and is using your key. If it is followed by Authentication succeeded (publickey), the server accepted it. If the client offers the key and then gets Permission denied, the problem is on the server, so read its log.
Summary of common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Client prints "UNPROTECTED PRIVATE KEY FILE" | Private key mode is 644, 755, or similar | `chmod 600 ~/.ssh/id_ed25519` |
| Server log: "bad ownership or modes for directory /home/alice" | Home directory is group- or world-writable | `chmod go-w ~` |
| Server log: "bad ownership or modes for file ... authorized_keys" | File is writable by others or owned by another user | `chmod 600` and `chown alice:alice` |
| Works as root, fails as the user | File created by root, so ownership is wrong | `sudo chown -R alice:alice /home/alice/.ssh` |
| Permissions look right, still denied on RHEL or Fedora | Wrong SELinux label | `restorecon -R -v ~/.ssh` |
Exercise
A colleague's key login fails with "Permission denied (publickey)". On the server, ls -ld ~/.ssh ~/.ssh/authorized_keys shows drwx------ and -rw-------, both owned by the right user, and the public key is definitely listed in the file. The server log says "Authentication refused: bad ownership or modes for directory /home/bob". What is wrong, and what single command fixes it?
Show answer
The problem is the home directory itself, not .ssh. StrictModes requires that /home/bob is not writable by group or others, and it is probably mode 775 or 777. Running chmod go-w /home/bob (or chmod go-w ~ as bob) removes the extra write bits. The next login attempt works, and sshd does not need to be reloaded.
Exercise
You copy your private key id_ed25519 from a USB stick formatted as exFAT to ~/.ssh/ on your Linux laptop. When you run ssh, you get a warning that the key file is too open and ssh ignores it. Which command fixes it, and why did the permissions change?
Show answer
Run chmod 600 ~/.ssh/id_ed25519. File systems such as exFAT and FAT have no Unix permission bits, so the copy appears with a broad default mode such as 755 or 777. The ssh client refuses private keys that group or others can read, so you must reset the mode after copying.