In Linux, users and groups are fundamental to managing access control, defining who can access what resources, and ensuring system security. This page will explore how to manage users and groups, including creating, modifying, and deleting them, as well as setting appropriate permissions.
A user in Linux is an account that can log in to the system and perform tasks. Each user has a unique username, user ID (UID), and a home directory where personal files and configurations are stored.
A group is a collection of users. Groups are used to simplify the management of permissions. Instead of assigning permissions to each user individually, you can assign them to a group, and all members of that group inherit those permissions.
- System Users: These accounts are used by system processes and services. They typically have a UID below 1000 and do not have a home directory.
- Regular Users: These are the accounts created for human users. They usually have a UID starting from 1000 and a home directory in
/home
.
To add a new user, use the useradd
command:
sudo useradd username
- By default, this command creates a user with the specified
username
, assigns the next available UID, and creates a home directory at/home/username
.
-
Specify Home Directory:
sudo useradd -m -d /custom/home/dir username
-m
: Creates a home directory.-d
: Specifies a custom home directory.
-
Assigning a Shell:
sudo useradd -s /bin/bash username
-s
: Specifies the login shell.
-
Setting an Expiry Date:
sudo useradd -e 2024-12-31 username
-e
: Specifies an expiry date for the account.
To set a password for a user, use the passwd
command:
sudo passwd username
This command prompts you to enter and confirm a password for the specified user.
To modify an existing user account, use the usermod
command:
-
Change a User's Home Directory:
sudo usermod -d /new/home/dir -m username
-d
: Specifies the new home directory.-m
: Moves the content of the old home directory to the new one.
-
Change a User's Shell:
sudo usermod -s /bin/zsh username
-s
: Specifies the new login shell.
-
Lock or Unlock a User Account:
sudo usermod -L username # Lock sudo usermod -U username # Unlock
-L
: Locks the user account.-U
: Unlocks the user account.
To delete a user, use the userdel
command:
sudo userdel username
-
Delete a User and Their Home Directory:
sudo userdel -r username
-r
: Removes the user's home directory and mail spool.
To add a new group, use the groupadd
command:
sudo groupadd groupname
This creates a new group with the specified groupname
.
To add a user to a group, use the usermod
command with the -aG
option:
sudo usermod -aG groupname username
-aG
: Adds the user to the specified group(s) without removing them from other groups.
To view the groups a user belongs to, use the groups
command:
groups username
A user's primary group is the group that owns files created by the user. To change the primary group, use the usermod
command:
sudo usermod -g groupname username
-g
: Specifies the new primary group.
To delete a group, use the groupdel
command:
sudo groupdel groupname
This removes the specified group from the system.
The /etc/passwd
file contains user account information, including usernames, UIDs, home directories, and default shells. Each line represents a user, with fields separated by colons (:
).
Example entry:
username:x:1001:1001::/home/username:/bin/bash
The /etc/shadow
file stores hashed passwords and related information. It is readable only by the root user for security reasons.
Example entry:
username:$6$hashvalue:18442:0:99999:7:::
The /etc/group
file contains group information, including group names, GIDs, and group members. Each line represents a group.
Example entry:
groupname:x:1001:user1,user2
To create a new user and add them to an existing group:
sudo useradd -m -s /bin/bash -G groupname username
sudo passwd username
To change a user's primary group and add them to multiple secondary groups:
sudo usermod -g primarygroup -G group1,group2 username
To lock an account that hasn't been used recently:
sudo passwd -l username
This locks the account until an administrator unlocks it.
- Least Privilege Principle: Always assign the minimum necessary permissions to users and groups.
- Regularly Audit Accounts: Periodically review user accounts and groups to ensure that only active and necessary accounts exist.
- Use Strong Passwords: Enforce strong password policies to enhance security.
- Lock Unused Accounts: Lock or remove accounts that are no longer in use to prevent unauthorized access.
Understanding and effectively managing users and groups in Linux is crucial for system administration. By mastering commands like useradd
, usermod
, groupadd
, and understanding the significance of files like /etc/passwd
and /etc/group
, you can ensure that your system is secure and that users have the appropriate level of access.
Next: Managing Permissions
Previous: File Compression and Archiving