adduser Command in Linux

Learn via video courses
Topics Covered

Overview

In the vast and diverse ecosystem of Linux, user management is a crucial responsibility of system administrators. This includes the addition of new users, a task often accomplished with the adduser command in Linux. The adduser command is a simple yet powerful tool that allows administrators to create a new user account in a Linux system. This command sets up a secure environment for the new user by creating a home directory, setting up appropriate file permissions, and more.

Syntax of adduser Command in Linux

The basic syntax of the adduser command in Linux is as follows:

Where:

  • adduser: This is the command itself, used to add a new user.
  • [options]: These are optional flags or parameters that can be used to modify the behavior of the command.
  • username: This is the name of the new user that you want to create.

Options in adduser Command in Linux

  1. --home: Defines the new user's home directory.

    For example -

    Output:

    This command creates a new user named 'newuser' with the home directory '/home/newuser'.

  2. --shell: Specifies the new user's login shell.

    For example -

    Output:

    This command creates a new user named 'newuser' and sets the login shell to '/bin/zsh'.

  3. --disabled-password: Creates a new user without setting a password.

    For example -

    Output:

    This command creates a new user named 'newuser', but does not set a password for the account.

Example Usages

  • Adding a new user with the default settings.:

    Output:

    Explanation: The command created a new user named 'newuser' with default settings.

  • Adding a new user and assigning them to multiple groups.:

    Output:

    Explanation: This command adds a new user 'newuser' and assigns them to 'group1' and 'group2'.

Tips

  • The adduser command must be run with superuser privileges, typically by using 'sudo'.

  • Use the '--no-create-home' option if you don't want to create a home directory for the new user.

  • If you want to add a system user, use the '--system' option.

Advanced Use Cases of adduser Command in Linux

  • Adding a system user.:

    Output:

    Explanation: This command creates a system user named 'newuser'. System users are usually used to run services or daemons and don't have a home directory by default.

  • Adding a user with a specific user ID (UID).:

    Output:

    Explanation: This command creates a new user named 'newuser' with a specific UID '1234'.

  • Creating a new user and setting a custom comment for the user.:

    Output:

    Explanation: This command creates a new user 'newuser' and sets the comment field of the user's entry in the /etc/passwd file to 'This is a test user'.

Conclusion

  • The adduser command in Linux is a powerful tool for managing users in a Linux environment.

  • Options such as '--home', '--shell', '--disabled-password' allow for customization of the new user's environment.

  • Remember to run adduser with superuser privileges using 'sudo'.

  • For more complex user setups, you can use options like '--system', '--uid', and '--comment' for greater control over the new user's configuration.