Skip to content
Visual explainer 3h to learn 4 steps

Linux users, permissions & sudo

Read and set file permissions and audit who can run what with sudo.

By Invicly Tech, , in Cyber Security Fundamentals. About 10 minutes to read.

Topic 11 of 49

In short

Linux files have read, write and execute permissions for owner, group and others. Special bits like SUID change how programs run. Auditing permissions and sudo rules shows who can do what, which is the foundation of least privilege on Linux.

Explained simply

On Linux, every file and folder has an owner, a group and three sets of permissions: one for the owner, one for the group and one for everyone else. Each set says whether you may read (r), write (w) or execute (x). That small label decides who can see your secrets, change your config or run your programs.

For a defender, permissions are the first lock on every door. If a password file can be read by everyone, or a startup script can be changed by any user, an attacker who gets a low-level account can quickly become root (the all-powerful administrator). This jump is called privilege escalation.

sudo is the controlled way to borrow root power. Rules in the sudoers file say exactly which user may run which command as which account. Good rules are narrow. Bad rules, like letting a user run any editor as root, quietly hand over the whole machine.

Auditing means looking for these mistakes on purpose: files that anyone can write, programs with special bits, and sudo rules that are too wide. You fix them before an attacker finds them, and you watch the logs to see who used sudo and when.

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

FlowReading permissions
  1. Step 1
    Type

    File, directory or link.

  2. Step 2
    Owner rwx

    What the owner may do.

  3. Step 3
    Group rwx

    What the group may do.

  4. Step 4
    Other rwx

    What everyone else may do.

MapCommon Linux privilege escalation paths to audit
Low-privilege user
  • Weak file modes
    • World-writable scripts run by root
    • Readable /etc/shadow or keys
  • Special bits
    • Unusual SUID-root binary
    • Risky file capability
  • Loose sudo rules
    • Editor or shell allowed
    • Wildcard arguments
    • ALL with NOPASSWD
  • Writable paths
    • Writable folder in root's PATH
    • Writable cron or systemd file

In depth

What r, w and x mean on a directory

On a file, r lets you read the content, w lets you change it and x lets you run it. On a directory the meaning changes, and this trips up many people. r lets you list the names inside. x lets you enter the directory and reach files by name. w lets you create, rename and delete entries inside it, but only when x is also set.

The security surprise: deleting a file needs write permission on the directory, not on the file. So if a user can write to /srv/app, they can delete and replace a root-owned config file there, even if the file itself is mode 644. The sticky bit (as on /tmp) blocks this by allowing only a file's owner to delete it. Always check the folder as well as the file.

  • ls -ld dir shows the directory's own mode
  • Directory w + x means you can replace any file inside
  • namei -l /path/to/file shows the mode of every folder on the way

SUID, SGID, sticky and file capabilities

SUID (mode 4000) makes a program run as its owner instead of the person who started it. It exists for good reasons: passwd and sudo need root to do their job. But any bug in a SUID-root program, or a program that lets you start a shell, becomes a path to root. That is why defenders keep a list of SUID files and alert when a new one appears.

SGID (2000) does the same with the group, and on a directory makes new files inherit its group. The sticky bit (1000) on a shared directory stops users deleting each other's files. Linux ignores SUID on interpreted scripts, so attackers target binaries.

Modern Linux also has file capabilities: small slices of root power, such as cap_net_bind_service to open ports below 1024. They do not show in ls -l, so audit them separately with getcap.

  • find / -xdev -perm -4000 -type f lists SUID files
  • find / -xdev -perm -2000 -type f lists SGID files
  • getcap -r / 2>/dev/null lists files with capabilities

How a sudo rule is read

A sudoers line has four parts: who, on which host, as which user, and which commands. For example, alice ALL=(root) /usr/bin/systemctl restart nginx means alice, on any host, may run that one command as root. A name starting with % is a group, so %admins ALL=(ALL:ALL) ALL gives the admins group full root.

Two patterns are dangerous. First, programs that can start other programs: editors (vim), pagers (less), find, awk, python, tar and many more can open a root shell. Allowing them is the same as allowing everything. Second, wildcards in arguments. The sudoers manual warns that /usr/bin/cat /var/log/messages* also allows cat /var/log/messages /etc/shadow, because the star matches any extra text.

Safer options: allow exact commands, use sudoedit for editing files (it edits a copy as you, not as root), and avoid NOPASSWD for broad rules.

FlowParts of one sudoers rule
  1. 1
    Who

    alice, or %group for a group

  2. 2
    Host

    ALL means any machine

  3. 3
    Run as

    (root) or (ALL:ALL)

  4. 4
    Tags

    Optional, e.g. NOPASSWD:

  5. 5
    Command

    Full path plus exact arguments

Auditing permissions and sudo use

A permission audit is a repeatable set of searches. Look for world-writable files outside /tmp, files with no valid owner (often left after a user was deleted), SUID files, writable folders in root's PATH, and readable private keys. Save the results and compare them over time: a change is often more interesting than the list itself.

