Introduction to Linux User and Group Management
In Linux, user and group management is essential for controlling access to system resources. Each user has a unique ID, and groups are used to assign common permissions to multiple users.
- Users: Individual accounts created for people or services.
- Groups: A collection of users with shared permissions.
Key Commands Overview
Here are the basic commands you’ll be using:
- useradd: Adds a new user to the system.
- groupadd: Creates a new group.
- usermod: Modifies user information, including assigning users to groups.
- passwd: Sets a password for a user.
- id: Displays a user’s ID and group information.
Step-by-Step Guide
Adding a New User
To add a new user, you’ll use the useradd command. By default, this command creates a new user without assigning a password.
Syntax:
sudo useradd [options] username
Example:
sudo useradd john
This creates a new user called john without a password. To make the account usable, you must set a password.
Setting a Password:
sudo passwd john
You’ll be prompted to enter and confirm a new password for the user john.
Creating a New Group
To create a new group, you use the groupadd command. This helps when you want to manage multiple users with the same permissions.
Syntax:
sudo groupadd [groupname]
Example:
sudo groupadd devteam
This creates a new group called devteam.
Adding a User to a Group
Once the user and group are created, you can assign the user to the group using the usermod command.
Syntax:
sudo usermod -aG [groupname] [username]
Example:
sudo usermod -aG devteam john
This adds john to the devteam group. The -aG flag ensures the user is added to the group without removing them from any other groups they belong to.
Verifying User and Group Assignment
You can verify that a user has been successfully added to a group by using the id command.
Example:
id john
This displays the user’s ID and group memberships, confirming the changes.
Checking Group Memberships
You can also list all members of a specific group using the getent command.
Syntax:
getent group [groupname]
Example:
getent group devteam
This shows all users who are members of the devteam group.
Practical Hands-on Exercise
Follow these steps to practice adding users and groups:
Add a new user called student1 to the system.
sudo useradd student1
Set a password for student1.
sudo passwd student1
Create a new group called students.
sudo groupadd students
Add student1 to the students group.
sudo usermod -aG students student1
Verify that student1 is in the students group.
id student1