Linux Permissions and User Management: A Starting Guide

Md Saif ZamanMd Saif Zaman
3 min read

In the world of Linux system administration, understanding file permissions and user management is crucial for maintaining a secure and well-organized system. This blog post will dive deep into these concepts, providing the knowledge and commands need to effectively manage Linux environment.

Part 1: File Permissions

Understanding the Basics

Linux file permissions are based on a simple yet powerful concept. Each file and directory has three sets of permissions:

  1. Owner permissions

  2. Group permissions

  3. Others permissions

Each set can have three types of permissions:

  • Read (r)

  • Write (w)

  • Execute (x)

Viewing Permissions

To view file permissions, use the ls -l command. The output will look something like this:

-rwxrw-r-- 1 user group 4096 Sep 15 10:00 example.txt

Here's what each part means:

  • The first character indicates the file type (- for regular file, d for directory)

  • The next nine characters represent the permissions for owner, group, and others

  • The following fields show the number of hard links, owner, group, file size, last modified date, and filename

Changing Permissions

The chmod command is used to change file permissions. There are two main ways to use it:

  1. Symbolic method:

     chmod [who][operation][permissions] filename
    

    Example: chmod u+x script.sh adds execute permission for the owner.

  2. Numeric method:

     chmod ### filename
    

    Example: chmod 755 script.sh sets rwx for owner, rx for group and others.

Special Permissions

Linux also has special permissions:

  1. SetUID (Set User ID)

  2. SetGID (Set Group ID)

  3. Sticky Bit

These are set using chmod with special flags:

chmod u+s file  # SetUID
chmod g+s file  # SetGID
chmod +t directory  # Sticky Bit

Part 2: User and Group Management

Creating Users

To create a new user:

sudo useradd -m -s /bin/bash username
sudo passwd username

Modifying Users

To modify existing users:

sudo usermod [options] username

Common options include -l (change username), -d (change home directory), and -s (change default shell).

Deleting Users

To delete a user:

sudo userdel -r username

The -r option removes the user's home directory and mail spool.

Group Management

  • Create a group: sudo groupadd groupname

  • Add a user to a group: sudo usermod -aG groupname username

  • Remove a user from a group: sudo gpasswd -d username groupname

  • Delete a group: sudo groupdel groupname

Changing File Ownership

  • Change owner: sudo chown newowner filename

  • Change owner and group: sudo chown newowner:newgroup filename

  • Change only group: sudo chgrp newgroup filename

Part 3: Advanced Access Control

Access Control Lists (ACLs)

ACLs provide more fine-grained access control:

  1. Set an ACL: setfacl -m u:username:rwx file

  2. View ACLs: getfacl file

  3. Remove an ACL: setfacl -x u:username file

Conclusion

Learing Linux permissions and user management is essential for any system administrator. By understanding these concepts and commands, we'll be able to maintain a secure and efficient Linux environment. Remember to always use these powerful tools responsibly and double-check the commands before executing them, especially when using sudo.

Practice makes perfect, so don't hesitate to set up a test environment and experiment with these commands. Happy Linux administrating!

0
Subscribe to my newsletter

Read articles from Md Saif Zaman directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Md Saif Zaman
Md Saif Zaman