File Permissions & Ownership
BeginnerLinux file permissions control who can read, write, or execute every file. Understanding rwx, octal notation, chmod, chown, and setuid is critical for security and system administration.
Overview
Every Linux file has an owner (user), a group, and a set of permissions for three categories: owner, group, and others. Permissions are represented as three groups of rwx (read, write, execute) bits. The numeric (octal) notation (755, 644, 600) maps directly: r=4, w=2, x=1. Understanding permissions is essential for securing web servers (do not give world-write to config files), running executables, and understanding why "Permission denied" errors happen.
Reading Permissions
The ls -l output encodes all permission information in a compact 10-character string. Learn to read it at a glance.
ls -la /etc/nginx/nginx.conf
# -rw-r--r-- 1 root root 2691 Jan 15 10:23 nginx.conf
# │└─┬──┘└─┬──┘└─┬──┘
# │ owner group others
# │
# â””── file type:
# - = regular file
# d = directory
# l = symbolic link
# b = block device
# c = character device
# Permission bits: r=read w=write x=execute
# rw- r-- r--
# 110 100 100 ↠binary
# 6 4 4 ↠octal = 644
# Common permission patterns:
# 755 rwxr-xr-x binaries, directories (owner full, others read+execute)
# 644 rw-r--r-- config files (owner read-write, others read-only)
# 600 rw------- private keys, secrets (owner only)
# 777 rwxrwxrwx âš ï¸ world-writable — security risk, avoid in production
# 400 r-------- read-only secrets (SSH private keys)
ls -la ~/.ssh/
# -rw------- 1 akshay akshay 1823 Jan 10 id_rsa ↠600: good
# -rw-r--r-- 1 akshay akshay 399 Jan 10 id_rsa.pub ↠644: goodchmod & chown
chmod changes permissions, chown changes ownership. Both accept symbolic (u+x, go-w) and numeric (755) modes.
# chmod — change permissions
# Numeric mode (easiest to remember)
chmod 755 script.sh # rwxr-xr-x
chmod 644 config.conf # rw-r--r--
chmod 600 ~/.ssh/id_rsa # rw------- (SSH requires this)
chmod 700 ~/.ssh/ # rwx------
# Symbolic mode (more readable for small changes)
chmod +x script.sh # add execute for all
chmod u+x,go-x script.sh # add execute for owner, remove for group+others
chmod g+w app/ # add write for group
chmod o-rwx secrets/ # remove all permissions for others
chmod a-x file.sh # remove execute for all (a = all)
# Recursive (dangerous — think before using on / )
chmod -R 755 /var/www/html/
# chown — change owner and group
chown akshay file.txt # change owner
chown akshay:developers file.txt # change owner and group
chown -R www-data:www-data /var/www/ # recursive (for web server files)
chgrp developers project/ # change group only
# Check your own identity
id
# uid=1000(akshay) gid=1000(akshay) groups=1000(akshay),4(adm),27(sudo),1001(docker)Special Bits — setuid, setgid, sticky
Three special permission bits handle advanced cases: setuid (run as file owner), setgid (inherit group), and sticky bit (protect shared directories like /tmp).
# setuid (s in owner execute position)
# Binary runs with file OWNER's privileges regardless of who runs it
ls -la /usr/bin/passwd
# -rwsr-xr-x 1 root root ... /usr/bin/passwd
# ↑ setuid bit — anyone can run passwd, it runs as root to write /etc/shadow
chmod u+s /path/to/binary # set setuid
chmod 4755 /path/to/binary # numeric (4 = setuid, 755 = rwxr-xr-x)
# setgid on a DIRECTORY
# Files created inside inherit the DIRECTORY's group (not creator's group)
mkdir /shared
chgrp developers /shared
chmod g+s /shared # set setgid on directory
# chmod 2775 /shared # numeric: 2 = setgid
# Now all files created in /shared belong to 'developers' group
# Sticky bit on a directory
# Only the file OWNER can delete their own files (even if others have write)
ls -la /tmp
# drwxrwxrwt 18 root root ... /tmp
# ↑ sticky bit — you can't delete other users' files in /tmp
chmod +t /shared # set sticky bit
chmod 1777 /tmp # numeric: 1 = sticky
# Find all setuid binaries (security audit)
find / -perm -4000 -type f 2>/dev/nullKey Points to Remember
- 1Permissions are three groups of rwx: owner, group, others.
- 2Numeric: r=4, w=2, x=1. Common: 755 (binaries), 644 (configs), 600 (private keys).
- 3chmod changes permissions; chown changes file ownership.
- 4setuid on executables: runs with the file owner's privileges (e.g., passwd runs as root).
- 5Sticky bit on directories: only the file owner can delete their own files (used on /tmp).
- 6SSH will refuse to use a private key that is not 600 or 400 — "bad permissions" error.
Interview Questions
Sign in to ask AriaWhat does chmod 755 mean?
What is the setuid bit and why is it needed for passwd?
Ask Aria about File Permissions & Ownership
Your personal AI tutor — ask anything about this concept
Revision Status
Personal Notes
Sign in to save personal notes for this topic.
Discussion
Sign in to join the discussion.