Skip to main content

Configuring Linux File Permissions and Ownership

Understand the Unix permission model and manage file permissions and ownership on production Linux servers using chmod, chown, and ACLs.

30 Terms

Overview and What You Will Learn

Every Linux file has an owner, a group and a set of permission bits, and together they decide who can read, write or execute it. In this lab you will read permissions, change them with chmod and chown, control defaults with umask, grant extra access with ACLs, use the special bits, and apply safe permission patterns to a web root, an application directory, SSH keys and logs. It assumes a Debian or Ubuntu test server and a sudo-capable user.

  • Reading the permission string and converting it to octal
  • Changing modes with symbolic and octal chmod
  • Changing owners and groups with chown
  • Controlling default permissions with umask
  • Granting per-user access with ACLs
  • Using setuid, setgid and the sticky bit
  • Diagnosing "Permission denied" along a whole path

Why This Matters in Production

Wrong permissions break deployments and weaken security at the same time. A script fails because its process user cannot write to a log directory, a web server returns 403 because the document root is unreadable, and SSH refuses a key file that is too open. Imagine a payments team at a company like Razorpay that makes every file in the payment service directory owned by one service user, writable by nobody else. If the web server or a CI runner is compromised, it still cannot change the payment code.

Core Principles

Linux checks permissions in a fixed order and stops at the first match. If you are the file's owner, only the owner bits apply. If not, and you are in the file's group, only the group bits apply. Otherwise the other bits apply, so an owner with fewer rights than "other" can be denied access that everyone else has.

flowchart TD A([Process accesses file]):::src --> B{Is the user
the owner?}:::q B -->|yes| O[Apply owner bits]:::ok B -->|no| C{In the file's
group?}:::q C -->|yes| G[Apply group bits]:::ok C -->|no| X[Apply other bits]:::warn classDef src fill:#DBEAFE,color:#1E293B classDef q fill:#FFEDD5,color:#1E293B classDef ok fill:#10B981,color:#FFFFFF classDef warn fill:#F97316,color:#FFFFFF

The ten-character string from ls -l has a file type followed by three triplets for owner, group and other. For -rwxr-xr--, the first character is the type (- file, d directory, l symlink), and the triplets are rwx, r-x and r--. Each permission has a value, and you add them up per triplet:

Permission Letter Value
Read r 4
Write w 2
Execute x 1

So rwx r-x r-- is 7, 5 and 4, which is octal 754. These are the modes you will use most:

Mode String Typical use
644 -rw-r--r-- Config files, HTML, static assets
640 -rw-r----- Sensitive config readable by one group
600 -rw------- SSH private keys and secrets
755 -rwxr-xr-x Scripts, binaries, public directories
750 -rwxr-x--- Restricted scripts and app directories
700 -rwx------ The ~/.ssh directory

Never use 777 in production, since it lets every user on the machine change the file.

Execute means something different on a directory. It means "can enter and traverse", so without it you cannot cd into the directory or reach anything inside, even files you could otherwise read. Read on a directory lets you list names, and write lets you create and delete entries.

Three special bits change how files and directories behave:

Bit Octal On a file On a directory
setuid 4000 Runs as the file's owner No effect
setgid 2000 Runs with the file's group New files inherit the directory's group
sticky 1000 No effect Users can delete only their own files

Examples are /usr/bin/passwd with setuid, which lets users change their own password, and /tmp with the sticky bit. In ls -l the bits show as s or t in the execute position, and as capital S or T when the execute bit itself is missing. Linux ignores setuid on shell scripts, so it works only on compiled binaries.

Detailed Step-by-Step Practical Lab

Step 1 - Create a Lab Directory and Read Permissions
Bash
mkdir -p /tmp/permissions-lab/subdir /tmp/permissions-lab/app/config /tmp/permissions-lab/logs
cd /tmp/permissions-lab
touch file1.txt file2.sh
ls -la
# drwxr-xr-x 2 ubuntu ubuntu 4096 Oct 14 10:00 subdir
# Detailed view of one file, with the octal mode
stat -c '%a %U:%G %n' file1.txt
Step 2 - Change Permissions with chmod

Symbolic mode uses u (owner), g (group), o (other) and a (all), with + to add, - to remove and = to set exactly:

