How to use the command 'useradd' (with examples)

How to use the command 'useradd' (with examples)

The useradd command is used to create a new user in Linux-based operating systems. It allows system administrators to easily add new users to the system with various customizable options. This command is useful in managing user accounts and permissions on a Linux system.

Use case 1: Create a new user

Code:

sudo useradd username

Motivation: The motivation for this use case is to add a new user to the system with the default settings. This is the simplest use case of the useradd command and is commonly used when creating user accounts with default configuration.

Explanation:

  • sudo: This command is used to execute the following command as a superuser or with administrative privileges.
  • useradd: This is the main command used to add a new user to the system.
  • username: This is the name of the user to be created. Replace it with the desired username.

Example output: No output will be displayed upon successful execution of this command. To verify the new user, you can check the /etc/passwd file or use the id command.

Use case 2: Create a new user with the specified user id

Code:

sudo useradd --uid id username

Motivation: This use case is helpful when you need to create a user with a specific user ID (UID) instead of letting the system assign it automatically. Assigning specific UIDs can be useful for managing permissions or when migrating users between systems.

Explanation:

  • --uid id: This option allows you to specify the desired UID for the new user. Replace id with the desired UID number.
  • Other arguments have the same meaning as in the previous use case.

Example output: No output will be displayed upon successful execution of this command. To verify the new user, you can check the /etc/passwd file or use the id command.

Use case 3: Create a new user with the specified shell

Code:

sudo useradd --shell path/to/shell username

Motivation: Sometimes, you may want to assign a specific shell to a user. This use case allows you to specify the path to the desired shell binary when creating a new user.

Explanation:

  • --shell path/to/shell: This option allows you to specify the path to the user’s login shell. Replace path/to/shell with the actual path to the desired shell binary.

Example output: No output will be displayed upon successful execution of this command. To verify the new user, you can check the /etc/passwd file or use the id command.

Use case 4: Create a new user belonging to additional groups

Code:

sudo useradd --groups group1,group2,... username

Motivation: In some cases, you may need to create a user that belongs to additional groups apart from the default group. This use case allows you to add the new user to multiple groups during user creation.

Explanation:

  • --groups group1,group2,...: This option lets you specify the additional groups to which the new user will belong. Replace group1,group2,... with the names of the desired groups, separated by commas.

Example output: No output will be displayed upon successful execution of this command. To verify the new user’s groups, you can use the id or groups command.

Use case 5: Create a new user with the default home directory

Code:

sudo useradd --create-home username

Motivation: By default, when creating a new user, the system creates a home directory for that user. This use case is useful when you want to create a user with a default home directory.

Explanation:

  • --create-home: This option instructs the system to create a home directory for the new user.

Example output: No output will be displayed upon successful execution of this command. To verify the new user’s home directory, you can check the /etc/passwd file or use the getent passwd username command.

Use case 6: Create a new user with the home directory filled by template directory files

Code:

sudo useradd --skel path/to/template_directory --create-home username

Motivation: This use case is helpful when you want to provide a set of pre-defined template files to be copied to the new user’s home directory. It allows for easy customization of the user’s environment upon creation.

Explanation:

  • --skel path/to/template_directory: This option specifies the path to a directory containing the template files to be copied to the user’s home directory. Replace path/to/template_directory with the actual path to the template directory.

Example output: No output will be displayed upon successful execution of this command. To verify the new user’s home directory and the copied template files, you can check the /etc/passwd file or inspect the user’s home directory.

Use case 7: Create a new system user without the home directory

Code:

sudo useradd --system username

Motivation: System users are typically used to run system services or processes that don’t require interactive logins. This use case is useful for creating system users without creating a home directory.

Explanation:

  • --system: This option sets the user type to a system user, which is identified by a UID lower than the minimum value defined in the UID_MIN configuration (usually 1000).

Example output: No output will be displayed upon successful execution of this command. To verify the new system user, you can check the /etc/passwd file or use the id command.

Conclusion:

The useradd command is a versatile tool in managing user accounts in a Linux system. It provides various options to customize user creation, such as specifying user IDs, shells, groups, and home directories. By understanding these use cases, you can effectively create and configure new users based on your requirements.

Related Posts

How to use the command "doctl kubernetes options" (with examples)

How to use the command "doctl kubernetes options" (with examples)

The doctl kubernetes options command provides a way to retrieve information about available options for working with Kubernetes clusters in DigitalOcean.

Read More
How to use the command 'avrdude' (with examples)

How to use the command 'avrdude' (with examples)

The avrdude command is a driver program for programming Atmel AVR microcontrollers.

Read More
How to use the command ttyplot (with examples)

How to use the command ttyplot (with examples)

This article will provide examples and explanations of various use cases for the ttyplot command, a real-time plotting utility for the command-line with data input from stdin.

Read More