Skip to content
Visual explainer 4h to learn 4 steps

Filesystem, users & permissions

Navigate the filesystem and fix ownership and permission problems without guessing.

By Invicly Tech, , in Full DevOps with Python. About 12 minutes to read.

Topic 1 of 34

In short

Every file in Linux has an owner, a group and three sets of permission bits. Most 'permission denied' errors come from the wrong owner or a missing bit, so reading ls -l output fixes them fast.

Think of it like this: Like an office key card that opens different doors for you, your team and visitors.

Explained simply

Linux keeps everything in one big folder tree that starts at / (called the root directory). There are no drive letters like C:. A second disk or a USB stick simply shows up as a folder somewhere in that tree, for example /mnt/data. Config lives in /etc, logs in /var/log, and your apps usually go in /srv or /opt.

Every file carries a small label: who owns it, which group it belongs to, and what each kind of person may do with it. When a program opens a file, Linux compares that label with the user the program runs as. If they don't match, you get "Permission denied". The file is fine; the wrong person is knocking.

A user is just an account with a number called a UID (user ID). Root has UID 0 and can do almost anything. People and services each get their own account, so a bug in the web server can't wipe your home folder. Groups bundle users together, so you can give a whole team access in one step.

In DevOps you meet this daily: a deploy script that can't write to /srv/app, an SSH key that is "too open", a disk that is suddenly full. Once you know where things live and who owns them, these stop being mysteries and become one-line fixes.

Press Play on a diagram to walk through it one step at a time.

MapReading -rwxr-x--- (mode 750)
-rwxr-x--- deploy:web
  • Owner rwx = 7
    • r read = 4
    • w write = 2
    • x execute = 1
  • Group r-x = 5
    • Members of group web
    • Can read and run
  • Others --- = 0
    • Everyone else
    • No access at all
FlowFixing permission denied
  1. 1
    Read the error

    Note the exact path and the user running the command.

  2. 2
    Check with ls -l

    See owner, group and mode of the file and parent dirs.

  3. 3
    Check your identity

    Run id to list your user and groups.

  4. 4
    Fix owner or mode

    Use chown for wrong owner, chmod for missing bits.

  5. 5
    Retest as that user

    Use sudo -u to confirm the fix for the real user.

FlowHow the kernel checks one file access
  1. 1
    Process opens file

    It runs as a UID with some groups

  2. 2
    Is it root?

    Yes: read and write are allowed

  3. 3
    Is it the owner?

    Yes: use the owner bits only

  4. 4
    In the file's group?

    Yes: use the group bits only

  5. 5
    Everyone else

    Use the others bits

  6. 6
    Allow or deny

    Deny returns Permission denied

MapWhere things live on a Linux server
/
  • /etc
    • System and app config
    • passwd, group, sudoers
  • /var
    • /var/log for logs
    • /var/lib for app and database data
  • /home
    • One folder per person
    • ~/.ssh holds SSH keys
  • /srv and /opt
    • Your deployed apps
    • Data served by the machine
  • /usr
    • Installed programs
    • /usr/local for hand-installed tools
  • /tmp
    • Scratch files
    • Mode 1777, often wiped on reboot

In depth

How Linux decides: owner, then group, then others

When a process opens a file, the kernel compares the process's user ID and group IDs with the file. It checks in a fixed order and stops at the first match. If you are the owner, only the owner bits count, even if the group bits would allow more. If you are not the owner but are in the file's group, the group bits apply. Otherwise the others bits apply.

Root (UID 0) skips read and write checks entirely. That is why running everything as root hides permission bugs until production, where the service runs as a normal user and suddenly fails.

  • The first match wins, even if a later class is looser
  • Mode 077 locks out the owner but not the group
  • Root bypasses read and write checks
  • id shows your UID, GID and every group you are in

umask: the default for new files

New files don't get permissions at random. Programs ask for 666 for files and 777 for directories, and the umask removes bits from that request. With the common umask 022, new files become 644 (rw-r--r--) and new directories 755. With umask 027, others get nothing: files become 640 and directories 750.

Run umask to see your current value. A service can set its own with UMask=0027 in its systemd unit, which is handy for apps that write files other users should not read.

CompareTwo common umask values
umask 022
  • Files: 666 becomes 644
  • Directories: 777 becomes 755
  • Others can still read
umask 027
  • Files: 666 becomes 640
  • Directories: 777 becomes 750
  • Others get nothing

Special bits: setuid, setgid and sticky

Beyond rwx there are three extra bits. setuid (4000) on a program makes it run as the file's owner; /usr/bin/passwd uses it so normal users can update the root-owned password file. setgid (2000) on a directory makes new files inherit the directory's group, which is perfect for shared team folders. The sticky bit (1000) on a directory means only a file's owner can delete it; /tmp has mode 1777 for exactly this reason.

In ls -l they show up as s or t in the execute position, for example drwxrwsr-x for a setgid folder.

  • chmod 2775 /srv/shared turns on setgid for a folder
  • chmod +t /srv/drop turns on the sticky bit
  • List setuid programs: find / -perm -4000 -type f 2>/dev/null
CompareThe three special bits
setuid (4000)
  • Set on programs
  • Runs as the file's owner
  • Example: /usr/bin/passwd
  • Shown as s in the owner x slot
setgid (2000)
  • Here: set on directories
  • New files inherit the folder's group
  • Good for shared team folders
  • Shown as s in the group x slot
Sticky (1000)
  • Set on directories
  • Only a file's owner can delete it
  • Example: /tmp is 1777
  • Shown as t in the others x slot