Bash
chmod u+x file2.sh
ls -l file2.sh # -rwxr--r--
chmod go-w file1.txt
chmod 644 file1.txt # -rw-r--r--
chmod 755 file2.sh # -rwxr-xr-x
chmod 700 subdir # drwx------

Recursive changes need care. Using one octal mode for a whole tree is a classic mistake, because directories need the execute bit and plain files usually do not:

Bash
chmod -R 644 app # wrong: removes x from the directories too
cd app # Permission denied
# Right: capital X adds execute only to directories
# (and to files that are already executable)
chmod -R u=rwX,go=rX app
cd app && cd ..
Step 3 - Change Ownership with chown
Bash
sudo useradd -r -s /usr/sbin/nologin app-service
sudo chown app-service file1.txt # change owner
sudo chown app-service:app-service file2.sh # owner and group
sudo chown :app-service subdir # group only (same as chgrp)
ls -l file1.txt file2.sh
# -rw-r--r-- 1 app-service ubuntu 0 Oct 14 10:00 file1.txt
# -rwxr-xr-x 1 app-service app-service 0 Oct 14 10:00 file2.sh
# You are no longer the owner, so the owner bits no longer apply to you
echo "test" >> file1.txt # Permission denied
# Recursive ownership change for a directory tree
sudo chown -R app-service:app-service app
Step 4 - Control Defaults with umask

The umask clears bits from the starting mode of new objects, which is 666 for files and 777 for directories. Think of it as a mask, not subtraction. A umask of 022 gives 644 files and 755 directories, 027 gives 640 and 750, and 077 gives 600 and 700.

Bash
umask # 0022
# Test a stricter umask in a subshell so your own shell is unchanged
(umask 027; touch f-027; mkdir d-027; ls -ld f-027 d-027)
# -rw-r----- 1 ubuntu ubuntu 0 ... f-027
# drwxr-x--- 2 ubuntu ubuntu 4096 ... d-027

A umask in /etc/profile.d/ would affect every login shell for every user, and service accounts that never log in would not read it. For a systemd service, set it in the unit instead:

INI
[Service]
UMask=0027
Step 5 - Grant Per-User Access with ACLs

ACLs let you give a specific user or group access without changing the owner or group. They need the acl package and a filesystem that supports them, which ext4 and XFS do by default:

Bash
sudo apt install acl
sudo useradd -m deploy
touch acl-demo.txt
getfacl acl-demo.txt
# Give the deploy user read and write access
setfacl -m u:deploy:rw acl-demo.txt
getfacl acl-demo.txt
# user::rw-
# user:deploy:rw-
# group::r--
# mask::rw-
# other::r--
ls -l acl-demo.txt # a trailing + marks a file with an ACL

The mask line caps what named users and groups can receive, and chmod on the group bits changes the mask. If an ACL seems ignored, check getfacl for #effective: notes. Set a default ACL on a directory so new files inherit it, then remove entries:

Bash
setfacl -d -m u:deploy:rw logs
touch logs/new.log
getfacl logs/new.log
setfacl -x u:deploy acl-demo.txt # remove one entry
setfacl -b acl-demo.txt # remove all ACLs
Tip

cp -a and tar --acls preserve ACLs, but a plain cp or tar can silently drop them, so check backups and deploy tools before relying on ACLs.

Step 6 - Use the Special Bits

A setgid directory makes shared folders work, because new files inherit the directory's group instead of the creator's primary group:

Bash
sudo groupadd devs
mkdir shared
sudo chgrp devs shared
chmod 2775 shared
touch shared/report.txt
ls -ld shared # drwxrwsr-x ... devs
ls -l shared/report.txt # -rw-r--r-- ... devs
ls -ld /tmp # drwxrwxrwt: the t is the sticky bit
ls -l /usr/bin/passwd # -rwsr-xr-x: the s is setuid
# Audit setuid and setgid files, staying on one filesystem
sudo find / -xdev -type f \( -perm -4000 -o -perm -2000 \) 2>/dev/null
Security

Setuid and setgid binaries are a common privilege escalation route. Audit them regularly, and investigate any you did not expect.

Step 7 - Apply Production Permission Patterns

Run these on a test server. They show the shape of each pattern, so adjust the names to your system.

