CodeGym /Courses /Docker SELF /User Management: Commands adduser, usermod, passwd

User Management: Commands adduser, usermod, passwd

Docker SELF
Level 2 , Lesson 1
Available

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?

  1. A new entry is created in the /etc/passwd file for the user.
  2. A home directory is generated (e.g., /home/username).
  3. Basic settings are configured (e.g., default shell).
  4. 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:

  1. Set a password for the user.
  2. 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
Important:

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

  1. Add the user tester:

    sudo adduser tester
    
  2. Make sure the user is created:

    cat /etc/passwd | grep tester
    

Task 2: Configure user parameters

  1. Change the username:

    sudo usermod -l test_user tester
    
  2. Change the user's home directory:

    sudo usermod -d /home/test_user -m test_user
    
  3. Verify the changes are applied:

    ls -l /home/
    
  4. Change the user's shell:

    sudo usermod --shell /bin/zsh test_user
    
  5. Check the updated user data:

    cat /etc/passwd | grep test_user
    

Task 3: Set a new password for the user

  1. Set the password for the user:

    sudo passwd test_user
    
  2. 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"
  1. First, the ls -l command runs, which lists the files in the current directory.
  2. The output of the first ls command is redirected as input to the second grep command using a pipe |.
  3. 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
  1. First, the cat file.txt command runs, which displays the contents of the file file.txt on the screen.
  2. The output of the first cat command is redirected as input to the second grep command using a pipe |.
  3. The second command filters the lines it receives and outputs only the lines containing "Linux".
  4. Then, the output of the second grep command is redirected as input to the third wc command using a pipe |.
  5. The third command counts the number of lines it receives and displays it.

8. Common Mistakes and Tips

  1. 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.

  2. 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.

  3. 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.

  4. Values of UID and GID. Sometimes admins want to specify values for UID (user ID) and GID (group ID) when creating a user. For this, the useradd 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.

1
Task
Docker SELF, level 2, lesson 1
Locked
Creating a new user
Creating a new user
1
Task
Docker SELF, level 2, lesson 1
Locked
Modifying user parameters
Modifying user parameters
1
Task
Docker SELF, level 2, lesson 1
Locked
Change user shell
Change user shell
1
Task
Docker SELF, level 2, lesson 1
Locked
User password setup
User password setup
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION