Understanding Linux permissions is crucial for anyone working with Linux systems. Whether you’re a new system administrator, developer, or Linux enthusiast, mastering file permissions is essential for maintaining system security and proper file access control.
Linux implements a hierarchical permission system with three levels of access:
Each permission level has three basic rights:
# Example file permissions display -rwxr-xr-- 1 user group 4096 Nov 1 2024 example.txt
Permissions can be represented numerically:
# Symbolic mode chmod u+x script.sh # Add execute permission for user chmod g-w file.txt # Remove write permission for group chmod o=r document.pdf # Set others to read-only # Numeric mode chmod 755 script.sh # rwxr-xr-x chmod 644 file.txt # rw-r--r--
The umask command sets default permissions for new files and directories:
# Check current umask umask # Set new umask umask 022 # Results in 755 for directories, 644 for files
# Switch to root user su - # Execute single command as root sudo apt update # Edit system file with sudo sudo nano /etc/hosts
# Change owner chown user1 file.txt # Change owner and group chown user1:group1 file.txt # Recursive ownership change chown -R user1:group1 directory/
Try this hands-on exercise:
Problem: Create a script that needs to be executable by the owner only, readable by the group, and inaccessible to others.
touch script.sh
Solution:
# Create the file touch script.sh # Set permissions (owner: rwx, group: r--, others: ---) chmod 740 script.sh # Verify permissions ls -l script.sh
# Standard web directory permissions chmod 755 /var/www/html chmod 644 /var/www/html/*.html
# Check file permissions ls -l problematic_file # Check current user and groups id
# Make script executable chmod +x script.sh
Q: Why can’t I modify a file even as the owner? A: Check if the file has write permissions for the owner using ls -l
. Use chmod u+w filename
to add write permissions.
Q: What’s the difference between su and sudo? A: ‘su’ switches to another user account completely, while ‘sudo’ executes single commands with elevated privileges.
Q: How do I recursively change permissions? A: Use chmod with the -R flag: chmod -R 755 directory/
Q: What’s the safest permission for configuration files? A: Usually 644 (rw-r–r–) or 640 (rw-r—–) depending on security requirements.
Q: How do I check my current user and group memberships? A: Use the id
command to display all user and group information.
Understanding Linux permissions is fundamental to system security and proper file management. Practice these commands regularly, and always consider security implications when modifying permissions.