File Permissions and Ownership

How Linux file permissions work: reading ls -l, rwx on files versus directories, chmod in symbolic and octal form, umask, owners and groups, special bits, and sudo.

By The Dark DebuggerUpdated 29 min read

This article explains who may read, change and run each file on a Linux system, and how to change that: reading ls -l, what read, write and execute mean for files and (differently) for directories, chmod in both notations, umask, owners and groups, the setuid, setgid and sticky bits, and the basics of sudo. You should be comfortable creating and listing files (Working with Files and Directories) and using globs (Wildcards, Globbing, and Brace Expansion). The next article, Finding Files with find, locate, and xargs, uses permissions as search criteria.

Why permissions matter

Three messages account for most permission trouble in day-to-day work:

  • bash: ./deploy.sh: Permission denied when you run a script you just wrote.
  • WARNING: UNPROTECTED PRIVATE KEY FILE! when SSH refuses a key you copied from another machine.
  • A teammate who can read a shared report but can't save changes to it.

All three are fixed in seconds once you can read a permission string and know which bit is missing. The wrong fix, chmod 777 on everything, makes the error go away by making the file writable by every account on the machine. By the end of this article you won't need it.

Set up a practice directory with a config file, a script and a directory:

mkdir -p ~/practice/perms && cd ~/practice/perms
printf 'db_host=db.internal\n' > app.conf
printf '#!/usr/bin/env bash\necho "Deploying release $1"\n' > deploy.sh
mkdir releases

Reading ls -l

ls -l
total 12
-rw-r--r-- 1 sid sid   20 Nov  9 21:56 app.conf
-rw-r--r-- 1 sid sid   48 Nov  9 21:56 deploy.sh
drwxr-xr-x 2 sid sid 4096 Nov  9 21:56 releases

The first column is ten characters. Take -rw-r--r-- apart:

Positionapp.confMeaning
1-file type: - regular file, d directory, l symbolic link
2–4rw-permissions for the user who owns the file
5–7r--permissions for members of the file's group
8–10r--permissions for others: everyone else

The third and fourth columns are the owner (sid) and the group (sid). On Ubuntu, every user gets a personal group with the same name, which is why they match here.

When you access a file, Linux picks exactly one of the three sets: the user set if you own the file, otherwise the group set if you're in its group, otherwise the "others" set. It does not combine them. (root skips these checks for reading and writing.)

stat prints the same information along with the octal form, which we'll get to shortly, and id shows which user and groups you are:

stat -c '%A %a %U:%G %n' app.conf deploy.sh releases
id
-rw-r--r-- 644 sid:sid app.conf
-rw-r--r-- 644 sid:sid deploy.sh
drwxr-xr-x 755 sid:sid releases
uid=1000(sid) gid=1000(sid) groups=1000(sid),4(adm),20(dialout),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev),100(users),107(netdev),1001(docker)

That is the default first user on Ubuntu under WSL: a personal group plus several system groups, including sudo (allowed to run commands as root) and users, which we'll use for a shared directory later.

What r, w and x mean for a file

For a regular file the meanings are what you'd guess: r read the contents, w change the contents, x run it as a program. Taking a bit away from yourself shows each one failing:

chmod u-r app.conf
cat app.conf
chmod u+r,u-w app.conf
echo 'db_port=5432' >> app.conf
chmod u+w app.conf
cat: app.conf: Permission denied
bash: app.conf: Permission denied

The second error comes from bash, not echo, because the shell opens the file for the >> redirection before echo ever runs.

Execute permission is the one you'll meet most. A new file never has it, so running a fresh script fails:

./deploy.sh v1.5.0
bash deploy.sh v1.5.0
chmod u+x deploy.sh
./deploy.sh v1.5.0
ls -l deploy.sh
bash: ./deploy.sh: Permission denied
Deploying release v1.5.0
Deploying release v1.5.0
-rwxr--r-- 1 sid sid 48 Nov  9 21:56 deploy.sh

