Linux and servers · Beginner · 12 min
How File Permissions Work in Linux: chmod, chown and umask
By Padding · Updated 15 Nov 2026
Ubuntu 26.04.1 LTS
You will read Linux permission strings, change them with chmod and chown, set up a folder two users can share, and control the defaults with umask.
On this page
Every file and folder on Linux has an owner, a group and a set of permissions. They decide who can read it, change it or run it. When a website returns "403 Forbidden", or a script says "Permission denied", the cause is nearly always here.
In this tutorial you will learn to read the permission string from ls -l, change it with chmod, change ownership with chown, share a folder between two users, and set the defaults for new files with umask. You will use two test users so you can see a permission refuse someone. Root ignores permissions, so testing as root proves nothing.
Step 1 · Read a permission string
Set up a practice folder with two users, alice and bob, and a group they share.
sudo apt-get update
sudo apt-get install -y adduser
sudo adduser --disabled-password --gecos "" alice
sudo adduser --disabled-password --gecos "" bob
sudo addgroup webteam
sudo usermod -aG webteam alice
sudo usermod -aG webteam bob
sudo mkdir -p /tmp/permissions-demo
cd /tmp/permissions-demo
echo "hello" > notes.txt
ls -l notes.txtThe last command prints:
-rw-r--r-- 1 root root 6 Nov 15 04:22 notes.txtRead it from left to right.
- The first character is the type:
-for a file,dfor a directory,lfor a link. - The next nine characters are three groups of three: the owner's permissions, the group's, and everyone else's.
- In each group,
ris read,wis write andxis execute. A-means that permission is off. - Then come the link count, the owner (
root), the group (root), the size, the date and the name.
So rw-r--r-- means: the owner can read and write, the group can only read, and everyone else can only read.
Check it worked
You can print the same information as a number with stat:
stat -c '%a %A %U:%G' /tmp/permissions-demo/notes.txtIt prints 644 -rw-r--r-- root:root. The number is the octal form, covered next.
Step 2 · Change permissions with chmod
chmod changes the permissions. It takes them in two forms.
Symbolic form says what to add or take away. The letters are u (owner), g (group), o (others) and a (all). The operators are +, - and =.
cd /tmp/permissions-demo
chmod u+x,g-r,o= notes.txt
ls -l notes.txtThis gave the owner execute, took read from the group, and set others to nothing. The result is -rwx------.
Octal form sets all nine bits at once. Read is 4, write is 2 and execute is 1. Add the values for each group.
| Digit | Meaning | Bits |
|---|---|---|
| 7 | read, write, execute | rwx |
| 6 | read, write | rw- |
| 5 | read, execute | r-x |
| 4 | read | r-- |
| 0 | nothing | --- |
So 640 is rw-r-----: the owner reads and writes, the group reads, others have nothing.
cd /tmp/permissions-demo
chmod 640 notes.txt
ls -l notes.txtWhat the execute bit does
A script is not runnable until it has x. Make one and see:
cd /tmp/permissions-demo
printf '#!/bin/sh\necho ran\n' > run.sh
./run.sh || echo "exit code $?"
chmod +x run.sh
./run.shThe first attempt fails with Permission denied and exit code 126. After chmod +x it prints ran.
On a directory, x means "can enter it". A directory needs r to list its contents and x to open anything inside. A directory with r but no x lists names but lets nobody open the files.
Step 3 · Change the owner and group with chown
chown changes the owner, the group, or both. Only root can give a file to another user.
cd /tmp/permissions-demo
mkdir -p site
sudo chown alice:webteam notes.txt site
ls -l notes.txt
ls -ld siteThe syntax is chown owner:group path. Leave out the owner (chown :webteam file) to change only the group, or use chgrp webteam file. Add -R to apply it to everything inside a directory. Use it carefully: chown -R on the wrong path is hard to undo.
Check it worked
Now alice owns notes.txt with the mode 640, and the group webteam can read it. Test with both users:
runuser -u alice -- cat /tmp/permissions-demo/notes.txt
if runuser -u nobody -- cat /tmp/permissions-demo/notes.txt; then echo "unexpected: everyone can read it"; false; fialice sees the text. nobody, who is neither the owner nor in the group, gets Permission denied. The if form lets the script continue past the expected failure.
Step 4 · Share a directory between a group
Two people in webteam should both write into site and read each other's files. This needs two things: group write permission, and a way for new files to inherit the group.
cd /tmp/permissions-demo
sudo chmod 2775 site
ls -ld siteThe first digit, 2, sets the setgid bit. On a directory, it makes every new file inside belong to the directory's group instead of the creator's primary group. In ls -l it shows as an s in the group's execute position: drwxrwsr-x.
Check it worked
Have bob create a file, and look at its group:
runuser -u bob -- touch /tmp/permissions-demo/site/index.html
ls -l /tmp/permissions-demo/siteThe file is owned by bob, and its group is webteam, not bob. Without the setgid bit, the group would be bob, and alice could not edit it.
Troubleshooting
If bob cannot create the file, check the three things a path needs, in order: bob is in the group (id bob), the group has write on the directory (ls -ld), and every parent directory is executable by him (namei -l /tmp/permissions-demo/site). A user added to a group only gets the new group at their next login.
Step 5 · Control defaults with umask
New files do not start with 777. The umask is a mask of permissions to remove from each new file or directory. Files start from 666 and directories from 777, and the mask takes bits away.
umaskOn Ubuntu the value is 0022, which removes write from group and others. New files get 644 and new directories 755. A stricter 077 leaves only the owner with access:
cd /tmp/permissions-demo
(umask 077; touch private.txt; mkdir private-dir)
touch ordinary.txt
mkdir ordinary-dir
ls -ld private.txt private-dir ordinary.txt ordinary-dirThe first two come out -rw------- and drwx------. The others use the default: -rw-r--r-- and drwxr-xr-x. The parentheses run the umask change in a subshell, so it affects only those two commands. To change it for a whole login session, set umask in ~/.profile.
Step 6 · Spot dangerous permissions
A file that anyone can write is a problem on a web server, because any compromised process can change it. List them under a folder:
chmod 777 /tmp/permissions-demo/ordinary.txt
find /tmp/permissions-demo -type f -perm -o+wThe command prints ordinary.txt. -perm -o+w matches files where the "others" write bit is set. Repair it:
chmod o-w /tmp/permissions-demo/ordinary.txtTwo more special bits show up on a server.
- Sticky bit (
tat the end, as indrwxrwxrwt): in a shared directory such as/tmp, only a file's owner can delete it. - setuid (
swhere the owner'sxwould be, as in-rwsr-xr-x): the program runs as its owner, not as whoever starts it./usr/bin/passwduses it, so ordinary users can change their own password.
Never set setuid on your own scripts or programs unless you know why you need it.
Next steps
- Apply this to a real login in
/tutorials/create-a-sudo-user-on-ubuntu-26-04. - Use correct ownership on keys in
/tutorials/log-in-to-a-server-with-ssh-keys, where SSH refuses files with loose permissions. - Read
man chmod,man chownandman umaskon the server for the full list of options.
Related tutorials
- How to Schedule Jobs with Cron on LinuxYou will schedule a command to run every minute, then on a real timetable, read the cron syntax, and learn why a job that works in your shell can fail under cron.
- How to Run Your App as a systemd Service on Ubuntu 26.04You will write a systemd unit file that starts your app at boot, restarts it when it crashes, runs it as an unprivileged user, and sends its logs to the journal.
- How to Log In to a Linux Server with SSH KeysYou will create an Ed25519 SSH key, install its public half on a server, log in without a password, and save the connection as a short name.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials