Introduction
File permissions in Linux are a fundamental security mechanism that controls who can read, modify, or execute files and directories. Misconfigured permissions can lead to data leaks, service failures, or system compromise. This guide will help you master permission management using standard commands to ensure your system's security and correct operation.
Requirements
Before you begin, ensure you have:
- Access to a Linux terminal (Ubuntu, Debian, CentOS, Fedora, or another distribution)
- Basic command-line skills
- Superuser privileges (sudo) may be required to change permissions for system files or modify ownership
Step 1: Checking Current Permissions
To understand the current configuration, use the ls -l command. It displays detailed file information, including permissions, owner, and group.
ls -l filename
Example output:
-rwxr-xr-- 1 user group 1024 Feb 16 10:00 example.txt
Decoding the permission string:
- The first character
-indicates the file type (-for regular file,dfor directory,lfor symlink). - The next three characters (
rwx) are the owner's permissions (read, write, execute). - The following three (
r-x) are the group's permissions. - The last three (
r--) are permissions for all other users. - If a character is replaced with
-, that permission is absent.
Step 2: Changing Permissions with chmod
The chmod command changes access permissions. There are two main approaches: symbolic and numeric (Octal).
Symbolic Method
You specify a category (u – user/owner, g – group, o – others, a – all) and an operation (+, -, =).
chmod u+x script.sh # Add execute permission for the owner
chmod g-w file.txt # Remove write permission for the group
chmod o=r file.txt # Set read-only for others
chmod a+x /usr/bin/app # Add execute permission for everyone
Numeric (Octal) Method
Each permission has a numeric value: r=4, w=2, x=1. Sum the values for each category (owner, group, others).
7= 4+2+1 (rwx)6= 4+2+0 (rw-)5= 4+0+1 (r-x)4= 4+0+0 (r--)0= 0+0+0 (---)
Example: chmod 755 file
- Owner: rwx (7)
- Group: r-x (5)
- Others: r-x (5)
chmod 755 /path/to/script.sh
chmod 644 /path/to/config.conf # Owner: rw-, group and others: r--
To change permissions recursively in a directory:
chmod -R 755 /var/www/html
Step 3: Changing File Ownership with chown
The chown command changes the owner and/or group of a file. Syntax: chown [new_owner]:[new_group] file.
chown alice:developers project.txt # Change both owner and group
chown alice project.txt # Change only the owner
chown :developers project.txt # Change only the group
To apply recursively:
chown -R alice:developers /projects/
⚠️ Important: Changing the owner of system files usually requires sudo privileges.
Step 4: Changing a File's Group with chgrp
If you only need to change the group, use chgrp:
chgrp developers file.txt
Recursively:
chgrp -R developers /shared/
Step 5: Practical Permission Configuration Examples
Example 1: Web Server (Nginx/Apache)
Configuration files should be protected, while logs must be writable by the web server.
# Configuration: only root can read/write, group www-data can only read
chmod 640 /etc/nginx/nginx.conf
chown root:www-data /etc/nginx/nginx.conf
# Logs: group www-data can write
chmod 664 /var/log/nginx/access.log
chown www-data:adm /var/log/nginx/access.log
Example 2: Script Run by root
A script that should only be executable by root and not modifiable by others:
chmod 700 /usr/local/bin/backup.sh # Only the owner (root) has full permissions
chown root:root /usr/local/bin/backup.sh
Example 3: Shared Directory for a Team
A directory where multiple users have write access:
# Create a group and add users
groupadd shared-group
usermod -aG shared-group alice
usermod -aG shared-group bob
# Configure the directory
mkdir /shared/project
chown root:shared-group /shared/project
chmod 2775 /shared/project # SGID bit: new files inherit the directory's group
Now all files created in /shared/project will belong to the shared-group.
Example 4: Secure Upload Directory
If you need to allow file uploads but prevent execution:
chmod 733 /uploads # Owner: full permissions, group and others: write and execute (but not read)
This allows uploading files but prevents reading their contents.
Verifying the Result
After making changes, check permissions with ls -l:
ls -l /path/to/file
Ensure:
- Owner and group are set correctly.
- Permissions match the requirements (e.g.,
-rw-r--r--for regular files,drwxr-xr-xfor directories).
Test functionality:
- Try executing scripts as different users.
- Check if you can write to a file or directory.
- For web servers, verify they can read configurations and write logs.
Common Issues
"Permission denied" error when running a command
- Cause: insufficient permissions (e.g., trying to modify a system file without sudo).
- Solution: add
sudobefore the command if you have admin rights, or adjust the file's permissions to grant your user access.
Permission changes didn't apply
- Ensure you are modifying the correct file or directory.
- Check if permissions are overridden by mount options (e.g.,
noexec,nosuidin/etc/fstab). - For symlinks, use
chmod -hor modify the target file's permissions.
Recursive change affected the wrong files
- Always verify the path before using
-R. - Use
findfor precise selection:find /var/www -type f -name "*.php" -exec chmod 644 {} \;
Permissions 777 are too open
- Avoid
777except for temporary debugging. Instead, configure groups and use775or755. - For directories requiring write access by multiple users, use group permissions and the SGID bit (
chmod 2770).
Cannot change owner to another user
- Ensure the user exists (
id username). - Changing ownership to a non-sudo user requires root privileges.