Chapter 1 · Lesson 6 of 19
Server basics

Linux file permissions explained

Linux file permissions control who may read, write or execute each file, set separately for the owner, the group and everyone else.

By the end you will

Read an rwx permission string and set modes and owners with chmod and chown.

Time
14 min
Level
Beginner
Check
4 questions
You need
  • A Linux terminal and a test file

Sooner or later a deploy fails with Permission denied: a web server cannot read your files, a script will not run, or a container cannot write to a mounted folder. Almost every time the cause is who owns the file and which three-bit pattern it carries. Learn to read that pattern and these errors become a two minute fix.

How it works#

Every file and folder has one owner (a user), one group and a set of permission bits. The bits are split into three classes: the owner, the group, and everyone else (others). When a process wants to open a file, the kernel picks one class and uses only that class. If you are the owner, the owner bits apply, even if the group bits are more generous. If you are not the owner but are in the group, the group bits apply. Otherwise the others bits apply. Root skips these checks.

Each class has three rights, read (r), write (w) and execute (x). Their meaning is different for files and folders:

RightOn a fileOn a folder
rRead the contentsList the names inside
wChange the contentsCreate, rename or delete entries inside
xRun it as a programEnter it and reach files by name

That last column explains many surprises. To delete a file you need write on the folder that holds it, not on the file. To open /srv/app/data/file.txt you need x on every folder along the path.

Reading the permission string#

See file permissions in the glossary for short definitions. Run this in any folder:

bash
ls -l

A line looks like -rwxr-xr-- 1 alice dev 120 Nov 27 notes.sh. The first character is the type (- for a file, d for a directory, l for a symbolic link). The next nine characters are three groups of three: the owner, the group, and others. A - means that right is missing. After the link count come the owner (alice) and the group (dev).

Numbers instead of letters#

Each right has a value: read is 4, write is 2, execute is 1. Add them up for each class.

  • 7 = 4 + 2 + 1 (read, write, execute)
  • 6 = 4 + 2 (read, write)
  • 5 = 4 + 1 (read, execute)
  • 4 = read only
  • 0 = nothing

So 644 means the owner can read and write, everyone else can only read. 755 is the usual setting for scripts and folders. 600 is right for secrets such as private keys.

A worked example: deploy.sh is created with mode -rw-r--r--. You run ./deploy.sh and get Permission denied, because nobody has execute. You run chmod u+x deploy.sh, and ls -l now shows -rwxr--r--. The script runs for you. A teammate in another group can still read it but not run it, which is what you want until you decide otherwise.

Step 1: Change permissions with chmod#

bash
chmod 644 notes.txt
chmod 755 deploy.sh
chmod u+x deploy.sh

The last form adds execute for the owner (u) only. Letters are u (owner), g (group), o (others) and a (all). + adds a right, - removes it and = sets it exactly.

What you should see: no output when it works. Run ls -l to see the change.

Warning: do not use chmod 777. It gives every user write access. Also be careful with -R, which changes every file inside a folder, and makes plain files executable if you use a blanket 755.

Step 2: Change the owner with chown#

bash
sudo chown alice:dev notes.txt

This sets the owner to alice and the group to dev. Only root can give a file to another user. Double check the path before you add -R, because a recursive chown on the wrong folder, such as /, can break the system.

What you should see: no output. ls -l notes.txt now shows alice dev.

Step 3: Fix a folder so a service can write to it#

A common case: a service running as user www-data must write to /srv/app/uploads.

bash
sudo chown -R www-data:www-data /srv/app/uploads
sudo chmod 750 /srv/app/uploads

750 lets the owner do everything, the group read and enter, and others nothing. Prefer giving the service ownership over opening the folder to everyone.

What you should see: no output. ls -ld /srv/app/uploads shows drwxr-x--- and the owner www-data.

Check it worked#

bash
ls -l notes.txt
stat -c '%a %U:%G %n' notes.txt

