breakitdown

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

Answer

chmod 400 sets r--------: the owner can read the file but not change it, and nobody else has any access. It is often used for SSH private keys, such as the .pem key downloaded for an EC2 instance.

Try

chmod 400 = r--------: owner read, everyone else nothing.

Read 4Write 2Execute 1Digit
Owner (user)4
Group0
Others0
Special0
  1. 4
    Owner (user): 4 = 4 read
  2. 0
    Group: 0 = no permissions
  3. 0
    Others: 0 = no permissions

On a file

  • Owner (user) read it
  • Group no access
  • Others no access

On a directory

  • Owner (user) list the names inside (names only: without x the files cannot be opened or even stat'ed)
  • Group no access
  • Others no access

chmod 400 filechmod u=r,go= filels -l → -r--------

What to know

OpenSSH's rule for private keys is that the key file must not be accessible by group or others; any mode where the last two digits are 0 passes. So 400 and 600 both work. 400 adds protection against overwriting the key by accident.

The rules for the other files are looser. With StrictModes (the default), sshd ignores authorized_keys if that file, ~/.ssh or the home directory is writable by group or others, or owned by someone other than you or root. Read access for others is allowed there; 700 for ~/.ssh and 600 for authorized_keys are the recommended values.

Where it fits

Gotchas

Related explainers

← chmod calculator