Linux Permissions and User Management: A Starting Guide
Table of contents
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:
Owner permissions
Group permissions
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:
Symbolic method:
chmod [who][operation][permissions] filename
Example:
chmod u+x
script.sh
adds execute permission for the owner.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:
SetUID (Set User ID)
SetGID (Set Group ID)
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:
Set an ACL:
setfacl -m u:username:rwx file
View ACLs:
getfacl file
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!
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