After chmod 644 you should see -rw-r--r-- in the first command. The second prints the number, owner and group in one line, for example 644 alice:dev notes.txt. To test as another user, run sudo -u www-data cat notes.txt: it should print the file or fail with Permission denied, which tells you what that user can really do.

Edge cases and gotchas#

  • Folders need x to be entered. A folder at 644 cannot be opened with cd. Use 755 or 750 for folders. A common cleanup uses chmod -R u=rwX,go=rX where capital X sets execute only on folders and already executable files.
  • Every folder on the path counts. A file can be 644 and still unreadable if a parent folder blocks you. Run namei -l /full/path/to/file to see each step.
  • Docker volumes use numeric IDs. (See Docker volumes and persistent data.) A bind mounted folder belongs to a numeric user ID on the host, and the container user may have a different ID with the same name. If the container gets Permission denied, compare the IDs with ls -ln and id inside the container.
  • Copying changes ownership. cp makes you the owner of the copy, and cp -p or rsync -a keeps permissions. Restoring a backup as the wrong user is a classic way to break an app.
  • Umask sets the default. New files get their mode from the umask, often 022, which gives 644 for files and 755 for folders. A different umask in a service explains files that are unreadable to others.

Troubleshooting#

SymptomLikely causeFix
Permission denied when running ./deploy.shThe file lacks the execute bitRun chmod u+x deploy.sh
Permission denied on cd folderThe folder has no execute bit for youAdd x with chmod 755 folder if sharing is intended
chown: changing ownership of 'file': Operation not permittedOnly root can change an ownerAdd sudo
rm: cannot remove 'file': Permission deniedNo write on the containing folderCheck ls -ld on the folder
Web server shows 403 or cannot read filesThe server user cannot read the file or a parent folderCheck ownership and the x bit on parent folders
Container gets Permission denied on a mounted folderHost owner and container user IDs differCompare ls -ln with id in the container and chown to match
chmod: cannot access: No such file or directoryWrong pathCheck with ls and pwd
SSH ignores your key fileMode is too openchmod 600 the key and chmod 700 ~/.ssh

Real-world scenario#

A web app returns 403 for uploaded images. The files exist, and nginx is running. Mara runs namei -l /srv/app/uploads/photo.jpg and sees that /srv/app is mode drwx------ owned by deploy. nginx runs as www-data, which cannot enter that folder.

She does not reach for 777. She runs sudo chmod 750 /srv/app, then sudo chgrp www-data /srv/app, so the web server user is in the group that may enter. She repeats sudo -u www-data cat /srv/app/uploads/photo.jpg and the file prints.

The fix was one folder and one group, not a blanket change.

Quick reference#

ModeMeaningCommon use
600Owner read and writePrivate keys, secrets
644Owner read and write, others readConfig and web files
700Owner only, with enter on foldersPrivate folders such as ~/.ssh
750Owner all, group read and enterShared service folders
755Everyone can read and runScripts and public folders

Show a mode as a number: stat -c '%a %n' file.

Security and safety notes#

  • Never run chmod -R 777 or chown -R on /, /etc or /usr. It can break the system and open security holes.
  • Keep private keys at 600. SSH refuses keys that other users can read, as in log in with SSH keys.

Next#

Essential Linux commands for servers.

Check what you learned4 questions
1What does chmod 644 file give?
2What does the string -rwxr-xr-- mean for others?
3Why avoid chmod 777?
4A folder is mode 644. You own it. What happens when you run cd into it?

Frequently asked questions

What does chmod 755 mean?

The owner can read, write and execute (7). The group and everyone else can read and execute (5) but not write.

Why should I never use chmod 777?

It lets every user on the machine change or replace the file. Give the smallest permission that works instead.

What does the x bit mean on a folder?

On a folder, x means you may enter it and reach the files inside by name. Without x on a folder you cannot cd into it, even if you can read its listing.

Why can I not delete a file I own?

Deleting removes a name from the folder, so it needs write permission on the folder, not on the file. Check ls -ld on the folder.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios