Linux create user Using `useradd`

Introduction

Learn how to create and configure a user in Linux using the `useradd` command. This comprehensive guide covers creating a user, assigning groups, and configuring sudo privileges without a password.

Managing user accounts is a fundamental task for any Linux system administrator. Whether you are setting up a new server or adding users to an existing system, understanding how to create and configure users efficiently is crucial. In this tutorial, we will explore how to use the `useradd` command to create a new user in Linux, assign them to specific groups, and enable them to execute sudo commands without requiring a password.

Why Use `useradd` for User Creation?

The `useradd` command is a powerful tool that allows system administrators to create user accounts with specific options such as User ID (UID), Group ID (GID), home directory, shell, and more. Unlike graphical user interfaces, `useradd` provides a streamlined way to automate user creation through scripts, making it ideal for managing large numbers of users or configuring servers.

Creating a User with `useradd` Command

Step 1: Define User Specifications

Let's create a user named `test` with the following specifications:
  • User ID (UID): 1234
  • Group ID (GID): 1002
  • Supplementary Groups: IT, vboxusers
  • Default Shell: /bin/bash
  • Home Directory: /home/test
  • Password: Whoami12345678

Step 2: Run the `useradd` Command

To create the user with these specific options, execute the following command in your terminal:
sudo useradd test -d /home/test -s /bin/bash -u 1234 -g 1002 -G IT,vboxusers  ; echo -e "Whoami12345678\nWhoami12345678\n" | sudo passwd test

Explanation of the Command:

  • test: The username being created.
  • -d /home/test: Specifies the home directory for the user.
  • -s /bin/bash: Sets the default shell for the user.
  • -u 1234: Assigns a specific User ID (UID) to the user.
  • -g 1002: Assigns the user to a specific primary group (GID 1002).
  • -G IT,vboxusers: Adds the user to supplementary groups IT and vboxusers.
  • passwd test: Sets the password for the user.

Step 3: Verify User Creation

After creating the user, it is essential to verify that the user has been configured correctly. You can do this by using the id command:

id test

The expected output should look like this:

uid=1234(test) gid=1002(IT) groups=1002(IT),981(vboxusers)

This output confirms that the user test has been created with the specified User ID, Group ID, and supplementary groups.

Configuring Sudo Privileges Without a Password

Granting sudo privileges without a password is often necessary for automation scripts or specific administrative tasks. Here's how to configure this for the test user.

Step 1: Modify the Sudoers File

To allow the test user to run sudo commands without being prompted for a password, you need to modify the sudoers file. You can do this by appending the following line:

echo "test ALL=(ALL) NOPASSWD:ALL" | sudo tee -a /etc/sudoers

Step 2: Validate the Sudoers File

It's crucial to ensure that the sudoers file is valid and free of syntax errors. You can check this by running:

sudo visudo -c

If the command returns no errors, your configuration is correct.

Step 3: Test Sudo Access

Finally, test the sudo configuration to ensure that the user test can execute commands without entering a password:

sudo cat /etc/hosts

If the command executes without prompting for a password, the configuration is successful.

Frequently Asked Questions (FAQs)

What is the difference between useradd and adduser?

useradd is a low-level utility for creating users, providing more control over the configuration, while adduser is a higher-level script that simplifies the process and is more user-friendly, especially for new administrators.

Can I create a user without assigning a password?

Yes, you can create a user without assigning a password by omitting the password-related options. However, such a user will not be able to log in until a password is set.

How can I delete a user in Linux?

To delete a user, you can use the userdel command followed by the username. To remove the user along with their home directory, use:

sudo userdel -r test

How do I lock a user account in Linux?

You can lock a user account by using the passwd command with the -l option:

sudo passwd -l test

This prevents the user from logging in.

Conclusion

Creating and managing user accounts in Linux is a fundamental skill for system administrators. The useradd command provides a robust and flexible way to create users with specific configurations. Additionally, configuring sudo access without a password enhances security while maintaining efficiency in administrative tasks.

By following this tutorial, you have learned how to create a user with useradd, assign groups, and configure passwordless sudo access. These skills are essential for managing a secure and efficient Linux environment. Thank you for reading the huuphan.com page!

The result, Linux create user


Linux create user

Comments

Popular posts from this blog

Linux Desktop Frozen? 5 Proven Fixes Before a Hard Reboot

Minecraft Bedrock on Linux: 4 Proven Ways to Play in 2026

DXVK in Wine on Linux: 7 Easy Setup Steps, Tested