bash deploy.sh works without x because there you are running bash, which merely reads the script. ./deploy.sh asks the kernel to execute the file itself, which requires x (and the #!/usr/bin/env bash line tells the kernel which interpreter to use).

What r, w and x mean for a directory

A directory is a list of names. The bits apply to that list, and this is where most confusion comes from:

BitOn a directory it lets you…
rlist the names in it (ls)
wcreate, delete and rename entries in it (only together with x)
xenter it (cd) and access anything inside by name

The surprising combinations are worth seeing. With r but no x, you can see the names but nothing else:

mkdir vault && echo 's3cret' > vault/token.txt
chmod 600 vault
ls -ld vault
ls vault
ls -l vault
cat vault/token.txt
cd vault
drw------- 2 sid sid 4096 Nov  9 21:56 vault
token.txt
ls: cannot access 'vault/token.txt': Permission denied
total 0
-????????? ? ? ? ?            ? token.txt
bash: cd: vault: Permission denied

ls could read the name token.txt, but ls -l couldn't look up the file's details (hence the question marks), cat couldn't open it, and cd refused. With x but no r, it's the opposite: you can't list the directory, but you can open a file whose name you already know:

chmod 100 vault
ls vault
cat vault/token.txt
ls: cannot open directory 'vault': Permission denied
s3cret

Finally, without w you can't add or remove entries, but you can still change the contents of a file that is itself writable, because that changes the file, not the directory listing:

chmod 500 vault
touch vault/new.txt
rm vault/token.txt
echo 'rotated' > vault/token.txt
cat vault/token.txt
chmod 700 vault
touch: cannot touch 'vault/new.txt': Permission denied
rm: cannot remove 'vault/token.txt': Permission denied
rotated

The practical rule: directories need r and x together to be useful. That's why directories are almost always 755 or 750 or 700, never 644.

Deleting a file depends on the directory, not the file

Since deleting removes a name from a directory, it is governed by the directory's w bit. A read-only file in a writable directory can be deleted. rm asks first when you run it at a terminal:

printf 'keep me\n' > readonly.txt
chmod 444 readonly.txt
echo 'append' >> readonly.txt
rm readonly.txt
bash: readonly.txt: Permission denied
rm: remove write-protected regular file 'readonly.txt'? n

I answered n. rm -f skips the question and deletes it. When rm doesn't run at a terminal (in a script or cron job), it doesn't ask: I checked with input redirected from /dev/null, and the file was removed without a prompt. Write protection on a file is not protection against deletion.

Changing permissions with chmod: symbolic mode

Symbolic mode says who, what operation, and which bits:

  • who: u (user/owner), g (group), o (others), a (all three)
  • operation: + add, - remove, = set exactly (anything not listed is removed)
  • bits: r, w, x, plus X and the special bits covered later

Several clauses can be joined with commas. Watch each change on a report file:

printf 'region,total\nnorth,1200\n' > report.csv
chmod 600 report.csv
ls -l report.csv
chmod g+r report.csv
ls -l report.csv
chmod u=rw,g=r,o=r report.csv
ls -l report.csv
chmod go-r report.csv
ls -l report.csv
chmod a+r report.csv
ls -l report.csv
chmod g=u report.csv
ls -l report.csv
chmod o= report.csv
ls -l report.csv
-rw------- 1 sid sid 24 Nov  9 21:56 report.csv
-rw-r----- 1 sid sid 24 Nov  9 21:56 report.csv
-rw-r--r-- 1 sid sid 24 Nov  9 21:56 report.csv
-rw------- 1 sid sid 24 Nov  9 21:56 report.csv
-rw-r--r-- 1 sid sid 24 Nov  9 21:56 report.csv
-rw-rw-r-- 1 sid sid 24 Nov  9 21:56 report.csv
-rw-rw---- 1 sid sid 24 Nov  9 21:56 report.csv

g=u copies the owner's bits to the group, and o= with nothing after it removes everything for others. Symbolic mode is best when you want to change one thing and leave the rest alone, like chmod u+x script.sh.

Capital X: execute only where it makes sense

X adds execute permission only to directories (and to files that are already executable for someone). That makes it the right tool for recursive changes, where directories need x but data files must not get it:

mkdir -p site/css site/img
touch site/index.html site/css/main.css site/img/logo.png
chmod -R u=rwX,go= site
chmod -R go+rX site
ls -lR site
site:
total 8
drwxr-xr-x 2 sid sid 4096 Nov  9 21:56 css
drwxr-xr-x 2 sid sid 4096 Nov  9 21:56 img
-rw-r--r-- 1 sid sid    0 Nov  9 21:56 index.html
 
site/css:
total 0
-rw-r--r-- 1 sid sid 0 Nov  9 21:56 main.css
 
site/img:
total 0
-rw-r--r-- 1 sid sid 0 Nov  9 21:56 logo.png

After the first command the whole tree was private to the owner (directories drwx------, files -rw-------); the second opened it up for reading. One recursive command, and every directory ended up 755 and every file 644, which is exactly what a static website needs.

Changing permissions with chmod: octal mode

Each permission bit has a value: r = 4, w = 2, x = 1. Add them for each of user, group and others, and you get three digits:

DigitBitsDigitBits
7rwx3-wx
6rw-2-w-
5r-x1--x
4r--0---

An octal mode sets all bits at once, so it's best when you know the exact end state:

chmod 640 app.conf
chmod 750 deploy.sh
stat -c '%a %A %n' app.conf deploy.sh
chmod 0755 deploy.sh
stat -c '%a %A %n' deploy.sh
640 -rw-r----- app.conf
750 -rwxr-x--- deploy.sh
755 -rwxr-xr-x deploy.sh

A fourth, leading digit holds the special bits (covered below); 0755 and 755 are the same. These are the modes you'll actually use:

ModeStringTypical use
644rw-r--r--ordinary files: documents, source code, web pages
600rw-------secrets: SSH private keys, .env files, credentials
640rw-r-----config readable by a service's group, hidden from everyone else
755rwxr-xr-xdirectories and programs everyone may use
750rwxr-x---directories and programs for the owner and one group
700rwx------private directories, such as ~/.ssh
777rwxrwxrwxalmost never correct; see Common mistakes

umask: the permissions new files get

New files don't start at random permissions. Programs ask for 666 (rw-rw-rw-) for files and 777 for directories, and the umask removes bits from that request. With the usual umask of 022, the write bit is removed for group and others:

umask
umask -S
touch default.txt; mkdir default-dir
ls -ld default.txt default-dir
0022
u=rwx,g=rx,o=rx
drwxr-xr-x 2 sid sid 4096 Nov  9 21:56 default-dir
-rw-r--r-- 1 sid sid    0 Nov  9 21:56 default.txt

umask -S shows the bits that are allowed rather than removed, which is easier to read. Changing the umask affects files created afterwards in that shell. Running it inside parentheses, which start a subshell, keeps the change temporary so you can compare:

(umask 027; touch private.txt; mkdir private-dir; ls -ld private.txt private-dir)
(umask 077; touch secret.txt; ls -l secret.txt)
(umask 002; touch shared.txt; ls -l shared.txt)
umask
drwxr-x--- 2 sid sid 4096 Nov  9 21:56 private-dir
-rw-r----- 1 sid sid    0 Nov  9 21:56 private.txt
-rw------- 1 sid sid    0 Nov  9 21:56 secret.txt
-rw-rw-r-- 1 sid sid    0 Nov  9 21:56 shared.txt
0022
umaskNew filesNew directoriesUse
022644755common default
002664775teams sharing files through a group
027640750servers: nothing readable by "others"
077600700scripts that write secrets

Two things to remember. The umask can only remove bits, so new files never get x (the request was 666); that's why every new script needs chmod +x. And it's a per-process setting: put umask 027 in ~/.bashrc to make it permanent for your shells, or at the top of a script that writes sensitive files.

Owners and groups: chown and chgrp

Every file has one owning user and one group. groups lists the groups you belong to:

groups
ls -l app.conf
chown root app.conf
chgrp users app.conf
ls -l app.conf
chgrp root app.conf
chown sid:sid app.conf
ls -l app.conf
sid adm dialout cdrom floppy sudo audio dip video plugdev users netdev docker
-rw-r--r-- 1 sid sid 20 Nov  9 21:56 app.conf
chown: changing ownership of 'app.conf': Operation not permitted
-rw-r--r-- 1 sid users 20 Nov  9 21:56 app.conf
chgrp: changing group of 'app.conf': Operation not permitted
-rw-r--r-- 1 sid sid 20 Nov  9 21:56 app.conf

The rules behind those results:

  • Only root can change a file's owner. Otherwise you could hand files to other users to dodge disk quotas, or plant files that look like someone else's.
  • The owner can change the group, but only to a group they belong to. users worked, root didn't.
  • chown user:group changes both at once, and chown :group changes just the group, like chgrp.

On a real server, ownership changes are done with sudo. These are the typical forms. I didn't run them, because my test account had no root password and the users and groups don't exist there:

sudo chown -R www-data:www-data /var/www/shop
sudo chown deploy: /srv/app/releases
sudo usermod -aG developers alice

The first gives a whole web root to the web server's account. In the second, a trailing : with no group means "the user's login group". The third adds alice to a group; group membership is read at login, so it only takes effect in her new sessions (or after newgrp developers). Forgetting that is a classic "I added her to the group and it still says Permission denied".

Special bits: setuid, setgid and sticky

Three more bits sit in front of the usual nine. They show up in the execute position of ls -l, and as the leading digit in octal: setuid = 4, setgid = 2, sticky = 1.

setuid: run as the file's owner

A program with the setuid bit runs with the privileges of its owner, not the user who started it. That's how an ordinary user can change their own password, which lives in a root-only file:

ls -l /usr/bin/passwd /usr/bin/sudo /usr/bin/crontab
find /usr/bin -perm -4000 -type f
-rwxr-sr-x 1 root crontab  39664 Mar 30  2024 /usr/bin/crontab
-rwsr-xr-x 1 root root     64152 May 30  2024 /usr/bin/passwd
-rwsr-xr-x 1 root root    277936 Mar  2  2026 /usr/bin/sudo
/usr/bin/newgrp
/usr/bin/umount
/usr/bin/sudo
/usr/bin/mount
/usr/bin/su
/usr/bin/gpasswd
/usr/bin/fusermount3
/usr/bin/passwd
/usr/bin/chfn
/usr/bin/chsh

The s in rws is setuid. crontab shows the group version, r-s in the group position: it runs with the crontab group so it can write to the spool directory. The find command lists every setuid program in /usr/bin; security audits run exactly this check, because an unexpected setuid-root binary is a privilege-escalation hole. Linux ignores setuid on shell scripts, so don't try to use it to give a script extra powers; that's what sudo rules are for.

setgid on a directory: shared group ownership

On a directory, setgid means "new files here belong to this directory's group", instead of the creator's personal group. That's the key to shared team directories. Compare a directory with and without it:

mkdir team-reports
chgrp users team-reports
chmod 2775 team-reports
ls -ld team-reports
touch team-reports/q3-summary.csv
mkdir team-reports/drafts
ls -l team-reports
mkdir plain-dir
chgrp users plain-dir
touch plain-dir/q3-summary.csv
ls -l plain-dir
drwxrwsr-x 2 sid users 4096 Nov  9 21:56 team-reports
total 4
drwxr-sr-x 2 sid users 4096 Nov  9 21:56 drafts
-rw-r--r-- 1 sid users    0 Nov  9 21:56 q3-summary.csv
total 0
-rw-r--r-- 1 sid sid 0 Nov  9 21:56 q3-summary.csv

Inside team-reports, the new file got group users, and the new subdirectory inherited the setgid bit too (r-s), so the rule carries down the tree. In plain-dir, the file got the creator's group sid, and the rest of the team would only have "others" access to it.

The sticky bit: /tmp

On a directory, the sticky bit means only a file's owner (or the directory's owner, or root) may delete or rename it, even if the directory is writable by everyone. That's what lets every user share /tmp without deleting each other's files:

