Understanding users, groups and File permissions is essential in Linux with this we can change permissions assigned to individual or as a group and can restrict access with the permissions in Linux. Let’s dive into this further.
Users:
User is someone that interacts with the system.
Let's consider a class in school or college. We have a teacher who has the authority to teach and enforce rules when needed. Similarly, in Linux, we have a root user who has all the powers, while a regular user functions like a student.
Root user - Has all access to create, change & delete. So root user should be used carefully.
Regular user - Have limited access granted by the root user.
The /etc/passwd
file stores user information and is the default configuration file.
Create User - useradd <user_name>
Delete user - userdel <user_name>
To set password for user - passwd <user_name>
Groups:
Users are added to groups, so instead of assigning permissions individually, we add them to groups and assign access. Managing permissions is easier with groups.
Imagine you're in college. Students in the same grade are grouped together in a class. This is similar to how the concept of groups works.
The /etc/group
file stores group information and is the default configuration file.
Create Group -
groupadd <group_name>
Delete group -
groupdel <group_name>
Adding user in the group -
usermod -aG <group_name> <user_name>
(this command is used to append the user to group which already has users)
gpasswd -a <user_name> <group_name>
(this command is used to add multiple users to group)
Removing user from the group -
gpasswd -d <user_name> <group_name>
To set group password -
gpasswd <group_name>
Permissions in Linux
Permissions in Linux tell the system who can do what with a file or folder. There are three basic permissions:
a) Read (r) 📖
The read permission allows a user to view a file or list the contents of a directory.
If a directory has read permission, you can see what files are inside.
Numerical value is 4
b) Write (w) ✏️
The write permission allows a user to modify a file or add/remove files in a directory.
It’s like being able to edit a document or rearrange items in a folder.
Numerical value is 2
c) Execute (x) ▶️
The execute permission allows a user to run a program or enter a directory.
For a directory, it allows you to “go into” the folder and access its contents.
Numerical value is 1
Do ls -l to check the permissions of files and directory,
Here, (d) is the directory and (-) indicates file at the start.
rwx : permission for owner’s
r-x : permission for groups
r-x : permission for others
Here,
Changing permissions
We have chmod, chown and chgrp. 3 permissions are:
chmod - (change mode) : change permissions for a file or directory
chown - (change owner) : Assigns a new user as the owner of a file or directory.
chgrp - (change group) : changes the group
TASKS:
1️⃣ User & Group Management
Create a user
devops_user
and add them to a groupdevops_team
.Set a password and grant sudo access.
Restrict SSH login for certain users in
/etc/ssh/sshd_config
.
Let’s solve this:
2️⃣ File & Directory Permissions
Create
/devops_workspace
and a fileproject_notes.txt
.Set permissions:
Owner can edit, group can read, others have no access.
Use
ls -l
to verify permissions.
Conclusion🚀:
So, whether you’re a beginner or just brushing up, remember that users, groups, and permissions are your toolkit for keeping your Linux system secure, organized, and efficient! 🛠️👨💻👩💻
Hope this helps you on your Linux journey! 🚀