0%
Complete Guide to Linux Permissions: Understanding and Mastering Access Rights

Complete Guide to Linux Permissions: Understanding and Mastering Access Rights

Learn how Linux permissions work, from symbolic notation to essential commands. A practical guide to securing your files and directories.

I

InSkillCoach

· min

Complete Guide to Linux Permissions

Permissions in Linux are a fundamental concept for system security. They determine who can access, modify, or execute files and directories. This guide explains in detail how permissions work and how to manage them effectively.

Anatomy of Linux Permissions

When you run the ls -l command, you see output similar to this:

drwxr-xr-x  2  shum  staff  4096  Jan 16 22:04  mail

Let’s break down each element:

1. File Type (1st character)

  • d: Directory
  • -: Regular file
  • l: Symbolic link
  • b: Block device file
  • c: Character device file

2. Permissions (next 9 characters)

Permissions are divided into three groups of three characters:

  • User (Owner): The first 3 characters
  • Group: The middle 3 characters
  • Others: The last 3 characters

For each group, the permissions are:

  • r: Read permission
  • w: Write permission
  • x: Execute permission
  • -: Permission not granted

The number of hard links to the file

4. Owner and Group

  • Owner: The name of the user who owns the file
  • Group: The name of the group that owns the file

5. Size

The file size in bytes

6. Date and Time

The date and time of the last modification

7. File Name

The name of the file or directory

Understanding Numeric Values

Permissions can also be represented in octal notation:

  • r = 4
  • w = 2
  • x = 1

Common examples:

  • chmod 755 = rwxr-xr-x
  • chmod 644 = rw-r--r--
  • chmod 777 = rwxrwxrwx (use with caution!)

Essential Commands

Changing Permissions

# Symbolic notation
chmod u+x fichier    # Adds execute permission for the user
chmod g-w fichier    # Removes write permission for the group
chmod o=r fichier    # Sets read-only for others

# Numeric notation
chmod 755 fichier    # rwxr-xr-x
chmod 644 fichier    # rw-r--r--

Changing the Owner

# Change the owner
chown utilisateur fichier

# Change the owner and the group
chown utilisateur:groupe fichier

Changing the Group

chgrp groupe fichier

Practical Use Cases

1. Securing a Script

# Make a script executable for the owner only
chmod 700 script.sh

# Check the permissions
ls -l script.sh
# Result: -rwx------ 1 user group 1234 Jan 21 10:00 script.sh

2. Setting Up a Shared Directory

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

3. Protecting Sensitive Files

# Secure a configuration file
chmod 600 config.conf

# Protect the .ssh directory
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa

Best Practices

  1. Principle of Least Privilege

    • Grant only the permissions that are necessary
    • Avoid using 777 except in exceptional cases
  2. Directory Security

    • Directories need the execute bit (x) to be traversed
    • Use 755 for public directories
    • Use 700 for private directories
  3. Sensitive Files

    • Use 600 or 400 for configuration files
    • Take particular care to protect files containing secrets
  4. Group Management

    • Create groups to manage access
    • Prefer group-based management over “others” permissions

Common Troubleshooting

Problem: “Permission Denied”

  1. Check the current permissions:
ls -l fichier
  1. Check the owner and the group:
ls -l fichier | awk '{print $3, $4}'
  1. Check your group membership:
groups

Problem: Recursive Permissions

To change the permissions of all files in a directory:

# Be careful when using -R!
chmod -R 755 directory/

Conclusion

Mastering Linux permissions is essential for:

  • Securing your system
  • Protecting sensitive data
  • Managing user access effectively
  • Preventing unauthorized modifications

Take the time to understand and apply permissions correctly, because they are the first line of defense of your Linux system.

Additional Resources

Remember that security is an ongoing process. Review your permissions regularly and adapt them as your needs evolve.

InSkillCoach

About InSkillCoach

Training and technology expert

Coach specializing in advanced technologies and AI, backed by GNeurone Inc.

Certifications:

  • AWS Certified Solutions Architect – Professional
  • Google Cloud certifications
  • Microsoft Certified: DevOps Engineer Expert
  • Certified Kubernetes Administrator (CKA)
  • CompTIA Security+
1.8k
90

Comments

Comments are powered by GitHub Discussions

Sign in with GitHub to join the discussion

Link copied!