What does chmod 400 mean? Read-only permissions for keys and files
By TechCompare · Updated · How we check
chmod 400 sets permissions to r--------: the owner can read the file, and group and others get nothing. Use it for SSH private keys and for files you only ever read.
Mode 400 lets the owner read the file and nothing else. Writing to it needs a chmod first, so it guards against accidental edits, the owner included. In ls -l it shows as -r--------. A script run by the owner can't write to the file either, until someone changes the mode.
How this is calculated
In octal, the owner's digit is 4, which is read only, and the group and others digits, 0 and 0, add up to nothing. There's no write or execute bit anywhere in the mode.
Read-only isn't a lock. The owner can run chmod to add write back. Deleting a file depends on the folder's write permission, not the file's own bits, so a 400 file in a folder you can write to can still be removed. An editor may warn that the file is read only.
The ssh(1) manual says a private key 'should be readable by the user but not accessible by others', and that ssh 'will simply ignore a private key file if it is accessible by others'. So 400 and 600 both work for a key. 400 is the stricter choice because it also takes away the owner's write bit.
Verdict
Reach for 400 when a key or file should stay unchanged. A downloaded key file that others can read, such as one at mode 644, is ignored by ssh, and chmod 400 key.pem is the quick fix.
Don't use 400 for a file a program has to write, like a log, a database or a config the app rewrites. A program that writes to the file in place, running as the owner, gets a permission error. Use 600 for those.
More CHMOD scenarios
Related guides
Frequently asked questions
chmod 400 or 600 for an SSH key?
What does chmod 400 mean?
Can the owner still delete or change a 400 file?
Why won't ssh use my private key file?
ls -l on the file to see its mode. If others can access it, run chmod 400 or chmod 600 on it. Both keep the file away from others while the owner keeps read access.Related tools
Subnet Calculator
Calculate network address, broadcast address, and usable IP ranges for IPv4/IPv6.
Use toolJSON Formatter
Validate, format, and minify JSON. Browse it as a tree, run JSONPath queries, compare two documents, or validate against a JSON Schema.
Use toolUnix Timestamp
Convert between Unix timestamps and human-readable dates.
Use tool