Skip to content
Laviyx Learn

Try

Linux and Bash course menu

Linux File Permissions Explained: chmod and chown

Read rwx permissions, change them with chmod using letters and numbers like 755 and 644, change owners with chown, understand umask, and fix Permission denied errors.

Intermediate7 min readUpdated
On this page

Linux is a multi-user system, so every file has an owner and a set of permissions. "Permission denied" is one of the first errors everyone meets.

Reading permissions#

Terminal
echo "hello" > notes.txt
mkdir reports
ls -ld notes.txt reports
Output
-rw-rw-r-- 1 asha asha    6 Nov  5 06:26 notes.txt
drwxrwxr-x 2 asha asha 4096 Nov  5 06:26 reports

The first column is broken down like this:

Text
-  rw-  rw-  r--
|   |    |    |
|   |    |    +-- others: everyone else
|   |    +------- group
|   +------------ user: the owner
+---------------- type: - file, d directory, l link
LetterOn a fileOn a directory
r readRead the contentList the names inside
w writeChange the contentCreate, delete and rename inside
x executeRun it as a programEnter it with cd
-Permission not givenPermission not given

chmod with letters#

chmod changes permissions. Choose who (u, g, o, or a for all), + or -, and the permission:

Terminal
echo 'echo "It runs"' > run.sh
./run.sh
chmod u+x run.sh
./run.sh
ls -l run.sh
Output
bash: ./run.sh: Permission denied
It runs
-rwxrw-r-- 1 asha asha 15 Nov  5 06:26 run.sh

The first attempt failed because the script was not executable.

Terminal
chmod o-r notes.txt
chmod g-w notes.txt
ls -l notes.txt
Output
-rw-r----- 1 asha asha 6 Nov  5 06:26 notes.txt

chmod with numbers#

Each permission has a value, and you add them up for each of user, group and others.

ValuePermission
4r
2w
1x

So rw- is 6, r-x is 5, and rwx is 7.

NumberLettersCommon use
755rwxr-xr-xPrograms, scripts, folders
644rw-r--r--Normal files
700rwx------Private folders, such as ~/.ssh
600rw-------Private files, such as SSH keys
Terminal
chmod 755 run.sh
chmod 600 notes.txt
stat -c '%a %A %n' run.sh notes.txt
Output
755 -rwxr-xr-x run.sh
600 -rw------- notes.txt

stat -c '%a' prints the number form, which is handy in scripts.

Changing a whole folder#

-R is recursive, but files and folders usually need different permissions. This is the safe pattern:

Terminal
mkdir -p site/css
touch site/index.html site/css/main.css
find site -type d -exec chmod 755 {} +
find site -type f -exec chmod 644 {} +
ls -lR site | grep -E '^[d-]'
Output
drwxr-xr-x 2 asha asha 4096 Nov  5 06:26 css
-rw-r--r-- 1 asha asha    0 Nov  5 06:26 index.html
-rw-r--r-- 1 asha asha 0 Nov  5 06:26 main.css

Changing the owner#

chown changes the owner and group, and it needs sudo:

Terminal
sudo chown root:root notes.txt
ls -l notes.txt
cat notes.txt
Output
-rw------- 1 root root 6 Nov  5 06:26 notes.txt
cat: notes.txt: Permission denied

Now asha cannot read it, because it belongs to root, with mode 600.

Terminal
sudo chown asha:asha notes.txt
cat notes.txt
Output
hello

chgrp changes only the group, and chown -R changes a whole folder.

umask#

New files get default permissions, set by the umask. It lists the permissions to remove from 666 for files and from 777 for folders:

Terminal
umask
Output
0002

With a umask of 0002, files start as 664 and folders as 775. Many servers use 0022, which gives 644 and 755.

Special permissions#

These are mainly useful to know for interviews:

BitExampleEffect
setuid/usr/bin/passwdThe program runs with the owner's rights
setgidShared foldersNew files inherit the folder's group
sticky/tmpOnly a file's owner can delete it
Terminal
ls -ld /tmp
ls -l /usr/bin/passwd
Output
drwxrwxrwt 13 root root 4096 Nov  5 06:26 /tmp
-rwsr-xr-x 1 root root 64152 May 30  2024 /usr/bin/passwd

The t shows the sticky bit, and the s shows setuid.

Common mistakes#

Practice#

Practice exercise

Make secret.txt readable and writable only by you, make app.sh executable by everyone but writable only by you, and print both modes as numbers.

Show solution
Terminal
chmod 600 secret.txt
chmod 755 app.sh
stat -c '%a %n' secret.txt app.sh
Output
600 secret.txt
755 app.sh

Key takeaway#

Next: Users, Groups and sudo.

Finished this lesson?