Linux: Files, Users and Permissions
Linux is both your attack machine and one of the most common targets: web servers, containers, devices. When you get a shell, the first move isn’t to fire an exploit but to understand where you are: who you are, what you can read, what runs as root and what sudo lets you run. Almost every Linux privilege escalation is about reading properly what the system is already telling you, and that starts with the file and permission model.
Concepts
Section titled “Concepts”The filesystem
Section titled “The filesystem”Everything hangs off /: there are no drive letters like C:. The hierarchy is a standard (FHS) and nearly identical across distributions.
| Path | What it holds | Why you care |
|---|---|---|
/etc |
System configuration, as text | passwd, shadow, sudoers, crontab, service configs with credentials |
/home · /root |
Users’ folders · root’s | SSH keys, histories, personal config files |
/var |
Changing data | Logs (/var/log), webroots (/var/www), databases, queues |
/tmp · /dev/shm |
Temporary, writable by everyone | Your workspace for uploading tools |
/usr/bin · /usr/local/bin · /opt |
System programs · hand-installed · large packages | Custom software and old versions |
/proc |
Kernel and process state (not on disk) | /proc/<pid>/environ and cmdline can leak secrets |
/dev |
Devices | /dev/null, disks, terminals |
Reading an ls -l line
Section titled “Reading an ls -l line”-rwxr-x--- 1 deploy www-data 18432 Nov 3 09:14 backup.sh| Field | Value | Meaning |
|---|---|---|
| Type | - |
Regular file (d directory, l link, c/b device, s socket, p pipe) |
| Owner permissions | rwx |
deploy reads, writes and executes |
| Group permissions | r-x |
www-data reads and executes |
| Others’ permissions | --- |
Everyone else, nothing |
| Links | 1 |
Number of hard links |
| Owner · group | deploy · www-data |
Who each block applies to |
| Size, date, name | 18432, Nov 3 09:14, backup.sh |
Users and groups
Section titled “Users and groups”- What counts is the UID; the name is a label. root is UID 0, the user whose permissions the kernel never checks.
- Service accounts (
www-data,mysql) have low UIDs and usually no login shell (/usr/sbin/nologin). - A group is a list of users. Each user has a primary group and can have secondary ones. Some groups grant power on their own:
sudo/wheel,docker,lxd,disk,adm(reads logs).
| File | Contents | Who can read it |
|---|---|---|
/etc/passwd |
Users: name, UID, GID, home, shell. The x means the hash lives in shadow |
Anyone |
/etc/shadow |
Password hashes | Root only (and the shadow group) |
/etc/group |
Groups and their members | Anyone |
r, w and x permissions
Section titled “r, w and x permissions”| Permission | On a file | On a directory |
|---|---|---|
r = 4 |
Read the contents | List the names |
w = 2 |
Modify it | Create, rename and delete inside |
x = 1 |
Execute it | Enter and traverse it |
Three blocks (owner, group, others) that add up: 755 = rwxr-xr-x (programs and directories), 644 = rw-r--r-- (regular files), 600 = rw------- (keys and credentials; SSH requires it for your private key).
Changing permissions and owners
Section titled “Changing permissions and owners”chmod u+x script.sh # with letters: who (u g o a), operation (+ - =), permission (r w x)chmod g-w,o= report.txt # remove group write and everything for otherschmod 640 config.php # with numberschmod -R 750 /opt/app # recursivesudo chown deploy:www-data backup.sh # change owner and group (requires root)chgrp www-data uploads/ # change group (if you belong to the target group)New files start from 666 and directories from 777, and the umask turns bits off (it never adds them). With 022 they’re born 644 and 755; with 027, 640 and 750; with 077, 600 and 700. Check and change it with umask (a builtin, so sudo umask makes no sense); to make it stick, put it in the shell’s startup file.
Special permissions
Section titled “Special permissions”| Bit | Where you see it | Effect |
|---|---|---|
SUID (4000) |
s in the owner’s x: -rwsr-xr-x |
The program runs with its owner’s privileges, not the caller’s. Legitimate example: /usr/bin/passwd. |
SGID (2000) |
s in the group’s x |
Same with the group. On a directory, anything created inside inherits its group. |
Sticky (1000) |
t in others’ x: drwxrwxrwt |
On a directory, only each file’s owner can delete or rename it. Example: /tmp. |
Capabilities
Section titled “Capabilities”Instead of all-or-nothing (root or not root), the kernel splits privileges into capabilities assigned to binaries: cap_net_bind_service (listen on low ports), cap_net_raw (raw packets, used by ping), cap_setuid (change UID). An interpreter with cap_setuid is equivalent to a root SUID. See capabilities.
sudo and su
Section titled “sudo and su”| sudo | su | |
|---|---|---|
| Password it asks for | Yours (or none with NOPASSWD) |
The target user’s |
| Who authorises it | A rule in /etc/sudoers or /etc/sudoers.d/ |
Knowing the password is enough |
| Scope | Exactly what the rule allows | Everything that user can do |
| Logging | Who, which command and when | Only that a user switch happened |
sudo -llists what you can run;sudo -u user cmdruns as another user;sudo -iopens a root session.su - userswitches user loading their environment (without the dash you keep your PATH and variables).- A sudoers rule looks like
WHO HOSTS=(AS_WHOM) COMMANDS. A leading%means a group:%sudo ALL=(ALL:ALL) ALLgives everything to that group;deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart nginxallows only that command, without a password.
In practice
Section titled “In practice”# Who am I, and which groups give me powerid # uid=33(www-data) gid=33(www-data) groups=33(www-data)sudo -l # what can I run as another user?
# Users with a valid shellgrep -vE 'nologin|false' /etc/passwd
# SUID and SGID binaries, and capabilitiesfind / -perm -4000 -type f 2>/dev/nullfind / -perm -2000 -type f 2>/dev/nullgetcap -r / 2>/dev/null
# Files and directories writable by everyone (outside /proc)find / -writable -type f -not -path '/proc/*' 2>/dev/null | headfind / -perm -0002 -type d -not -path '/proc/*' 2>/dev/null
# Can I read something I shouldn't?ls -la /etc/shadow /root 2>&1ls -la /home/*/ 2>/dev/nullNext offensive step
Section titled “Next offensive step”- Enumerate the system methodically: Linux enumeration.
- Abuse what you found: SUID, sudo, capabilities, cron.
- The full map: Linux privilege escalation.
Cheat sheet
Section titled “Cheat sheet”| Concept | What to remember |
|---|---|
| root | It’s UID 0, not a name. |
/etc/shadow |
Root only. If you can read it, you have hashes. |
x on a directory |
Enter. Without it you can’t access anything inside. |
w on a directory |
Delete and replace what’s inside (unless sticky). |
| 755 · 644 · 600 | Programs and directories · files · keys. |
| umask | Only removes permissions. 022 → 644/755. |
| SUID · SGID · sticky | Owner’s s · group’s s · others’ t. Uppercase means no effect. |
| SUID on scripts | Ignored. |
| Capabilities | Individual privileges. cap_setuid ≈ root. |
| sudo · su | Your password and logging · theirs and no detail. |
sudo -l |
The first thing you check. |
docker, lxd, disk groups |
Equivalent to root. |
Review questions
Section titled “Review questions”-
What can you do in a directory with
r--permissions? And with--x?Answer
With
r--you see the names of what’s inside, but you can’t enter or read anything. With--xyou can enter and access files whose exact name you know, but you can’t list the contents. -
With a umask of
027, what permissions does a new file get? And a directory?Answer
File: 666 minus 027 →
640(rw-r-----). Directory: 777 minus 027 →750(rwxr-x---). The umask turns bits off; it isn’t arithmetic subtraction, but with these values it matches. -
Why does
/usr/bin/passwdhave the SUID bit?Answer
Because a regular user must be able to change their password, which requires writing to
/etc/shadow, which only root can modify. With SUID the program runs as its owner (root), and the program itself limits what each user can change. -
You see
-rwSr--r--on a binary. Is it exploitable as SUID?Answer
No: the uppercase
Smeans the SUID bit is set but the owner lacks execute permission, so it can’t be run with those privileges. -
sudo -lshows(root) NOPASSWD: /usr/bin/vim /etc/hosts. Why is it dangerous?Answer
Because vim runs as root and lets you run commands from inside (
:!sh) or open other files. Restricting the argument to/etc/hostsdoesn’t limit what you can do once inside the editor. -
Your user is in the
dockergroup. What does that mean?Answer
That you can talk to the Docker daemon, which runs as root, and start a privileged container that mounts the host’s filesystem (
/): in practice, root on the machine.