The guide
File modes, in plain words.
What the ten characters at the start of an ls -l line mean, how Linux decides which three apply to you, what the special bits do, and the reason behind every flag the reader raises.
The ten characters
In -rw-r--r-- the first character is the type: - a file, d a folder (directory), l a symbolic link, c and b devices, p a named pipe, s a socket. The other nine are three groups of three, for the owner, the group and everyone else: r read, w write, x execute, or - for a right that is not given.
The number form adds the rights in each group: read 4, write 2, execute 1. So rw- is 6, r-x is 5 and rwx is 7, and -rw-r--r-- is 644. A fourth digit in front holds the special bits: 4 setuid, 2 setgid, 1 sticky (so 2775 is a setgid folder).
Which three apply to you
Linux looks at you once: if you own the file, the owner’s three rights apply and nothing else; if not, but you are in the file’s group, the group’s three apply; otherwise the last three. It does not add them up. That is why a file with mode 077 is closed to its own owner while everyone else may use it. The superuser (root) is not held back by these bits for reading and writing.
Folders are different
| Letter | On a file | On a folder |
|---|---|---|
r | read it | see the names in it |
w | change it | create, rename or delete the files in it, whoever owns them (needs x as well) |
x | run it as a program | go into it and reach what is inside by name |
Deleting a file is a change to its folder, not to the file, so write access on a folder lets an account delete files it could not even read. A folder with x but no r (711 is common for home folders) lets others reach a path they already know without listing what else is there.
setuid, setgid and the sticky bit
setuid (an s in the owner’s execute place) makes a program run as its owner, whoever starts it. passwd uses it to write a file only root may change, and sudo, su and mount need it too: the reader writes no command for system programs like these, because removing the bit can lock you out of root. On a folder Linux ignores it.
setgid (an s in the group’s place) makes a program run with its group’s rights. On a folder it does something useful: files made inside get the folder’s group instead of their creator’s, which keeps a team folder shared.
sticky (a t in the last place) on a folder means only a file’s owner, the folder’s owner or root may delete or rename the files in it, even though others can write there. /tmp is 1777 for that reason. On files Linux ignores it.
A capital S or T means the special bit is set but the execute right in that place is not. What that does depends on the bit:
- A capital
Sin the owner’s place: the owner cannot run the file, but if the group or everyone else can, the program still runs as its owner when they start it. Only when nobody may run it does setuid do nothing. - A capital
Sin the group’s place: on a folder it works as usual (new files take the folder’s group); on a file it does nothing on current Linux (it once marked files for mandatory locking). - A capital
Ton a folder: everyone else cannot go in, but the sticky bit still protects files from anyone who can write the folder, such as its group. Debian and Ubuntu keep/var/spool/cron/crontabsat 1730 (drwx-wx--T) for that reason. Only on a folder that just its owner can change does it do nothing.
The reader tells these cases apart, and it never suggests removing a sticky bit.
On GNU systems, chmod 755 on a folder keeps its setuid and setgid bits; chmod 00755 or chmod =755 clears them (GNU coreutils manual, “Directories and the Set-User-ID and Set-Group-ID Bits”).
Web folders
Hosts differ, so the reader asks two things when you tick “a web folder”: which account the web server runs as, and whether PHP runs as your own account. Common, safe starting points are folders 755 (or 750 where the host gives the web folder to the web server’s group), files 644 (or 640), and configuration files with passwords 600 or 640. Nothing in a web folder needs 777. Upload folders need write access for the account that writes the uploads, not for everyone.
These are the numbers WordPress gives for a host where PHP runs as your account (WordPress Advanced Administration Handbook, “Changing File Permissions”): folders 755 or 750, files 644 or 640, wp-config.php 440 or 400, and “no directories should ever be given 777, even upload directories”. Your host’s own help pages win where they differ.
Where PHP runs as your own account (PHP-FPM or suEXEC, as on many cPanel servers), a configuration file can be 600. Where PHP runs as the web server’s account, that account reads the file through its group or through “everyone else”, so 640 with the file’s group set to the web server’s group is the safer choice. The reader says this next to every such fix.
ACLs and SELinux
A + right after the mode means the file also has an access control list, which can give named accounts more (or less) than the bits show; read it with getfacl. A dot means an SELinux label, which can refuse access the bits would allow. The reader points these out; it cannot see inside them.
Every rule the reader checks
Each flag carries one of four levels: Fix now Check Worth a look Note. Some rules rise a level when the listing is marked as a web folder. A suggested fix only ever takes a right away; anything that needs judgement gets a note instead of a command.
- Fix now Full access for everyone (777)
- Every account on the server can change or replace it. On shared hosting that includes other customers' sites and any script that has been broken into.
- Fix now Anyone can change this file
- The "everyone else" class has write permission, so any account on the server can change it.
- Fix now Anyone can add or delete files in this folder
- Write permission on a folder lets every account add, rename or delete the files in it, even files it does not own.
- Note Shared folder with the sticky bit (like /tmp)
- Everyone can add files here, but only a file's owner (or the folder's owner, or root) can delete or rename it. Right for a shared temp folder; wrong for a site's own folder.
- Fix now Runs with its owner's powers (setuid)
- Whoever starts it, it runs as its owner. A few system programs need this (passwd, sudo), and Modewise gives those a note instead of a command. Anywhere else it is a common way in for an attacker.
- Check Runs with its group's powers (setgid)
- Whoever starts it, it runs with its group's rights. Expected on a few system programs only.
- Note New files here get this folder's group (setgid folder)
- Files and folders made inside take the folder's group instead of the creator's. Usually set on purpose for a team folder.
- Worth a look A special bit that has no effect
- The special bit is set where it can do nothing: setuid on a file that no class may run (a capital S with no x anywhere), or the sticky bit on a folder that only its owner can change. It is often a slip in a chmod command. A capital S or T does not always mean this; the guide's setuid, setgid and sticky section explains each case.
- Worth a look Everyone in the group can change it
- Fine when the group is only you or your own team. On a shared server, check who is in it (getent group NAME).
- Check Every account can read a file that usually holds passwords
- Files such as .env, wp-config.php and config.php hold database passwords and keys. The "everyone else" class can read this one.
- Fix now A private key others can read
- A private key should be readable by its owner only. ssh refuses to use one that the group or others can read.
- Check Others can read a file that may be a private key
- Files ending in .key are often TLS or SSH private keys, which only their owner should read. Other files use the same ending (a Keynote deck, a licence file), so Modewise writes no command for it: a private key is a short text file whose first line starts with -----BEGIN and names a PRIVATE KEY.
- Note A system program that needs its special bit
- Programs such as sudo, su, passwd, mount and crontab are installed setuid or setgid by the operating system and stop working without the bit, so Modewise writes no command for them: do not remove it. To see which package installed a program, run dpkg -S PATH (Debian, Ubuntu) or rpm -qf PATH (Red Hat-style systems).
- Fix now Others can change your SSH login setup
- If the group or others can write .ssh or authorized_keys, they can add their own key and log in as you. sshd also refuses such files when StrictModes is on (the default).
- Worth a look The owner has fewer rights than others
- Linux checks only the first class that matches you: the owner gets the owner's rights even when group or others have more. So the owner is refused what others may do.
- Worth a look Execute right on a web file
- Web servers read pages, styles and scripts; they do not need the execute right to send them or to run PHP through PHP-FPM or mod_php. 644 is the usual mode for files.
- Check Group can change a file in the web folder
- In a web folder, write access for the group is rarely needed and lets every account in that group change what visitors see.
- Check A database copy or backup in the web folder
- A file the web server can read in a web folder can be downloaded by anyone who guesses its address. Backups and database dumps belong outside the web folder.
- Note The web server may not be able to send this file
- Neither the group nor everyone else can read it. Many hosts serve static files through one of those classes, so this file may answer 403 Forbidden.
- Fix now A device everyone can write
- Write access to a disk or memory device lets any account change raw data. Only a few devices (/dev/null, /dev/tty and the like) are meant to be open to everyone.
- Note Write right on a folder without the enter right
- On a folder, w does nothing without x: a class that cannot go into the folder cannot add or delete anything in it.
When to ask for help
A file mode is a setting, but some settings are signs of trouble. Stop and ask before you change anything when:
- Something is there that you did not put there: a setuid or setgid program you did not install, a file or folder at 777 that you did not make, or a database copy or archive in the web folder that you did not leave. These can mean someone broke into the site. Changing the mode does not clean that up and can destroy the trail: keep a copy of the listing, delete nothing yet, and ask your host’s support or a system administrator to look.
- It is in a system folder:
/usr, /bin, /sbin, /lib, /etc, /bootand the folders below them get their modes from the operating system’s packages. Do not change them from this list. To see which package installed a file, rundpkg -S PATH(Debian, Ubuntu) orrpm -qf PATH(Red Hat-style systems); if none did, or a mode looks wrong, ask your host or a system administrator. - Your hosting is managed or shared: the host may set the right modes and own some of the files. Ask them before you change modes they set, and before you change who owns anything.
A Modewise reading is not a security audit, and the reader cannot tell a mistake from an attack.
What Modewise does not do
It does not connect to your server, log in, scan, or run any command, and it asks for no password. It reads text you paste and nothing else. It cannot know who is in a group, how your host runs PHP, or what an ACL holds; where that matters, it says so. The fixes are suggestions to read before you run them. Linux and other Unix-like systems are named here only to say what the tool reads.
It reads an ls line by its spaces. A name with two or more spaces in a row is read as written. A name that starts or ends with a space is read exactly only when ls quotes it (' name', as it does on a terminal), and a name holding a line break or another control character is not read correctly. Check any odd name before you run a fix for it.
References: the GNU coreutils manual (File permissions; chmod invocation; Directories and the Set-User-ID and Set-Group-ID Bits), chmod(1), chown(2), execve(2), sshd_config(5) StrictModes, and the WordPress Advanced Administration Handbook (Changing File Permissions).