For sudo, list the rules with sudo -l -U username and read every file in /etc/sudoers.d/. Then check use: every sudo command is logged with the user, the working folder and the full command. On Debian and Ubuntu it lands in /var/log/auth.log, on RHEL in /var/log/secure, and everywhere in the journal. Alert on sudo by unexpected users, and on commands such as useradd, visudo or chmod u+s.

  • find / -xdev -type f -perm -0002 finds world-writable files
  • find / -xdev \( -nouser -o -nogroup \) finds orphaned files
  • journalctl _COMM=sudo shows sudo activity

Learn it in 4 short steps

About 45m each. Track them with a timer in your plan.

  1. 1
    Read rwx permissions and ownership~45m
  2. 2
    Change permissions with chmod and chown~45m
  3. 3
    Explain SUID, SGID and sticky bits~45m
  4. 4
    Audit sudoers rules~45m

Key points

  • Permissions apply to owner, group and others.
  • chmod changes permissions, chown changes ownership.
  • SUID runs a program as its owner, often root.
  • Overly broad sudo rules undermine least privilege.

Key terms

Tap a card to flip it. 0/4 known

Examples

bash
ls -l report.txt
# -rw-r----- 1 alice staff report.txt

chmod 640 report.txt   # owner rw, group r, others none
chown alice:staff report.txt
find / -perm -4000 -type f 2>/dev/null   # list SUID files

Finding a dangerous sudo rule

A developer asked to read logs, so someone added a rule letting them run less as root. Step 1: list the user's rules with sudo -l -U dev. Step 2: notice /usr/bin/less, a pager that can run a shell command from inside it, so this rule really grants a root shell. Step 3: replace it with a safer design: add dev to the adm group, which can read most logs in /var/log on Debian and Ubuntu, or use journalctl, which members of adm or systemd-journal can read. Step 4: check the syntax and confirm the old rule is gone.

bash
sudo -l -U dev
#   (root) NOPASSWD: /usr/bin/less /var/log/*   <- risky
sudo visudo -f /etc/sudoers.d/dev     # delete that line
sudo usermod -aG adm dev             # read logs without sudo
sudo visudo -c                        # syntax check all files
sudo -l -U dev                        # rule is gone

Baseline and diff SUID files

You want to know if anything new gains SUID power. Step 1: on a clean, freshly built server, save a sorted list of SUID and SGID files. Step 2: run the same search daily from cron or a systemd timer. Step 3: compare with the baseline. A new SUID file in /tmp, /home or /dev/shm is a strong sign of tampering. Step 4: send any difference to your log system as an alert, and update the baseline only after a planned package change.

bash
sudo find / -xdev -type f \( -perm -4000 -o -perm -2000 \) 2>/dev/null \
  | sort > /root/suid-baseline.txt
# later, daily:
sudo find / -xdev -type f \( -perm -4000 -o -perm -2000 \) 2>/dev/null \
  | sort > /tmp/suid-now.txt
diff /root/suid-baseline.txt /tmp/suid-now.txt && echo "no change"

Quick reference

  • Deleting a file needs write on its directory, not on the file
  • find / -xdev -perm -4000 -type f lists SUID binaries; keep a baseline
  • getcap -r / 2>/dev/null lists file capabilities that ls -l does not show
  • sudo -l -U user shows exactly what that user may run
  • Never allow editors, pagers, shells or interpreters in sudo rules
  • Use sudoedit to let people edit root-owned files safely
  • Sudo use is logged in auth.log (Debian/Ubuntu), secure (RHEL) and the journal
  • /etc/shadow is 640 root:shadow on Debian or 000 on RHEL; it must never be world-readable

Quick quiz

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

Question 1

1A config file is mode 644 owned by root, inside /srv/app which is mode 777. What can any local user do?

Question 2

2Why is the sudo rule bob ALL=(root) /usr/bin/vim risky?

Question 3

3Which command lists files with Linux capabilities set?

Question 4

4Where do Ubuntu servers log sudo commands by default?

0/4 answered

Practice exercises

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

  1. Exercise 1easy

    In a lab VM, list all SUID files and pick three. For each, write one sentence on why it needs SUID.

  2. Exercise 2medium

    Write a script that prints world-writable files and orphaned files (no valid owner or group) on the root filesystem, skipping /proc, /sys and /tmp.

  3. Exercise 3hard

    Review this rule and replace it with a safe one: %support ALL=(root) NOPASSWD: /usr/bin/systemctl *. Support staff only need to restart and check the status of nginx.

Check yourself

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

Feel confident about this topic?

Frequently asked questions

What is GTFOBins?
It is a public list of normal Unix programs that can be misused to escape restrictions or get a shell when given sudo or SUID. Defenders use it to check whether a sudo rule or SUID file is dangerous.
Is NOPASSWD in sudoers always bad?
Not always. It is common for automation running one exact command. It is risky on broad rules, because anyone with the user's session gets root without any extra check.
Should I remove the SUID bit from programs I do not use?
On hardened servers, yes, for programs you are sure you do not need, but test first. Package updates may put the bit back, so keep checking your baseline.
ShareXLinkedInWhatsApp