Introduction / Why This Matters
Understanding the Linux permissions system is fundamental to system security and stability. Without proper configuration, you either leave critical files open to everyone or, conversely, block legitimate processes due to insufficient privileges. This guide explains the Linux security model (rwx) with simple examples and shows how to manage access to files and directories using the chmod and chown commands. You will learn to read permissions, modify them safely, and understand special bits like SUID.
Requirements / Preparation
- Access to a Linux terminal (any distribution: Ubuntu, CentOS, Debian, Fedora, etc.).
- Basic command-line skills: navigation (
cd,ls), file creation (touch,echo). - Superuser privileges (sudo) to change ownership (
chown) or modify permissions on system files. For experimentation, you can use your own home directory (/home/your_user).
Step-by-Step Guide
Step 1: Checking Current Permissions
The primary command for viewing permissions is ls -l. It provides a detailed file listing.
ls -l /home/user/Documents/
Example output:
-rwxr-xr-- 1 alice developers 2048 Feb 17 10:30 report.pdf
drwxr-xr-x 2 bob users 4096 Feb 17 10:31 projects
Breaking down the first line (-rwxr-xr--):
- The first character (
-): object type (-= regular file,d= directory,l= symbolic link). - The next 9 characters are split into three triplets: owner (alice), group (developers), others.
r= read,w= write,x= execute.- For the owner:
rwx(full access). - For the group:
r-x(read and execute, no write). - For others:
r--(read-only).
💡 Tip: For a quick check of a specific file, use
ls -l <filename>.
Step 2: Changing Permissions with Symbolic Mode (chmod)
This mode is intuitive: you address the user class (u = owner, g = group, o = others, a = all) and an operator (+ = add, - = remove, = = set exactly), then list the permissions (r, w, x).
Example 1: Grant write permission to the group for the file config.ini.
chmod g+w config.ini
The group's permissions will change from r-- to rw-.
Example 2: Remove execute permission from everyone except the owner for the script backup.sh.
chmod o-x backup.sh
Or for everyone except the owner and group:
chmod go-x backup.sh
Example 3: Set exact permissions: owner = all, group = read and execute, others = none.
chmod u=rwx,g=rx,o= script.sh
# Or equivalently: chmod 750 script.sh (see next step)
Step 3: Changing Permissions with Octal (Numeric) Mode
This is the most compact and commonly used method. Each permission (r, w, x) is assigned a number:
r= 4w= 2x= 1-(absence) = 0
The sum of these numbers for each of the three triplets (owner, group, others) gives the final octal number (0 to 7).
Common preset values:
7=rwx(4+2+1)6=rw-(4+2)5=r-x(4+1)4=r--(4)0=---
Example 1: chmod 755 script.sh
- Owner:
7=rwx - Group:
5=r-x - Others:
5=r-xThis is standard for executable scripts and public directories.
Example 2: chmod 644 config.conf
- Owner:
6=rw-(can read and modify) - Group:
4=r--(read-only) - Others:
4=r--(read-only) This is standard for regular configuration files.
Example 3: chmod 600 secret.key
- Only the owner can read and write. Group and others have no access. Critical for private keys and passwords.
Step 4: Changing Owner and Group (chown)
The chown command changes a file's metadata: who owns it and which group it belongs to.
sudo chown alice:developers project_folder/
This command sets the owner to alice and the group to developers for the project_folder/ directory.
Important nuances:
- To change only the group, you can omit the owner:
sudo chown :developers project_folder/. - To change only the owner, you can omit the group:
sudo chown alice project_folder/. - Superuser privileges (
sudo) are required if you are not the current file owner.
Step 5: Applying Changes Recursively
The -R flag (recursive) applies the command to all nested files and subdirectories. Use with extreme caution, especially with chmod 777 or chown on system paths.
# Correct: Give the owner (bob) full access to all files in his project, and the group/others read-only access.
sudo chown -R bob:bob /srv/www/myproject/
sudo chmod -R 755 /srv/www/myproject/
⚠️ Important: Recursive
chmodcan remove special bits (SUID/SGID) from files. To preserve them, usechmod -R --preserve-root(on some distributions) or apply permissions more selectively.
Step 6: Understanding and Configuring Special Bits (SUID, SGID, Sticky Bit)
These bits augment standard permissions and alter behavior.
- SUID (Set User ID) — 4xxx (works only for executable files).
- Indicator:
-rwsr-xr-x(seesinstead ofxfor the owner). - Effect: The file executes with the privileges of its owner, not the user who launched it.
- Example:
/usr/bin/passwdhas SUID so a regular user can change the system password (the/etc/shadowfile is root-only). - Setting:
chmod 4755 /path/to/fileorchmod u+s /path/to/file.
- Indicator:
- SGID (Set Group ID) — 2xxx.
- For files: Similar to SUID, but execution happens with the privileges of the file's group-owner.
-rwxr-sr-x. - For directories: Group inheritance. All new files created in this directory will belong to the directory's group, not the creating user's group.
drwxrwsr-x. - Example: Shared development folders (
/srv/shared). - Setting:
chmod 2770 /shared_folderorchmod g+s /shared_folder.
- For files: Similar to SUID, but execution happens with the privileges of the file's group-owner.
- Sticky Bit — 1xxx (typically for directories).
- Indicator:
drwxrwxrwt(seetinstead ofxfor "others"). - Effect: A user can only delete/rename files in a directory if they are the owner of that file (or the directory itself).
- Classic example:
/tmp— any user can create files, but only the file's owner can delete them. - Setting:
chmod 1777 /tmporchmod o+t /some/folder.
- Indicator:
Verifying the Result
- After making changes, run
ls -l <file>again to ensure the permission column updated as expected. - Practical test: Try an action that should be allowed/denied.
- For a file:
cat file.txt(read),echo "test" >> file.txt(write),./script.sh(execute). - For a directory:
cd /folder/(directory access requiresx),touch /folder/newfile(creating a file requireswandxon the folder).
- For a file:
- If the action fails but permissions look correct, check if the file/directory is a symbolic link (
lat the start of thels -lline). The link's permissions are irrelevant; the target object's permissions matter.
Common Issues
Operation not permittederror duringchownorchmodon system files: You are not the file owner and are not usingsudo. Alternatively, the file is on a filesystem mounted withnoexec/nosuid.Permission deniedwhen trying to runchmod/chown: You lack privileges to modify this file's metadata (you are not root and not the owner). Usesudofor system files, or change the owner if it's your file.- Nothing works after
chmod -R 777 /some/dir: You removed all special bits (SUID/SGID) from executables. Some system utilities (likesudo) will stop working. Restore permissions from a backup or reinstall the affected package. - Can't write to a folder even though I have
rwxon the file: Remember: creating/deleting files in a folder requires permissions on the folder itself (wandx), not on the file. - The
sbit (SUID/SGID) doesn't show afterchmod? Ensure you are setting it on an executable file (with thexbit). Ifxis absent,swon't appear.chmod 4755 file(notchmod 4766).