chmod 640: Configuration Files a Service Group Can Read
640 is rw-r-----: the owner can read and write, the owning group can read, and no other account can read anything at all. It is the right mode for a configuration file that contains a credential and must be read by one service.
At a glance
- Octal
- 640, written 0640 in four digit form
- Symbolic
- u=rw,g=r,o=
- On a file
- -rw-r-----
- On a directory
- drw-r-----, unusable because execute is missing
- Owner
- Read and write
- Group
- Read only. This is how the service gets in
- Others
- Nothing. No other local account can read the contents
In detail
640 answers a specific question: how does one service read a secret without every account on the machine reading it too. The file is owned by the account that manages it, the group is the service's group, and the group gets read only. The service can start; a shell user outside that group gets permission denied even with the full path.
It is the mode you will find on a lot of system files that carry credentials or sensitive detail. Log files under /var/log are commonly root:adm at 640 so that members of the adm group can read them without being root, and several packaged database and web server configuration files ship the same way for the same reason.
The group read bit rather than group write is the point. The service needs to read the configuration, not to rewrite it, so giving it write permission would let a compromised service modify its own configuration and persist. 640 keeps the write capability with the account or process that legitimately deploys the file.
Its partner directory mode is 750. A 640 file inside a 755 directory is still private in the sense that only the group can read it, but the directory listing reveals its name and size to everyone; a 750 directory hides even that. Pairing 750 directories with 640 files gives a tree where nothing outside the group learns anything.
For a secret only the owner needs, drop the group entirely and use 600. The choice between 600 and 640 is exactly the question of whether some other account has to read this file, and answering it deliberately is what stops environment files ending up at 644 by default.
Examples
chmod 640 /srv/app/.env
chmod u=rw,g=r,o= /srv/app/.envchown deploy:www-data /srv/app/.env
chmod 640 /srv/app/.env
-rw-r----- 1 deploy www-data /srv/app/.env
The deploy account writes it. The web server, running as a
member of www-data, reads it. Nobody else can open it at all.-rw-r----- 1 root adm /var/log/syslog
-rw-r----- 1 root adm /var/log/auth.log
Members of adm read the logs without being root.Things that catch people out
- Setting 640 without also setting the group achieves nothing. If the group is still the owner's private group, the service is not in it and will fail to read the file.
- A process that was already running does not pick up a new group membership. After adding a service account to a group, restart the service, or the read will keep failing against a mode that now looks correct.
- 640 files inside a 755 directory still leak their names, sizes and timestamps to any account that can list the directory. Use 750 on the directory when even the filename is sensitive.
- Deploy scripts that copy files often reset the mode to the umask default of 644. Set the mode explicitly after the copy, or the secret quietly becomes world readable on the next release.
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 750: Owner Writes, Group Reads, Others Shut Out
750 gives the owner everything, the group read and traverse, and everyone else nothing. The right mode for application directories that must stay private.
chmod 644: The Default File Mode and When to Change It
644 gives the owner read and write and everyone else read only. Why it is the default for files under a 022 umask, and the cases where it is wrong.
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 622: Write Without Read, and the Old Terminal Mode
622 lets the owner read and write while the group and others can only write. Where this came from, what write without read really allows, and why it is rare.
chmod 555: Read and Execute With No Write for Anyone
555 removes the write bit from the owner as well as everyone else. What that protects against, what it does not, and how real immutability is done.
Tools for this
- Chmod Calculator Free online chmod calculator - calculate Unix file permissions with an interactive chmod builder