Skip to content

Computing foundations · Part 10 of 10

Linux file permissions explained: users, groups, rwx and chmod

Every file has an owner, a group and three sets of read, write and execute bits. Read ls -l, use chmod and chown and follow least privilege.

· 2 min read

operating-systemslinux

On Linux every file and directory has an owner, a group and a set of permission bits that decide who can do what. Understanding them solves a large share of "permission denied" errors, and prevents risky fixes.

Reading ls -l

Code
-rwxr-x--- 1 alice developers 2048 Jan 15 10:00 deploy.sh

The first character is the type (- file, d directory, l symbolic link). The next nine are three groups of three, for the owner, the group and others:

  • r read, w write, x execute.
  • rwx for the owner, r-x for the group and --- for everyone else in this example.

Octal notation

Each permission has a value: read 4, write 2, execute 1. Add them for each of the three groups.

  • rwx = 7, r-x = 5, r-- = 4, --- = 0.
  • So rwxr-x--- is 750.
shell
chmod 750 deploy.sh        # owner rwx, group r-x, others nothing
chmod u+x script.sh        # add execute for the owner
chmod g-w shared.txt       # remove write for the group

What the bits mean on directories

  • r: list the names inside the directory.
  • w: create, delete and rename entries inside (needs x as well).
  • x: enter the directory and access items inside it by name.

A directory without x is unusable even if it is readable, a common source of confusion.

Owners and groups

shell
chown alice:developers deploy.sh      # change owner and group (needs privileges)
chgrp developers deploy.sh            # change only the group
id                                    # show your user and groups

New files get permissions from the process's umask. With a umask of 022, files are created as 644 and directories as 755.

Good practice

  1. Follow least privilege: give the minimum required, and prefer groups over opening a file to everyone.
  2. Avoid chmod 777. It lets any user modify the file and usually hides the real problem, which is the wrong owner or group.
  3. Keep secrets such as private keys at 600 and their directory at 700. SSH refuses keys that are too open.
  4. Run services as dedicated non-root users.
  5. Special bits such as setuid exist, but they are powerful. Use them rarely and review them.

Takeaway

Read the three triplets, think in octal, fix ownership before loosening permissions and keep secrets private.

A knowledge article, drafted with AI assistance and checked with automated tests. It is general background, not a description of my own experience or of any client's work.

Comments

Leave a comment

No account or email needed. Comments appear after review. A one-way hash of your IP address is kept for 30 days to fight spam, then erased.