Understanding File Permissions and Ownership
Learn how Linux decides who can read, write or run a file, and how to change permissions and ownership with chmod and chown.
Why permissions exist
Linux is a multi-user system. Even on a personal laptop, many programs run as different internal users. Permissions decide who may read a file, who may change it and who may run it. They protect your files from other users and protect the system from mistakes.
Owner, group and others
Every file and directory has an owner (a user) and a group (a named set of users). Permissions are set separately for three kinds of people:
- the owner
- the group
- others, which means everybody else
List files in long format to see all of this:
ls -l
-rwxr-xr-- 1 alex devs 2048 Oct 3 10:15 script.sh
Reading from the left:
-rwxr-xr--is the permission string, explained next.alexis the owner.devsis the group.2048is the size in bytes, followed by the date and the file name.
Reading the permission string
The first character is the type: - for a regular file and d for a directory. The next nine characters are three groups of three, for the owner, the group and others in that order.
- rwx r-x r--
| | | |
type owner group others
Each group uses the same three letters:
ris read: look at the contents.wis write: change the contents.xis execute: run the file as a program or script.-means that permission is not granted.
So rwxr-xr-- means: the owner can read, write and run; the group can read and run; others can only read.
The numeric form
Each permission has a number: read is 4, write is 2, execute is 1. Add the numbers for each group to get one digit.
rwx= 4 + 2 + 1 = 7r-x= 4 + 0 + 1 = 5r--= 4---= 0
Three digits give the owner, group and others. So rwxr-xr-- is 754.
Changing permissions with chmod
chmod ("change mode") sets permissions. With numbers:
chmod 755 script.sh
chmod 644 notes.txt
chmod 600 secret.txt
755: owner can read, write and run. Group and others can read and run. Typical for scripts and programs.644: owner can read and write. Group and others can only read. Typical for ordinary files.600: only the owner can read and write. Typical for private files such as keys.777: everyone can do everything. Avoid it.
The letter form
Letters are easier when you want to change one thing without touching the rest. The format is who, then + or -, then what.
- Who:
uuser (the owner),ggroup,oothers,aall. +adds a permission and-removes one.
chmod +x script.sh
chmod u+x script.sh
chmod go-w file.txt
chmod a+r file.txt
A very common error
You write a script and try to run it, and Bash answers Permission denied:
./backup.sh
bash: ./backup.sh: Permission denied
The file is missing the execute permission. Add it and run again:
chmod +x backup.sh
./backup.sh
Permissions on directories
For directories the letters mean something slightly different:
r: you can list the names inside.w: you can create, delete and rename files inside.x: you can enter the directory, for example withcd.
A directory without x cannot be entered, even if you can see that it exists.
Changing ownership: chown and chgrp
sudo chown bob file.txt
sudo chown bob:devs file.txt
sudo chgrp devs file.txt
sudo chown -R bob projects/
chown bob file.txtmakesbobthe owner.chown bob:devs file.txtsets the owner and the group together, writtenowner:group.chgrpchanges only the group.-Rapplies the change to a directory and everything inside it.
Only the administrator can give a file to another user, which is why these commands start with sudo.
A word about sudo
sudo means "run this command with administrator (root) rights". It asks for your password. Use it only when you understand what the command does, because root can overwrite or delete anything.
chmod 777 on something, it is almost always a bad idea. It lets every user read, write and run the file. Find the smallest permission that solves the problem instead.