Web root. The deploy user owns the files and the web server can only read them, so a compromised web process cannot rewrite the site:

Bash
sudo chown -R deploy:www-data /var/www/html
sudo chmod -R u=rwX,g=rX,o= /var/www/html

Application directory and config files:

Bash
sudo chown -R app-service:app-service /opt/myapp
sudo chmod 750 /opt/myapp
sudo chmod 640 /opt/myapp/config/*.conf

SSH. OpenSSH refuses keys when the files or the home directory are too open:

Bash
mkdir -p ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod go-w ~ # the home directory must not be group or world writable

Logs. The adm group reads logs on Debian and Ubuntu, and the setgid bit keeps new log files in that group:

Bash
sudo chown app-service:adm /var/log/myapp
sudo chmod 2750 /var/log/myapp
Step 8 - Clean Up
Bash
cd ~
rm -rf /tmp/permissions-lab
sudo userdel app-service
sudo userdel -r deploy
sudo groupdel devs

Production Best Practices & Common Pitfalls

Common Mistake

Running chmod 777 to make a permission error go away. It fixes the symptom by giving every user write access. Find which user needs access, then set the minimum owner, group and mode.

Tip

When diagnosing a permission problem, check every directory in the path, not just the file. Each directory needs execute for the user, so a 644 file inside a 700 directory is unreachable to everyone but the directory's owner. The command namei -l /full/path/to/file shows the permissions of every component at once.

Remember

Octal and symbolic modes do the same thing. Octal is quicker to type and symbolic is clearer to read, and you will see both in production scripts.

Mistake Problem Fix
chmod -R 644 on a directory tree Strips execute from directories, so nobody can enter them chmod -R u=rwX,go=rX
SSH private key too open SSH refuses to use it chmod 600 ~/.ssh/id_ed25519
Forgetting -R on a tree Only the top-level item changes Use -R, and check with ls -lR
Web server owns its own files A compromise can rewrite the site Owner is the deploy user, web server reads via group
Setting umask in /etc/profile.d for a service Services never read it UMask= in the systemd unit
Assuming an ACL grants access The mask can cut it back Read getfacl and its effective rights
  • Look beyond the mode bits. If the permissions look right but access still fails, check SELinux or AppArmor (ls -Z, aa-status), a noexec or read-only mount, and the immutable flag (lsattr).
  • Prefer groups over wide modes. Create a group for the people or services that need access, rather than opening the file to everyone.
  • Keep ownership minimal. Files a service only reads should not be owned by that service.

Quick Reference & Troubleshooting Commands

Task Command
View permissions ls -la
Show mode as octal stat -c '%a %U:%G %n' FILE
Check permissions along a path namei -l /full/path/to/file
Set a mode chmod 640 FILE or chmod u=rw,g=r,o= FILE
Recursive safe mode chmod -R u=rwX,go=rX DIR
Change owner and group sudo chown USER:GROUP FILE
Show the current umask umask
Show ACLs getfacl FILE
Add or remove an ACL entry setfacl -m u:USER:rw FILE and setfacl -x u:USER FILE
Find setuid files sudo find / -xdev -type f -perm -4000
Symptom First command What to look for
Permission denied writing namei -l PATH Write bit for the user or group, and execute on every directory
SSH ignores authorized_keys ls -la ~/.ssh ~/.ssh is 700, keys and authorized_keys are 600
Web server returns 403 namei -l /var/www/html/index.html The web user can read the file and traverse every directory
Script works by hand, fails in cron ls -l SCRIPT Execute bit for the cron user, and path permissions
App cannot read its config ls -l CONFIG Owner or group matches the app user
Modes look right but access fails ls -Z, lsattr, mount SELinux context, immutable flag, noexec mount

Resources

Explore More in Linux Fundamentals and File System

All 4 Topics

Frequently Asked Questions

Is Configuring Linux File Permissions and Ownership free to learn on DevOps Network?

Yes - this topic, like everything on DevOps Network, is 100% free with no paywall or sign-up gate.

What does the Configuring Linux File Permissions and Ownership topic cover?

Understand the Unix permission model and manage file permissions and ownership on production Linux servers using chmod, chown, and ACLs.

Found a mistake, an outdated step or a broken link?