Linux Permissions
& sudo
Understand how Linux controls who can read, write, and execute files, and how sudo grants temporary superuser power.
Why Permissions Matter
Linux is a multi-user operating system. Multiple people (or services) can share the same machine at the same time. Without a permission model, any user could read your private keys, overwrite system files, or delete another user's data.
The rwx Triad
Owner, Group, & Others
Reading ls -l Output
Run ls -l in a terminal and you will see a 10-character string at the start of each line. The first character is the file type; the remaining nine are three rwx triplets for owner, group, and others.
Octal Notation
Each permission maps to a power of two. Add the values for each triplet to get a single digit: r = \(4\), w = \(2\), x = \(1\). A dash means \(0\).
| Permission | Binary | Octal | Meaning |
|---|---|---|---|
| rwx | 111 | 7 | Full |
| rw- | 110 | 6 | Read + Write |
| r-x | 101 | 5 | Read + Execute |
| r-- | 100 | 4 | Read only |
| --- | 000 | 0 | None |
chmod: Changing Permissions
chmod g-w file
chmod o=r file
chmod 644 file
chmod 600 file
What is sudo?
sudo stands for "superuser do." It lets an authorized user execute a single command with root (administrator) privileges without logging in as root permanently.
sudo vs su vs root
| Concept | sudo | su | root login |
|---|---|---|---|
| Scope | Single command | Entire session | Entire session |
| Password | Your own | Target user's | Root's |
| Audit trail | ✓ Logged | Partial | ✕ None |
| Risk level | Low | Medium | High |
The /etc/sudoers File
The sudoers file controls who can use sudo and what commands they may run. It should only be edited with the visudo command, which validates syntax before saving.
alice ALL=(ALL:ALL) ALL
# Allow bob to restart nginx only
bob ALL=(root) /usr/bin/systemctl restart nginx
How to Set Permissions
Example 1: Making a Script Executable
You wrote a backup script called backup.sh but it won't run when you type ./backup.sh. Let's fix it.
-rw-r--r-- 1 alice devs 2048 Jun 10 backup.sh
# No execute bit! Add it for the owner:
$ chmod u+x backup.sh
$ ls -l backup.sh
-rwxr--r-- 1 alice devs 2048 Jun 10 backup.sh
Example 2: Protecting a Config File
The file db-config.yml contains a database password. Currently it is world-readable. We need only the owner to have access.
-rw-r--r-- 1 admin staff 512 Jun 10 db-config.yml
# Set owner rw, group none, others none:
$ chmod 600 db-config.yml
$ ls -l db-config.yml
-rw------- 1 admin staff 512 Jun 10 db-config.yml
Example 3: Using sudo to Install Software
Installing packages modifies system directories that belong to root. A normal user needs sudo to perform this action.
E: Could not open lock file - open (13: Permission denied)
# Elevate with sudo:
$ sudo apt install nginx
[sudo] password for alice: ********
Setting up nginx (1.24.0) ... done.
Common Mistakes
Quick Reference Table
| Command | What It Does | Example |
|---|---|---|
| ls -l | List permissions | ls -l /etc |
| chmod | Change permissions | chmod 644 file |
| chown | Change owner/group | chown alice:devs file |
| sudo | Run as root (1 cmd) | sudo apt update |
| visudo | Safely edit sudoers | sudo visudo |
Question 1 of 5
A file has the permission string -rwxr--r--. What can group members do with this file?
Question 2 of 5
What octal value represents the permission rw-r-x---?
Question 3 of 5
Why is sudo preferred over logging in directly as the root user?
Question 4 of 5
Which command would remove write permission from others on a file called report.txt?
Question 5 of 5
A system administrator runs chmod 777 /var/www/html. Why is this a security concern?