01 / 20

Linux Permissions
& sudo

Understand how Linux controls who can read, write, and execute files, and how sudo grants temporary superuser power.

Cyber Security

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

r : Read
View file contents or list directory entries.
w : Write
Modify or delete a file; add or remove files in a directory.
x : Execute
Run a file as a program; enter (cd into) a directory.

Owner, Group, & Others

Owner The user who created the file rwx Full control Group Users sharing a named group r-x Read + execute Others Everyone else on the system r-- Read only

Reading ls -l Output

-rwxr-xr-- Type Owner Group Others - = regular file | d = directory | l = symbolic link

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
rwx1117Full
rw-1106Read + Write
r-x1015Read + Execute
r--1004Read only
---0000None

chmod: Changing Permissions

Symbolic Mode
chmod u+x file
chmod g-w file
chmod o=r file
Numeric Mode
chmod 755 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.

Normal User Limited access sudo Root Privileges Full system control Elevated for one command only

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.

# Format: user host=(runas) commands
alice ALL=(ALL:ALL) ALL

# Allow bob to restart nginx only
bob ALL=(root) /usr/bin/systemctl restart nginx

How to Set Permissions

1
Identify the file: Run ls -l filename to see current permissions, owner, and group.
2
Decide who needs access: Does the owner, group, or others need read, write, or execute?
3
Choose your method: Symbolic (chmod u+x) for quick tweaks; numeric (chmod 644) for setting all at once.
4
Apply the change: Run chmod. Use sudo chmod if you are not the file owner.
5
Verify: Run ls -l again to confirm the new 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.

$ ls -l backup.sh
-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.

$ ls -l db-config.yml
-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.

$ apt install nginx
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

✕ chmod 777 Everything
Granting full access to all users destroys the permission model and opens the system to any exploit.
✕ Running as root
Logging in as root for daily work means every typo or malware runs with unlimited power.
✕ Editing sudoers Directly
Skipping visudo risks a syntax error that locks out all sudo access, requiring recovery mode.

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?