chmod and chown Explained with Examples on Linux
Learn to read Linux permission bits, change modes with chmod in numeric and symbolic form, change owners with chown, and verify each change with stat.
Contents
Every file on Linux has an owner, a group and a set of permission bits. chmod changes the bits, chown changes the owner and group. This guide shows how to read Linux file permissions, change them with numeric and symbolic modes, and check the result.
Reading Linux permissions
Each file has three kinds of permission (read, write, execute) for three categories: the owner, the file’s group, and everyone else.
ls -l shows them as a string such as -rw-r--r--. After the file type character come three triplets: owner, group, others. stat can print the same information in numeric form:
mkdir -p /tmp/perm-demo && cd /tmp/perm-demo
touch notes.txt
chmod 644 notes.txt
stat -c '%a %A %u:%g %n' notes.txt644 -rw-r--r-- 0:0 notes.txtNumeric (octal) modes
A numeric mode is one to four octal digits. Each digit is a sum: read is 4, write is 2, execute is 1. The last three digits are owner, group and others, so 755 means owner rwx, group and others r-x. An optional leading digit holds the special bits: 4 for setuid, 2 for setgid, 1 for sticky.
cd /tmp/perm-demo
chmod 600 notes.txt
stat -c '%a %A' notes.txt
chmod 755 notes.txt
stat -c '%a %A' notes.txtSymbolic modes
Symbolic modes name who is affected (u owner, g group, o others, a all), an operator, and the permissions. + adds bits, - removes them, and = sets exactly the bits you list and clears the rest. Clauses are separated by commas.
cd /tmp/perm-demo
chmod u=rw,go= notes.txt
stat -c '%a %A' notes.txt
chmod g+r,o+r notes.txt
stat -c '%a %A' notes.txt
chmod a-w notes.txt
stat -c '%a %A' notes.txtIf you leave out the who letters, the change applies as if you typed a, except that bits set in your umask are left alone. That is why chmod +w may not grant write to everyone. Writing a+w is explicit.
You can also copy permissions from one category to another, as in chmod g=u file, which gives the group whatever the owner has.
The capital X
X grants execute only if the target is a directory or already executable by someone. It is useful for recursive changes, because you can open up directories without making every regular file executable:
cd /tmp/perm-demo
mkdir -p tree/sub
touch tree/data.txt tree/sub/more.txt
chmod -R u=rwX,go=rX tree
stat -c '%a %n' tree tree/sub tree/data.txt tree/sub/more.txtDirectories end up as 755 and the plain files as 644.
Special bits
Setuid and setgid make a program run with the file’s owner or group as its effective ID. The sticky bit stops unprivileged users from removing or renaming files in a directory unless they own the file or the directory; /tmp is the usual example.
cd /tmp/perm-demo
mkdir shared
chmod 2775 shared
stat -c '%a %A' shared
chmod +t shared
stat -c '%a %A' sharedBe careful with a plain numeric mode on a directory: it preserves existing setuid and setgid bits. To clear them, use a leading zero (00755) or =755.
Changing owner and group with chown
The syntax is chown owner:group file. Giving only owner changes just the owner. owner: also sets the group to that owner’s login group. :group changes only the group, like chgrp.
Only a privileged process can change a file’s owner. The examples below run as root and use numeric IDs so they work on every distribution:
cd /tmp/perm-demo
chown 1234 notes.txt
stat -c '%u:%g' notes.txt
chown 1234:2345 notes.txt
stat -c '%u:%g' notes.txt
chown :3456 notes.txt
stat -c '%u:%g' notes.txt
chown -R 1234:1234 tree
stat -c '%u:%g %n' tree tree/sub/more.txtUseful options:
-Rrecurses into directories.-creports only files that actually changed, and-vreports every file.-hacts on a symbolic link itself instead of its target.--from=ownerchanges a file only if it currently matches.--reference=filecopies owner and group from another file.
Cautions
chmodnever changes a symlink’s own permissions. It follows command-line symlinks, but ignores symlinks found while recursing.- On Linux, when anyone changes the owner or group of an executable, the kernel clears its setuid and setgid bits. Check the mode afterward if you need them.
To see how much space a directory tree uses, read Check disk usage on Linux with df, du and ncdu. Archives can carry ownership too; see tar Command: Create, List and Extract Archives on Linux.
rm -rf /tmp/perm-demoSources
This article was drafted with AI assistance, fact-checked against the sources listed above and reviewed by a human editor before publication. Spotted an error? See our corrections policy.