Linux Permissions Explained: A Beginner’s Guide to File Security Commands

[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

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.

Understanding Basic Permission Concepts

User, Group, and Others

Linux implements a hierarchical permission system with three levels of access:

  • User (u): The file’s owner
  • Group (g): Members of the file’s assigned group
  • Others (o): Everyone else on the system

Read, Write, and Execute Permissions

Each permission level has three basic rights:

  • Read (r): Value of 4
  • Write (w): Value of 2
  • Execute (x): Value of 1
# Example file permissions display
-rwxr-xr-- 1 user group 4096 Nov 1 2024 example.txt

Numeric Permission Notation

Permissions can be represented numerically:

  • 7 (rwx) = 4 + 2 + 1
  • 6 (rw-) = 4 + 2
  • 5 (r-x) = 4 + 1
  • 4 (r–) = 4

Essential Permission Commands

The chmod Command

# 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--

Understanding umask

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

Working with su and sudo

# Switch to root user
su -

# Execute single command as root
sudo apt update

# Edit system file with sudo
sudo nano /etc/hosts

Managing Ownership with chown

# Change owner
chown user1 file.txt

# Change owner and group
chown user1:group1 file.txt

# Recursive ownership change
chown -R user1:group1 directory/

Your Turn! Practical Exercise

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.

  1. Create a new file:
touch script.sh
  1. Your task: Set the appropriate permissions using chmod.

Solution:

# Create the file
touch script.sh

# Set permissions (owner: rwx, group: r--, others: ---)
chmod 740 script.sh

# Verify permissions
ls -l script.sh

Quick Takeaways

  • Permissions are divided into user, group, and others
  • Basic permissions are read (4), write (2), and execute (1)
  • chmod modifies permissions
  • umask sets default permissions
  • su and sudo provide elevated privileges
  • chown changes file ownership

Common Permission Scenarios

Web Server Permissions

# Standard web directory permissions
chmod 755 /var/www/html
chmod 644 /var/www/html/*.html

Shared Directories

# Create a shared directory
mkdir /shared
chmod 775 /shared
chown :developers /shared

Troubleshooting

Common Permission Issues

  1. Permission Denied
# Check file permissions
ls -l problematic_file
# Check current user and groups
id
  1. Cannot Execute Script
# Make script executable
chmod +x script.sh

FAQs

  1. 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.

  2. 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.

  3. Q: How do I recursively change permissions? A: Use chmod with the -R flag: chmod -R 755 directory/

  4. Q: What’s the safest permission for configuration files? A: Usually 644 (rw-r–r–) or 640 (rw-r—–) depending on security requirements.

  5. Q: How do I check my current user and group memberships? A: Use the id command to display all user and group information.

References

  1. GNU Coreutils Documentation

  2. Ubuntu Community Help Wiki – File Permissions

  3. Red Hat Enterprise Linux Documentation

  4. The Linux Command Line, A Complete Introduction (2nd Edition)

Conclusion

Understanding Linux permissions is fundamental to system security and proper file management. Practice these commands regularly, and always consider security implications when modifying permissions.

Try this Exercise! Then, Share Your Experience

Start by auditing your important files’ permissions using ls -l. Create a test directory to practice these commands safely. Share your experience or questions in the comments below!


Happy Coding! 🚀

Linux Permissions

You can connect with me at any one of the below:

Telegram Channel here: https://t.me/steveondata

LinkedIn Network here: https://www.linkedin.com/in/spsanderson/

Mastadon Social here: https://mstdn.social/@stevensanderson

RStats Network here: https://rstats.me/@spsanderson

GitHub Network here: https://github.com/spsanderson


To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)