chmod 666 (rw-rw-rw-)
Every account on the system can read and modify the file. On a regular file that is rarely what you need: the real fix is usually a change of owner or group.
This tool runs in your browser. We never send what you type or paste to a server.
What chmod 666 sets
| Who | Digit | Read | Write | Execute | Symbolic |
|---|---|---|---|---|---|
| Owner (u) | 6 | yes | yes | no | rw- |
| Group (g) | 6 | yes | yes | no | rw- |
| Others (o) | 6 | yes | yes | no | rw- |
chmod 666 file # numeric
chmod a=rw file # the same, in symbolic form
ls -l shows this mode as -rw-rw-rw- on a file and drw-rw-rw- on a directory. On a file, execute means it can be run as a program. On a directory, read lists the names inside, write creates, renames and deletes entries, and execute (search) is what lets you enter it and reach the files inside.
What goes wrong with a 666 file
Any local user can change the contents, and so can any service account, including the one a compromised web application runs as. How bad that is depends on what reads the file afterwards:
- Configuration or code. If a more privileged program loads it, whoever can write the file can steer that program. The missing execute bit is no protection:
bash file,php fileandsource fileonly need to read it. - Data. A database file, an upload or a log can be silently altered or emptied.
- Secrets. Everyone can read it too.
Some software refuses to work with it at all: ssh ignores a private key with this mode, and sshd ignores a world-writable authorized_keys.
One thing 666 does not grant is deletion. Removing or renaming a file is governed by its directory.
Where 666 is normal
- Device files that everyone must be able to write to, such as
/dev/nulland/dev/zero. There is nothing in them to corrupt. - As a starting point. 666 is the mode programs request when they create a file; the umask then removes bits from it, which is how you end up with 644. A umask of
000would leave every new file at 666, so do not set one.
What to do instead
"Permission denied" on a write means the program is running as a user the mode does not cover. Find out which user, then give that user access and nobody else:
ls -l data.db # current owner and group
ps -o user= -p 1234 # the user that process 1234 runs as
- One program writes it. Make that account the owner:
chown appuser data.db(as root), then 600 or 644. - Several accounts write it. Put them in a group:
chgrp team data.dband 664, or 660 if others should not read it. - One extra user, no sensible group. On Linux with ACLs:
setfacl -m u:alice:rw data.db.
To find files that are already too open, and close them:
find /var/www -type f -perm -002 # writable by others
find /var/www -type f -perm -002 -exec chmod o-w {} +
666 on a directory
Pointless. Without the execute bit nobody can create, open or delete anything in the directory, however many write bits it has. People who try this were looking for 777, and should read that page before using it.
666 compared with its neighbours
Questions
Is chmod 666 safe?
Not on a multi-user machine or a server. Every account, including the ones services run under, can read and rewrite the file. It is acceptable for something disposable on a machine only you use. Otherwise change the file's owner or group so that the one program that needs to write can, and keep the mode at 644 or 664.
What is the difference between chmod 666 and 777?
777 adds the execute bit for everyone. On a file, that makes it directly runnable. On a directory, the execute bit is what makes the write bit usable, so a 777 directory can be modified by anyone while a 666 directory cannot be used by anyone. Both are too open for normal use.
Should I chmod 666 /var/run/docker.sock or a serial port like /dev/ttyUSB0?
It gets rid of the error, but it gives every user on the machine that access, and the mode is lost when the socket or device node is created again. Add your user to the group that owns it instead (ls -l shows which), then log out and back in. For Docker that group is docker; note that Docker's own documentation warns that membership amounts to root-level privileges.