How to use the useradd and adduser command in Linux ?

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

Your Cart