chmod 400: Read Only Keys and Downloaded PEM Files
400 is r--------: the owner can read the file, nobody can write it, and no other account has any access. It is the conventional mode for a private key file downloaded from a cloud provider and never edited afterwards.
At a glance
- Octal
- 400, written 0400 in four digit form
- Symbolic
- u=r,g=,o=
- On a file
- -r--------
- On a directory
- dr--------, listable by the owner and impossible to enter
- Owner
- Read only. Cannot modify the file without changing the mode first
- Group
- Nothing
- Others
- Nothing
In detail
400 is the mode people are told to apply to a downloaded key pair, and it is a good instruction. A cloud provider hands you a PEM file once, you will never legitimately edit it, and SSH will refuse to use it if anyone but the owner can read it. Removing the owner's write bit as well means an editor or a careless redirect cannot damage the only copy you have.
The difference from 600 is only that write bit, and it decides how the file is meant to be used. 600 is for a secret that changes: an environment file, a credentials file that a tool rewrites, a token that rotates. 400 is for a secret that is written once and read forever, where any write at all would be a mistake.
SSH is satisfied by either. Its check is that group and others have no access to the private key, so 400 and 600 both pass and 640 or 644 both fail with the unprotected private key warning. Choosing 400 adds protection against yourself rather than against another account.
The friction appears at rotation time. Replacing a 400 key means changing the mode first or removing the file and writing a new one, and scripts that expect to overwrite it in place will fail. That is a reasonable trade for a file whose whole security value is that it is never modified, but it should be a deliberate choice rather than a surprise.
As always, this is not encryption. Root reads it, your own account reads it, and any program running as you reads it. If the key needs to survive a stolen laptop, protect it with a passphrase when it is generated, or keep it in an agent or a hardware token rather than relying on a mode.
Examples
chmod 400 ~/keys/prod-server.pem
chmod u=r,g=,o= ~/keys/prod-server.pemmv ~/Downloads/prod-server.pem ~/keys/
chmod 400 ~/keys/prod-server.pem
ssh -i ~/keys/prod-server.pem user@host
Without the chmod, SSH refuses the key because the browser
saved it at 644 under the default umask.chmod u+w ~/keys/prod-server.pem
cp new-key.pem ~/keys/prod-server.pem
chmod 400 ~/keys/prod-server.pem
# or simply
rm -f ~/keys/prod-server.pem
install -m 400 new-key.pem ~/keys/prod-server.pemThings that catch people out
- The directory matters too. A 400 key inside a group writable directory can be replaced wholesale, so keep the containing directory at 700.
- Scripts that write a key file in place fail against 400 with a permission denied even when run as the owner, because the owner has no write bit.
- 400 and 600 are equally acceptable to SSH. Arguments about which is correct are usually about protecting the file from yourself, not about what the client checks.
- Copying the key to another machine with scp does not carry the mode. Set it again on arrival, or the first connection attempt from the new machine fails.
Try it: Chmod Calculator
Tick the nine permission boxes, or type an octal or symbolic value, and get the three digit and four digit octal, the ls -l rendering and the exact chmod command back.
Chmod Calculator
LOCAL · Browser-OnlyCalculate Unix file permissions with an interactive chmod builder
Permission Matrix
| Entity | Read (4) | Write (2) | Execute (1) | Octal | Symbolic |
|---|---|---|---|---|---|
| Owner (u) | 7 | rwx | |||
| Group (g) | 5 | r-x | |||
| Others (o) | 5 | r-x |
Special Bits
Advanced permission flags for executables and directories
Generated Permissions
755rwxr-xr-x-rwxr-xr-xchmod 755 filenamechmod u=rwx,g=rx,o=rx filenamePermission Breakdown
Owner (u)
7rwx- - Read files / list directory contents
- - Write / modify / delete
- - Execute / traverse directory
Group (g)
5r-x- - Read files / list directory contents
- - Execute / traverse directory
Others (o)
5r-x- - Read files / list directory contents
- - Execute / traverse directory
Common Permissions Reference
| Octal | Symbolic | Meaning |
|---|---|---|
777 | rwxrwxrwx | Full access for everyone |
755 | rwxr-xr-x | Owner full, others read and execute |
750 | rwxr-x--- | Owner full, group read and execute |
700 | rwx------ | Owner full access only |
666 | rw-rw-rw- | Read and write for everyone |
644 | rw-r--r-- | Owner read/write, others read only |
600 | rw------- | Owner read/write only |
555 | r-xr-xr-x | Read and execute for everyone |
444 | r--r--r-- | Read only for everyone |
400 | r-------- | Owner read only |
Related File permissions
All file permissions →chmod 600: Owner Only Read and Write for Keys and Secrets
600 gives the owner read and write and nobody else anything. The mode SSH, PostgreSQL and other tools insist on for files holding credentials.
chmod 440: Read Only for Owner and Group, the sudoers Mode
440 gives the owner and group read only and everyone else nothing. Why sudo refuses to run unless its configuration files carry exactly this mode.
chmod 700: Private Directories Such as ~/.ssh
700 gives the owner read, write and execute and everyone else nothing. Why SSH insists on it for ~/.ssh and what breaks when a parent is more open.
chmod 444: Read Only for Everyone, With Real Limits
444 gives read to all three classes and write to none, not even the owner. Why a 444 file can still be deleted and what actually makes a file immutable.
chmod 070: Group Only Access and the First Match Rule
070 grants the group everything and the owner nothing. The owner really is denied, because the kernel checks one class only, and this mode proves it.
chmod 007: Everyone Else Gets Everything, the Owner Gets Nothing
007 gives every other account full access while locking out the owner and the group. Almost always a typo for 700, and a lesson in how chmod is authorised.
Tools for this
- Chmod Calculator Free online chmod calculator - calculate Unix file permissions with an interactive chmod builder