chmod 400 (r--------)

Only the owner can read the file and nobody can write to it, the owner included. It is the mode cloud providers tell you to put on a private key you have just downloaded.

Owner
Group
Others
Symbolic
Command

This tool runs in your browser. We never send what you type or paste to a server.

What chmod 400 sets

WhoDigitReadWriteExecuteSymbolic
Owner (u)4yesnonor--
Group (g)0nonono---
Others (o)0nonono---
chmod 400 file      # numeric
chmod u=r,go= file  # the same, in symbolic form

ls -l shows this mode as -r-------- on a file and dr-------- on a directory. On a file, execute means it can be run as a program. On a directory, read lists the names inside, write creates, renames and deletes entries, and execute (search) is what lets you enter it and reach the files inside.

What 400 is for

Use it on a secret that is written once and only read afterwards. The usual case is a private key: the AWS EC2 instructions have you run chmod 400 on the downloaded .pem file before the first ssh -i, because OpenSSH ignores a private key that the group or other users can access.

OpenSSH does not care whether the owner can write. It accepts 400 and 600 alike and rejects anything with a group or others bit set. The only thing 400 adds over 600 is that you cannot overwrite the key by accident.

The same reasoning fits an API token file, a backup passphrase or a licence key: nothing should ever modify them, and nobody else should see them.

What the missing write bit does not stop

400 guards against accidents, not against you.

  • You can put the write bit back. Changing a mode needs ownership of the file, not write permission on it. chmod u+w works at any time.
  • You can delete or replace it. Removing and renaming are decided by the directory, not the file. If you can write to the directory, rm asks for confirmation on a write-protected file (when run from a terminal, without -f) and then removes it, and mv will put another file in its place.
  • Root ignores it. The superuser can read and write the file whatever its mode.

What does fail is writing into the file as an ordinary user: a redirect such as echo test > key.pem gets "Permission denied", and an editor will refuse or warn before saving.

400 on a directory

Not useful. With read but no execute, you can list the names in your own directory but cannot enter it, open anything in it or see the details of the files; ls -l reports errors for every entry. A directory you want to freeze needs at least 500 (r-x------), and one you want private and usable needs 700.

Commands

chmod 400 ~/.ssh/aws-key.pem              # lock a downloaded key
ssh -i ~/.ssh/aws-key.pem user@host
stat -c '%a %n' ~/.ssh/aws-key.pem        # GNU/Linux: prints the mode and the name
stat -f '%Lp %N' ~/.ssh/aws-key.pem       # macOS and BSD
chmod u+w ~/.ssh/aws-key.pem              # back to 600 when the file has to change

Keep the key on a file system that stores Unix permissions. On a FAT or exFAT stick, or a Windows drive mounted into Linux, chmod either fails or changes nothing, and ssh keeps rejecting the key. Copy it into your home directory first.

400 compared with its neighbours

ModeSymbolicDifference from 400
600rw-------The owner can also write: for keys and config files you edit
444r--r--r--Everyone can read: read-only, but not secret
700rwx------The private mode for a directory, such as ~/.ssh itself

Questions

Should an SSH private key be chmod 400 or 600?

Either. OpenSSH only requires that the key is not accessible by the group or by others, and both modes satisfy that. 400 also stops you overwriting the key by mistake; 600 is more convenient if a tool ever has to rewrite the file, for example to change its passphrase.

How do I edit or delete a file that is chmod 400?

As the owner, run chmod u+w on it (or chmod 600) and then edit it. To delete it you do not need to change anything: rm asks you to confirm because the file is write-protected, and removes it as long as you have write permission on the directory.

Can other users or root read a chmod 400 file?

Other ordinary users cannot. Root can, because the superuser bypasses read and write permission checks, and so can anyone able to run commands as the owner. The mode protects a secret from other accounts on the machine, not from its administrators.