Linux User Management: A Comprehensive Guide
Linux is a multi-user operating system, meaning that multiple users can work on the system simultaneously. Efficient user management is crucial for maintaining security, system integrity, and effective collaboration. In this guide, we'll explore how Linux handles user management, including creating, modifying, and deleting users, managing user permissions, and understanding user-related files.
Types of users in linux:
1. Regular Users or Normal Users:
These are standard users who can perform tasks like creating files and running applications but do not have administrative privileges.
Example:
Name: shani, anil, rahul etc
Home Directory: /home/username/
Shell: /bin/bash
2. Superuser (Root):
This is the administrative user with full control over the system, capable of modifying system settings, installing software, and managing other users.
Example:
Name: root
Home Directory: /root
Shell: /bin/bash
3. System User :
A system user in Linux is a special type of user account that is created and used by the operating system or system services, rather than by human users. These accounts are generally used to run system processes or background services and have specific permissions and restrictions.
Example:
Name: ftp, ssh, nginx etc
Home Directory: /var/ftp etc
Shell: /sbin/nologin
Common User Management Commands
Linux provides several commands to manage users and groups, most of which require root (administrator) privileges. Below are the most commonly used commands:
1. Adding a New User
To add a new user to the system, the useradd command is used. This command creates a new user and assigns it a home directory and other default settings.
sudo useradd <options> username
When you create a new user a default activity will happen
- User home directory created
- Unique UID, GID created
- Shell for user
- User entry in /etc/passwd file
Options:
-u : userid
-G: Secondary group ID or name
-g: Primary group ID or name
-d: Home directory path
-c: Comments or description about user
-s: Shell
-m: Home directory
Example
sudo useradd -g developer -s /bin/bash -c “developer user” -m -d /home/shanikr shanikr
Note: Make Sure group exists if mention group name
After creating the user, it's advisable to set a password
Change Password
sudo passwd username
You’ll be prompted to enter and confirm the password.
Viewing User Information
The id command shows information about a user, including their UID, GID, and the groups they belong to:
id username
You can also use the whoami command to check which user is currently logged in:
whoami
Check List of users
cat /etc/passwd
This file contains basic information about all users, including their username, UID, GID, home directory, and default shell.
2. Modifying User Details
To modify a user account, you can use the usermod command. This command allows you to change the username, home directory, login shell, and more.
Change the username
sudo usermod -l new_username old_username
Change the user’s home directory
sudo usermod -d /new/home/directory username
Add User In Secondary Group
sudo usermod -aG new_group_name|id username
Change Default Group Or Primary Group
sudo usermod -g new_group_name|id username
3. Delete or Remove User
If you want to delete a user, you can use the userdel command. If you also want to delete the user’s home directory, use the -r option:
sudo userdel -r username
This removes both the user and their home directory.
Forcefully delete user even user is loggedin
sudo userdel -rf username
4. Managing Groups
In Linux, users can be part of one or more groups. Groups allow for more efficient management of user permissions, as you can assign permissions to a group rather than to individual users.
Creating a New Group
To create a new group, use the groupadd command
sudo groupadd groupname
Adding a User to a Group
To add a user to an existing group, use the usermod command with the -aG flag:
sudo usermod -aG groupname username
Viewing Group Information
The groups command displays the groups to which a user belongs:
groups username
Check List of Groups
cat /etc/group
Renaming a Group
To rename a group, use the groupmod command with the -n option:
sudo groupmod -n new_groupname old_groupname
Delete Or Remove Group
To delete a group, use the groupdel command:
sudo groupdel groupname
This removes the group from the system but does not affect users or files that previously belonged to the group.
To Get All Options or flags or commands
use --help flag to get more options of command
command --help
Example
useradd --help
usermod --help
groupdd --help
These command will give you all available options of the givencommand
Keep learning keep growing!
Comments