Users, groups and sudo in practice

Accounts live in /etc/passwd, groups in /etc/group, and password hashes in /etc/shadow, which only root can read. For services, create accounts that cannot log in: useradd --system --shell /usr/sbin/nologin app. That account can own files and run a service, but nobody can open a shell as it.

sudo rules live in /etc/sudoers and /etc/sudoers.d/. Always edit them with visudo, which checks the syntax before saving; a broken file can lock everyone out of sudo. You can grant very narrow rights, such as letting the deploy user restart one service and nothing else.

  • deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart app
  • sudo -l lists what you are allowed to run
  • getent group web shows the members of a group

Finding what fills the disk

A full disk breaks databases, logs and deploys. Start wide with df -h to see which filesystem is full. Then narrow down: sudo du -xh --max-depth=1 /var | sort -h shows the biggest folders, and -x stays on that one filesystem. Repeat one level deeper until you find the culprit, often old logs, Docker images or a crash dump.

If df says the disk is full but du can't find the space, a deleted file may still be held open by a running process. sudo lsof +L1 lists such files; restarting that process frees the space.

Learn it in 4 short steps

About 1h each. Track them with a timer in your plan.

  1. 1
    Navigate with ls, cd, find and du~1h
  2. 2
    Read rwx bits and octal modes~1h
  3. 3
    Fix ownership with chown and chmod~1h
  4. 4
    Manage users, groups and sudo~1h

Key points

  • Permissions are read, write and execute for owner, group and others.
  • Octal modes add up: r=4, w=2, x=1, so 640 means rw-r-----.
  • Directories need x to be entered and r to list their contents.
  • Give access through groups and sudo rules instead of chmod 777.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
ls -l /srv/app/config.yml
sudo chown deploy:web /srv/app/config.yml
sudo chmod 640 /srv/app/config.yml
find /srv/app -type f -perm -o+w
du -sh /var/log/* | sort -h | tail -5
sudo usermod -aG web alice
id alice

Inspect, fix ownership and mode, and find world-writable files.

A shared folder for a team

Alice and Bob need to edit the same files in /srv/shared. Step 1: create a group and add both people. Step 2: give the folder to that group with mode 2770, so the group can read, write and enter, others get nothing, and the setgid bit makes every new file belong to devs. Step 3: both users log out and back in so the new group applies. To make new files group-writable too, each user sets umask 002 in their shell profile.

bash
sudo groupadd devs
sudo usermod -aG devs alice
sudo usermod -aG devs bob
sudo mkdir -p /srv/shared
sudo chown root:devs /srv/shared
sudo chmod 2770 /srv/shared
ls -ld /srv/shared
# drwxrws--- 2 root devs 4096 ... /srv/shared

SSH ignores your key

You copied your public key into ~/.ssh/authorized_keys on a server, but SSH still asks for a password. The SSH server refuses keys if your home folder, ~/.ssh or authorized_keys can be written by other people, because then someone else could add their own key. Step 1: look at the modes. Step 2: tighten them. Step 3: try again with ssh -v to see which key is offered and accepted.

bash
ls -ld ~ ~/.ssh ~/.ssh/authorized_keys
chmod go-w ~
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
# on your laptop, the private key must be private too
chmod 600 ~/.ssh/id_ed25519
ssh -v deploy@server 2>&1 | grep -i 'offering\|accepted'

Quick reference

  • ls -ld dir shows the directory itself, not what is inside it
  • stat -c '%a %U:%G %n' file prints the mode as numbers plus owner and group
  • umask 022 gives files 644 and dirs 755; umask 027 gives 640 and 750
  • chmod 2775 dir: new files inherit the directory's group (setgid)
  • /tmp is 1777: anyone can write, only the owner can delete (sticky bit)
  • ~/.ssh should be 700 and private keys 600, or SSH may refuse them
  • Edit sudo rules with visudo; put your own rules in /etc/sudoers.d/
  • df -h finds the full filesystem; du -xh --max-depth=1 finds the big folder

Common mistakes

  • Using chmod -R 777 to make an error go away; it opens files to everyone.
  • Forgetting -a in usermod -aG, which removes the user from other groups.
  • Expecting new group membership to apply before the user logs in again.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1A file is owned by alice with mode 077 (----rwxrwx). Can alice read it?

Question 2

2With umask 027, what mode does a newly created regular file get?

Question 3

3What does the setgid bit do on a directory?

Question 4

4df says / is 100% full, but du only finds half that much data. What is a likely cause?

Question 5

5Why should you edit sudo rules with visudo instead of a normal editor?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Create one file with your normal umask, then set umask 077 and create a second file. Compare both with ls -l and explain the difference.

  2. Exercise 2medium

    Create a system user called app that cannot log in. Make /srv/app owned by app and group web, so app can write, web can read, and others get nothing.

  3. Exercise 3hard

    Let the user deploy restart the app service without a password, but nothing else. Prove that other systemctl commands are still refused.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

What is the difference between being root and using sudo?
Root is the all-powerful account itself. sudo lets a normal user run single commands as root, and every use is logged, so teams can see who did what.
Is chmod 777 ever okay?
Almost never on a server. It lets every user and every compromised process change the file. Fix the owner or group instead.
What does the dot or plus after the mode in ls -l mean?
A dot (-rw-r--r--.) means the file has an SELinux security label. A plus (-rw-r--r--+) means extra access rules (ACLs) are set; view them with getfacl.
How do I see permissions as numbers instead of letters?
Run stat -c '%a %n' file. It prints something like 640 file.

Go deeper

ShareXLinkedInWhatsApp