Cart 0
No products in the cart.

How to use the useradd and adduser command in Linux ?

How to use the useradd and adduser command in Linux ?

 

Useradd vs Adduser

Useradd and adduser are two commands that are used to create new user accounts in Linux. Both commands have the same purpose, but the syntax and options are slightly different.

 

Useradd

Useradd is a command that is used to create a new user account in Linux. It requires the user to provide the username, and can optionally set other user attributes, such as home directory, default shell, and group membership. 

 

 

Useradd Syntax

It's syntax is given below:

$ useradd [options] username

 

The options used with the useradd command are: 

  • -u : Specifies the user id for the new user
  • -g : Specifies the group id for the new user
  • -s : Specifies the default shell for the user
  • -m : Creates the user’s home directory
  • -d : Specifies the path of the user’s home directory
  • -e : Specifies the expiry date of the user
  • -c : Specifies the comment for the user

 

Examples of using useradd

1. Create a user named testuser with home directory /home/user and default shell /bin/bash:
$ useradd -m -d /home/user -s /bin/bash -g dev -G devuser testuser

This will create a user named testuser with home directory /home/user and default shell /bin/bash. The user will be added to the "dev" group and the "devuser" secondary group.

 

2. To create a user with username "john", the command used is: 
useradd -u 1000 -g 100 -s /bin/bash -m -d /home/john -e 2021-12-31 -c "John Doe" john

 

 

Adduser

Adduser is a command that is used to create a new user account in Linux. It is a wrapper for useradd, and it provides a simplified syntax that is easier to use. 

 

Adduser Syntax

The syntax of Adduser command is given below:

$ adduser [options] username

 

The options used with the adduser command are: 

  • -g : Specifies the group name for the user
  • -s : Specifies the default shell for the user
  • -d : Specifies the path of the user's home directory
  • -e : Specifies the expiry date of the user
  • -c : Specifies the comment for the user
  •  

Examples of using Adduser

1. Create a user named testuser with home directory /home/user and default shell /bin/bash:
$ adduser --home /home/user --shell /bin/bash --ingroup dev --add_extra_groups devuser testuser

The user will be added to the "dev" group and the "devuser" secondary group.

 

2. To create a user with username "john", the command used is: 
$ adduser -g users -s /bin/bash -d /home/john -e 2021-12-31 -c "John Doe" john

 

[Need Linux Support ? We can help you. ]

 



CONCLUSION

 

This article covers how to use both Useradd and adduser command in linux. In fact, Useradd and adduser are two commands that are used to create new user accounts in Linux. Useradd requires a more complex syntax, but provides more options for setting user attributes. Adduser provides a simpler syntax and fewer options, but is easier to use.

 

How to fix Useradd and Adduser Command Not Found in Ubuntu ?

If you are using Ubuntu and you encounter the error "useradd: command not found" or "adduser: command not found", it means that the useradd and adduser commands are not installed on your system. This can be fixed by installing the shadow package. Here's how:

 

1. Open the Terminal window.

 

2. Use the following command to install the shadow package:

$ sudo apt-get install shadow

 

3. Enter your password when prompted.

 

4. Once the installation is complete, you can use the useradd and adduser commands.

 

5. To create a user, use the following command:

$ useradd <username>

 

6. To set the user’s password, use the following command:

$ passwd <username>

 

7. Enter the password twice when prompted.

 

That's it! You have now successfully installed the shadow package and fixed the useradd and adduser command not found errors.

 


icon

Sign up to Newsletter

...and receive a free support ticket.