chmod 400: read-only private files, such as .pem keys

Open this example in the tool

Try it in the tool

Chmod Calculator

Translate 755 ↔ rwxr-xr-x ↔ u=rwx,g=rx,o=rx instantly.

chmod 400 is r--------: the owner may read the file, and nothing else is allowed to anyone. It is the read-only private mode.

Octal      400
Symbolic   r--------   (ls -l: -r--------)
Command    chmod 400 file

  owner   4  read
  group   0
  others  0

Why .pem keys are 400

When you download a key pair for a cloud server, the instructions say to run chmod 400 my-key.pem. Two reasons fit that mode:

  1. ssh refuses a private key that others can read. 600 would also satisfy it.
  2. You never edit a key, so removing the write bit costs nothing and prevents an accidental overwrite or a careless script appending to it.

If you see WARNING: UNPROTECTED PRIVATE KEY FILE! followed by Permissions 0644 ... are too open, the file arrived with a broader mode, for example from a download folder, a shared drive or an archive. chmod 400 key.pem fixes it, and chmod 700 ~/.ssh handles the folder if you keep keys there (700).

400 against 600

400 600
Owner can read yes yes
Owner can write no yes
Good for keys, certificates, files you never edit .env, config, secrets you update
Editing chmod u+w file first directly

Editors differ in how they deal with a read-only file: some warn, some replace it by writing a new file in the same folder, which works when the folder is writable. If a tool needs to rewrite the file (a certificate renewal, a password file), 400 gets in its way, and 600 is the practical choice.

Things 400 does not do

  • It does not stop the owner, and it does not stop root: either can change the mode or the file.
  • It does not stop deletion. That is controlled by the folder’s write permission (and its sticky bit).
  • It does not encrypt anything. A copy of the file in a backup or an image keeps no protection unless the mode is preserved and the backup is itself private.

Read-only mode is also used to protect important text files from a careless edit, or build artifacts that should not change. For “everyone may read” files use 644. To see how a mode such as r--r----- (440) breaks down, put it in the chmod calculator. And if a service that runs under another account can’t read its own key, the file is probably too closed: see 403 Forbidden for how to trace a path.

Frequently asked questions

What does chmod 400 mean?

Only the owner can read the file (r--------). Nobody can write it, not even the owner, until the mode is changed. Others have no access.

Why does AWS tell me to chmod 400 my .pem file?

ssh refuses a key that other users can read, and the key is something you should never edit. 400 fulfils both. 600 also works for ssh; 400 additionally prevents accidental changes.

Can the owner still delete a 400 file?

Deleting depends on the permissions of the folder, not of the file. If you can write to the folder you can remove the file even at 400, though rm asks for confirmation.

Does 400 stop an attacker?

It stops other ordinary users. A process running as the same user, or as root, can read the file, or change the mode back with chmod. It is a guard against mistakes and against other accounts, not a lock.

More in this reference