User and Group Management
Now that you're comfortable navigating the file system and working with text files, it's time to learn how to manage the users and groups that interact with your Ubuntu system. In this lesson, you'll learn how to create, modify, and manage user accounts and groups—essential skills for any system administrator.
Learning Goals:
- Create and manage user accounts
- Understand and work with user groups
- Set and change passwords
- Switch between user accounts
- Manage user privileges
Understanding Users and Groups
In Linux, every process and file is owned by a user and belongs to a group. This security model ensures that only authorized users can access specific resources.
Every user in Linux has a unique User ID (UID) and every group has a Group ID (GID). The first user created during installation (usually your account) typically has UID 1000, while system users have lower UIDs.
Viewing Current User Information
Let's start by checking who you are and what groups you belong to:
whoami
groups
id
The id command provides the most comprehensive information, showing your UID, GID, and all groups you belong to.
Creating and Managing Users
Creating New Users
To create a new user account, use the adduser command:
sudo adduser alice
You'll be prompted to set a password and fill in optional user information. The system automatically creates a home directory at /home/alice and sets up default configuration files.
Setting User Passwords
You can set or change passwords using the passwd command:
# Change your own password
passwd
# Change another user's password (requires sudo)
sudo passwd alice
Always use strong passwords! A good password should be at least 12 characters long and include a mix of uppercase, lowercase, numbers, and special characters.
Working with User Groups
Creating Groups
Groups allow you to manage permissions for multiple users simultaneously:
sudo addgroup developers
Adding Users to Groups
To add a user to an existing group:
sudo usermod -aG developers alice
The -aG flags are important: -a means "append" (so you don't remove existing groups) and -G specifies the group to add.
Viewing Group Information
getent group developers
Switching Between Users
Using su Command
The su (substitute user) command allows you to switch to another user:
# Switch to alice (requires alice's password)
su - alice
# Switch back to your original user
exit
Using sudo for Privileged Access
sudo -u alice whoami
Modifying and Deleting Users
Modifying User Properties
Use usermod to change user properties:
# Change user's home directory
sudo usermod -d /home/newhome alice
# Change user's login name
sudo usermod -l alice_new alice
# Change user's primary group
sudo usermod -g developers alice
Deleting Users
To remove a user account:
sudo deluser alice
# Remove user and their home directory
sudo deluser --remove-home alice
User Configuration Files
Several important files control user and group information:
- /etc/passwd
- /etc/shadow
- /etc/group
cat /etc/passwd
Each line shows: username:password:UID:GID:description:home_directory:shell
sudo cat /etc/shadow
Contains encrypted passwords and password aging information.
cat /etc/group
Shows group names, passwords, GIDs, and member lists.
Common Pitfalls
- Forgetting the
-aflag withusermod -G: This removes the user from all other secondary groups - Deleting users without backing up data: Always backup important files before deleting accounts
- Using weak passwords: This creates security vulnerabilities
- Not logging out of privileged sessions: Always
exitfromsuorsudosessions - Modifying system UIDs/GIDs: Changing system user IDs can break system functionality
Summary
In this lesson, you learned how to:
- Create and manage user accounts with
adduseranddeluser - Work with groups using
addgroupandusermod - Set and change passwords with
passwd - Switch between users using
suandsudo - Understand the key configuration files in
/etc/
User and group management forms the foundation of Linux security and multi-user environments. These skills are essential for maintaining a secure and well-organized system.
Show quiz
- What command would you use to create a new user named "bob"?
- How do you add user "bob" to the "developers" group without removing him from his current groups?
- Which file contains encrypted password information for users?
- What's the difference between
su usernameandsu - username? - Why is the
-aflag important when usingusermod -G?
Answers:
sudo adduser bobsudo usermod -aG developers bob/etc/shadowsu usernameswitches user but keeps the current environment, whilesu - usernameprovides a fresh login environment- Without
-a, the user would be removed from all other secondary groups and only belong to the specified group