ls -ld /tmp /var/tmp
ls -ld /tmp/snap-private-tmp
rmdir /tmp/snap-private-tmp
drwxrwxrwt 18 root root 12288 Nov  9 21:56 /tmp
drwxrwxrwt  6 root root  4096 Nov  9 21:55 /var/tmp
drwx------ 2 root root 4096 Nov  9 21:39 /tmp/snap-private-tmp
rmdir: failed to remove '/tmp/snap-private-tmp': Operation not permitted

The t at the end is the sticky bit. /tmp is writable by everyone, yet I couldn't remove a directory owned by root. Without the sticky bit, any user could delete anything in /tmp.

Capital S and T

The special bits share a position with x. When x is not set underneath, ls shows a capital letter, which almost always signals a mistake:

touch notes.txt
chmod 2644 notes.txt
ls -l notes.txt
mkdir dropbox
chmod 1770 dropbox
ls -ld dropbox
chmod 1777 dropbox
ls -ld dropbox
-rw-r-Sr-- 1 sid sid 0 Nov  9 21:56 notes.txt
drwxrwx--T 2 sid sid 4096 Nov  9 21:56 dropbox
drwxrwxrwt 2 sid sid 4096 Nov  9 21:56 dropbox

1777 is the /tmp mode: world-writable with the sticky bit.

