User Management: Commands adduser
, usermod
, passwd
1. Why is it important to understand user management?
Today, we're diving into user management because it's a key part of Linux administration. Creating users, setting up their environment, and managing their passwords — all of this will become an essential toolkit for your work. Let's go!
Often in Linux, many users operate on the server. The system grants each of them certain permissions to protect data and avoid chaos. You need to know how to add new users (like developers in your team), configure their environment (e.g., changing the shell), and restrict access when needed.
2. Creating a User with adduser
Linux provides the adduser
command, which is a more user-friendly variant of the useradd
command. It allows you to quickly and easily create new users:
Main Syntax
adduser username
What Happens When You Create a User?
- A new entry is created in the
/etc/passwd
file for the user. - A home directory is generated (e.g.,
/home/username
). - Basic settings are configured (e.g., default shell).
- You’ll be asked to enter a password and additional information (such as name, phone number — but these are optional).
Example
Let's create a new user dev_user
:
sudo adduser dev_user
After entering this command, you’ll be prompted to:
- Set a password for the user.
- Enter additional information, which you can skip by simply pressing
Enter
.
Verification
To confirm the user has been created, you can check the /etc/passwd
file:
cat /etc/passwd | grep dev_user
You’ll see a line containing the information about the dev_user
.
3. Configuring a User with usermod
When a user is already created, you might need to change their settings—like renaming them, setting a different shell, or adding them to a group.
Renaming a User
First, make sure the user is not logged in to the system. Then run the command:
sudo usermod -l new_name old_name
Example:
sudo usermod -l developer dev_user
Now the user dev_user
will be called developer
. However, their home directory will still have the old name for now.
Changing the Default Shell
Suppose you need to change the user's shell to /bin/zsh
. To do so, execute this command:
sudo usermod --shell /bin/zsh developer
Check if the shell has been updated using:
cat /etc/passwd | grep developer
Changing the Home Directory
If you want the new user to work in a different home directory, do the following:
sudo usermod -d /new/path developer
If the directory doesn’t exist yet, use the -m
option to have it created and the contents of the old directory moved:
sudo usermod -d /home/new_developer -m developer
4. Managing Passwords with passwd
Setting and Changing Passwords
To set (or change) a user's password, use the following command:
sudo passwd username
Example:
sudo passwd developer
The system will ask you to enter the new password twice to make sure there are no typos.
Forcing Password Change on Next Login
This can be useful if you're creating users and want them to set their own password on their first login:
sudo passwd -e developer
After this, the user developer
will see a message prompting them to change their password upon logging in.
5. Practice
Task 1: Create a new user
Add the user
tester
:sudo adduser tester
Make sure the user is created:
cat /etc/passwd | grep tester
Task 2: Configure user parameters
Change the username:
sudo usermod -l test_user tester
Change the user's home directory:
sudo usermod -d /home/test_user -m test_user
Verify the changes are applied:
ls -l /home/
Change the user's shell:
sudo usermod --shell /bin/zsh test_user
Check the updated user data:
cat /etc/passwd | grep test_user
Task 3: Set a new password for the user
Set the password for the user:
sudo passwd test_user
Force the user to change the password at the next login:
sudo passwd -e test_user
6. Getting to Know Pipes |
in Linux
Pipes |
are a mechanism in Unix/Linux that lets you pass the output of one command (stdout) as the input (stdin) to another command. It’s a super handy tool that allows you to chain commands, creating "pipelines" to process data.
How does it work?
When you use a pipe between commands, Linux redirects the standard output of the first command into the standard input of the next one. This makes it efficient to handle large amounts of data without temporary files.
Syntax:
command1 | command2 | command3
command1
generates data.command2
processes the received data.command3
performs further processing or saves the result.
Example:
List files and filter by pattern:
ls -l | grep ".txt"
Here ls -l
lists files, and grep
filters only files with the .txt
extension.
Count lines containing a specific word:
cat file.txt | grep "Linux" | wc -l
The file content is displayed sequentially, lines with "Linux" are searched, and then their count is calculated.
Advantages of pipes:
- Simplifies complex operations.
- Minimizes the use of temporary files.
- Enables combining powerful Linux utilities for task automation.
7. Introduction to grep in Linux
grep
(Global Regular Expression Print) is a command-line utility in Unix/Linux for searching lines in files that match a specific pattern. It’s widely used for data searching and filtering thanks to its support for regular expressions and flexible options.
How does grep
work?
grep
takes a text file (or standard input) as input, searches for lines that match the specified pattern, and outputs them.- If no match is found, the command outputs nothing.
Examples
List files and filter by pattern:
ls -l | grep ".txt"
- First, the
ls -l
command runs, which lists the files in the current directory. - The output of the first
ls
command is redirected as input to the secondgrep
command using a pipe|
. - The second command filters the lines it receives and outputs only the lines containing ".txt".
Count lines containing a specific word:
cat file.txt | grep "Linux" | wc -l
- First, the
cat file.txt
command runs, which displays the contents of the file file.txt on the screen. - The output of the first
cat
command is redirected as input to the secondgrep
command using a pipe|
. - The second command filters the lines it receives and outputs only the lines containing "Linux".
- Then, the output of the second
grep
command is redirected as input to the thirdwc
command using a pipe|
. - The third command counts the number of lines it receives and displays it.
8. Common Mistakes and Tips
Error "user is currently logged in" when changing the username. If the user is logged into the system, you won’t be able to change their name. Make sure the user logs out first.
Errors when changing the home directory. If you forget to specify
-m
, the contents of the old directory won’t transfer to the new one. The user might lose their data.Forgot to set a password for the user. After creating a user with
adduser
, make sure to check if they have a set password. Without a password, they won’t be able to log in.Values of
UID
andGID
. Sometimes admins want to specify values forUID
(user ID) andGID
(group ID) when creating a user. For this, theuseradd
command is used, but it’s a topic for advanced Linux study.
9. Practical Application
Imagine: you’re the server admin for a company. Developers come to you, and each of them demands access to the system with personalized settings. You'll be able to:
- Create users and assign them the necessary access.
- Set up their environment (e.g., shell) according to their preferences.
- Manage passwords and ensure security.
These commands will also come in handy during interviews for DevOps or system administrator positions. You might be asked about how to create and configure users, or even be given this task to complete as part of the interview.
Cheat Sheet: Key Commands
Command | Description |
---|---|
sudo adduser username |
Creates a new user. |
sudo usermod -l new old |
Changes the username. |
sudo usermod --shell path name |
Sets the default shell. |
sudo usermod -d path name |
Changes the home directory. |
sudo passwd name |
Sets a new password. |
sudo passwd -e name |
Forces the user to change their password upon their next login. |
Practice! Linux is all about hands-on experience. The more you experiment with commands, the easier it'll be for you to work with real systems.
GO TO FULL VERSION