sudo basics

sudo runs a single command as another user, root by default, after checking that you're allowed to. On Ubuntu, members of the sudo group may run anything, and sudo asks for your own password, not root's. It then remembers you for a few minutes (15 by default) so you aren't asked for every command.

My test account had no password set up for sudo, so sudo -n (non-interactive: fail instead of prompting) shows what happened:

sudo -n true
sudo: a password is required

The forms you'll use most (not run here, for that reason):

CommandWhat it does
sudo apt install treerun one command as root
sudo -u postgres psqlrun a command as a different user
sudo -llist what you're allowed to run
sudo -iopen a root login shell; exit to leave it
sudoedit /etc/ssh/sshd_configedit a root-owned file with your own editor, safely

My advice: use sudo for single commands, and avoid working in a root shell. Each sudo is a deliberate decision and ends up in the system log; a root shell makes every typo a root typo.

The redirection trap

This one catches everybody once:

echo "maintenance tonight 22:00" > /etc/motd-maintenance
sudo echo "maintenance tonight 22:00" > /etc/motd-maintenance
bash: /etc/motd-maintenance: Permission denied
bash: /etc/motd-maintenance: Permission denied

The error is identical, and it comes from bash both times. Redirections are handled by your shell, running as you, before sudo starts, so sudo never even ran (it didn't ask for a password). The usual fix is to let a root process open the file, with tee:

echo "maintenance tonight 22:00" | sudo tee /etc/motd-maintenance > /dev/null

tee writes its input to the file and also to its output, which > /dev/null discards. (I didn't run this one, since it needs a password. Pipes and /dev/null are covered in Redirection and Pipes.)

Where you'll use this

Making a script executable, and keeping it that way in Git

You've seen the local fix: chmod u+x deploy.sh. The version of this problem that wastes the most time happens in CI. A script committed from a Windows machine, or created without +x, is stored in Git as non-executable, so the pipeline fails with Permission denied even though it works on your laptop. Git records the executable bit, and you can set it directly in the index:

mkdir -p ~/practice/perms/repo && cd ~/practice/perms/repo
git init -q
printf '#!/usr/bin/env bash\necho "running tests"\n' > run-tests.sh
git add run-tests.sh
git ls-files -s run-tests.sh
git update-index --chmod=+x run-tests.sh
git ls-files -s run-tests.sh
100644 3e12482344f664c3ef7be82484de9c1d68d35d2f 0       run-tests.sh
100755 3e12482344f664c3ef7be82484de9c1d68d35d2f 0       run-tests.sh

100644 is a normal file and 100755 an executable one. After the next commit, every checkout, including the CI runner's, gets an executable script. (Also run chmod +x run-tests.sh locally; update-index changes only what Git stores.)

Fixing SSH's "UNPROTECTED PRIVATE KEY FILE"

SSH refuses to use a private key that other users could read. Here is what that looks like, using a freshly generated key and ssh-keygen -y (which loads the private key to print its public half, applying the same check as ssh):

mkdir -p ~/.ssh && chmod 700 ~/.ssh
ssh-keygen -t ed25519 -N '' -C 'deploy@example.com' -f ~/.ssh/id_ed25519 -q
ls -l ~/.ssh
chmod 644 ~/.ssh/id_ed25519
ssh-keygen -y -f ~/.ssh/id_ed25519
total 8
-rw------- 1 sid sid 411 Nov  9 21:57 id_ed25519
-rw-r--r-- 1 sid sid 100 Nov  9 21:57 id_ed25519.pub
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/home/sid/.ssh/id_ed25519' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/home/sid/.ssh/id_ed25519": bad permissions

ssh-keygen itself created the key as 600. The 644 here simulates what happens when a key is copied from a USB stick, a Windows folder, or an archive. The fix:

chmod 600 ~/.ssh/id_ed25519
ssh-keygen -y -f ~/.ssh/id_ed25519
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFYTsBZlY8ri7uZUzeGvXzRLxLIAw0kiD9nFto0Cl7OV deploy@example.com

The full set of permissions SSH expects:

PathMode
~/.ssh700
~/.ssh/id_ed25519 (any private key)600
~/.ssh/id_ed25519.pub644
~/.ssh/authorized_keys600
~/.ssh/config600

On the server side, sshd also ignores authorized_keys if your home directory or ~/.ssh is writable by group or others (its StrictModes setting), and key logins fail with a plain "Permission denied (publickey)" and no hint why. If key login suddenly stops working after someone ran a recursive chmod on a home directory, check those modes first. I didn't have an SSH server in my test environment, so that part is described, not demonstrated.

A shared team directory

Putting the pieces together, here is a directory where a team can create and edit each other's files. The group is users because that's the group my test account had; on a real server you'd create one (sudo groupadd ops) and add the team to it.

cd ~/practice/perms
(umask 002; touch team-reports/q4-forecast.csv)
ls -l team-reports
total 4
drwxr-sr-x 2 sid users 4096 Nov  9 21:56 drafts
-rw-r--r-- 1 sid users    0 Nov  9 21:56 q3-summary.csv
-rw-rw-r-- 1 sid users    0 Nov  9 21:56 q4-forecast.csv

Three settings work together:

  1. Group ownership (chgrp users team-reports) decides who the team is.
  2. setgid on the directory (chmod 2775) makes every new file and subdirectory belong to that group automatically.
  3. A umask of 002 for team members makes new files group-writable. Compare q3-summary.csv, created with umask 022, which teammates can read but not change, with q4-forecast.csv, created with 002.

Forgetting the third point is the usual reason a "shared" directory works for reading and fails for editing.

Other common situations:

  • Secrets on disk. .env files, database passwords (~/.pgpass), API tokens and TLS private keys should be 600 and owned by the account that uses them. Some tools, like ssh and PostgreSQL's psql for .pgpass, refuse to use them otherwise.
  • Web roots after a deploy. Directories 755, files 644, owned by the deploy user, with only upload or cache directories writable by the web server. Finding Files with find, locate, and xargs shows the find -type d/-type f commands for fixing a whole tree.
  • Log files. 640 with a group like adm lets administrators read logs without sudo. Note 4(adm) in my id output above: that's what Ubuntu uses it for.

Common mistakes

chmod -R 644 on a directory

You want every file to be 644, so you run it recursively, and lock yourself out of the whole tree:

mkdir -p site/css site/img
touch site/index.html site/css/main.css site/img/logo.png
chmod -R 644 site
ls -l site
cd site
chmod: cannot access 'site/img': Permission denied
chmod: cannot access 'site/css': Permission denied
chmod: cannot access 'site/index.html': Permission denied
ls: cannot access 'site/img': Permission denied
ls: cannot access 'site/css': Permission denied
ls: cannot access 'site/index.html': Permission denied
total 0
d????????? ? ? ? ?            ? css
d????????? ? ? ? ?            ? img
-????????? ? ? ? ?            ? index.html
bash: cd: site: Permission denied

chmod changed site itself first, which removed its x bit, and then couldn't get into it to change anything else. The fix is the capital X form, which gives directories x back and leaves files alone:

chmod -R u=rwX,go=rX site
ls -lR site
site:
total 8
drwxr-xr-x 2 sid sid 4096 Nov  9 21:57 css
drwxr-xr-x 2 sid sid 4096 Nov  9 21:57 img
-rw-r--r-- 1 sid sid    0 Nov  9 21:57 index.html
 
site/css:
total 0
-rw-r--r-- 1 sid sid 0 Nov  9 21:57 main.css
 
site/img:
total 0
-rw-r--r-- 1 sid sid 0 Nov  9 21:57 logo.png

chmod +w and chmod -w respect the umask

Leaving out the "who" (u, g, o, a) does not mean "everyone". It means "everyone, minus the bits blocked by the umask":

chmod 444 report.csv
chmod +w report.csv
ls -l report.csv
chmod a+w report.csv
ls -l report.csv
chmod -w report.csv
ls -l report.csv
-rw-r--r-- 1 sid sid 24 Nov  9 21:56 report.csv
-rw-rw-rw- 1 sid sid 24 Nov  9 21:56 report.csv
chmod: report.csv: new permissions are r--rw-rw-, not r--r--r--
-r--rw-rw- 1 sid sid 24 Nov  9 21:56 report.csv

With umask 022, chmod +w only gave the owner write access, and chmod -w only removed the owner's, leaving group and others writable. GNU chmod warns about it, which is the only clue you get. Always say who: chmod a-w, chmod go-w.

Reaching for chmod 777

777 makes a file or directory writable by every account on the system, including any compromised web application running as www-data. It usually "fixes" a permissions error that was really an ownership problem: the right answer is to give the file to the right user or group (chown, chgrp, a shared group with setgid) and use 755/644 or 775/664. If you find yourself typing 777, stop and work out who actually needs access.

Expecting chmod to work on Windows drives in WSL

Under WSL, files under /mnt/c live on the Windows filesystem, which doesn't store Linux permissions by default. When I listed a project folder under /mnt/c during testing, every file showed as -rwxrwxrwx, and that's what chmod keeps coming back to. If permissions matter, for SSH keys or for scripts, keep those files in your Linux home directory (~), not under /mnt/c.

Summary

  • ls -l shows type, then three rwx sets for user, group and others; Linux applies exactly one set: user if you own the file, else group, else others.
  • On files, r/w/x mean read, modify, execute. On directories, r lists names, x lets you enter and reach files by name, and w (with x) lets you create, delete and rename entries. Deleting a file is a write to its directory.
  • Symbolic chmod (u+x, go-w, u=rw,g=r,o=) changes specific bits; octal chmod (644, 600, 755, 750, 700) sets them all. X adds execute only to directories, so chmod -R u=rwX,go=rX is the safe recursive form.
  • New files get 666 and directories 777, minus the umask: 022 gives 644/755, 002 group-writable, 077 private. Files never start executable.
  • Only root can change a file's owner; the owner may change its group to any group they belong to. Group membership changes apply at the next login.
  • setuid (4) runs a program as its owner (passwd, sudo), setgid (2) on a directory makes new files inherit its group, sticky (1) stops users deleting each other's files (/tmp is 1777).
  • sudo runs one command as root using your own password. Redirections run before sudo, so use | sudo tee file to write root-owned files.
  • SSH requires ~/.ssh at 700 and private keys at 600; fix "UNPROTECTED PRIVATE KEY FILE" with chmod 600.

Exercises

Work in ~/practice/perms.

Exercise 1: Two ways to the same modeeasy

Create app.conf and give it these permissions: owner read and write, group read, others nothing. Do it once with symbolic mode and once (after resetting it to 644) with octal mode, and check with stat each time.

Show Solution
printf 'db_host=db.internal\n' > app.conf
chmod u=rw,g=r,o= app.conf
stat -c '%a %A %n' app.conf
chmod 644 app.conf
chmod 640 app.conf
stat -c '%a %A %n' app.conf
640 -rw-r----- app.conf
640 -rw-r----- app.conf

rw- is 4+2 = 6, r-- is 4, and --- is 0. The = operator in symbolic mode is what makes it set an exact state, like octal, instead of adding or removing bits.

Exercise 2: Permission denied on a new scripteasy

Create backup.sh containing a #!/usr/bin/env bash line and an echo. Run it as ./backup.sh, read the error, fix it so that only you can execute it, and run it again.

Show Solution
printf '#!/usr/bin/env bash\necho "Backing up the billing database..."\n' > backup.sh
./backup.sh
chmod u+x backup.sh
./backup.sh
ls -l backup.sh
bash: ./backup.sh: Permission denied
Backing up the billing database...
-rwxr--r-- 1 sid sid 62 Nov  9 21:57 backup.sh

New files are created without execute permission, whatever your umask. u+x adds it for the owner only; group and others can still read the script but not run it directly.

Exercise 3: Secrets that are private from birthmedium

Create a directory secrets, then, in a single subshell, write API_TOKEN=abc123 to secrets/api.env and create a subdirectory secrets/certs so that both are private to you as they are created, without running chmod on them afterwards.

Hint

Change the umask inside ( ... ) so it only affects those commands.

Show Solution
mkdir -p secrets
(umask 077; printf 'API_TOKEN=abc123\n' > secrets/api.env; mkdir secrets/certs)
ls -l secrets
total 8
-rw------- 1 sid sid   17 Nov  9 21:57 api.env
drwx------ 2 sid sid 4096 Nov  9 21:57 certs

With umask 077, the requested 666 becomes 600 and 777 becomes 700. Creating secrets private from the start matters: with chmod afterwards, there's a short window in which the file exists with your normal 644 permissions.

Exercise 4: A private team foldermedium

Create shared/ops owned by group users (or another group you belong to; check with groups). Members of the group must be able to create and edit files there, new files must automatically belong to the group, and other users must have no access at all. Create runbook.md inside it the way a team member with the right umask would, and verify.

Show Solution
mkdir -p shared/ops
chgrp users shared/ops
chmod 2770 shared/ops
(umask 007; touch shared/ops/runbook.md)
ls -ld shared/ops
ls -l shared/ops
drwxrws--- 2 sid users 4096 Nov  9 21:57 shared/ops
total 0
-rw-rw---- 1 sid users 0 Nov  9 21:57 runbook.md

2770 is setgid (2) plus rwx for owner and group and nothing for others. The setgid bit gave runbook.md the group users, and umask 007 made it group-writable and invisible to others.

Exercise 5: Rescue a tree after chmod -R 644hard

Recreate the broken site tree from the common mistakes section (mkdir -p site/css site/img, three files, then chmod -R 644 site). Using a single chmod command, restore it so that every directory is 755 and every file 644, without making any file executable.

Hint

Which symbolic permission letter adds execute to directories only?

Show Solution
chmod -R u=rwX,go=rX site
ls -lR site
site:
total 8
drwxr-xr-x 2 sid sid 4096 Nov  9 21:57 css
drwxr-xr-x 2 sid sid 4096 Nov  9 21:57 img
-rw-r--r-- 1 sid sid    0 Nov  9 21:57 index.html
 
site/css:
total 0
-rw-r--r-- 1 sid sid 0 Nov  9 21:57 main.css
 
site/img:
total 0
-rw-r--r-- 1 sid sid 0 Nov  9 21:57 logo.png

This works even though site is unreadable at the start, because chmod -R changes the top directory before descending into it. u=rwX gives site back its x bit (it's a directory), so chmod can then enter it and fix everything below. The files had no execute bit, so X gives them none.


Tested with: GNU Bash 5.2.21 on Ubuntu 24.04 (WSL2), November 2026, with GNU coreutils 9.4, OpenSSH 9.6p1 (ssh-keygen) and Git 2.43.0. Every command shown with output was run as an ordinary user (no root access) with that user's real group memberships, in a fresh, empty home directory. The rm confirmation prompt was captured through a pseudo-terminal, since rm only asks when it's attached to one. Not run: the sudo chown, sudo usermod and sudo tee commands (the test account had no sudo password, as sudo -n true shows), and the SSH server-side StrictModes behaviour, because no SSH server was available.

Was this page helpful?
The Dark Debugger

Written by The Dark Debugger

Engineer documenting the things that took me too long to learn, so they take you less time. Every Bash tutorial here is tested end to end before publishing.