5 Useradd Command Examples, With Explanations

I have previously written these two articles for creating users in the main used Linux distributions.

Now, I will show you 10 more useradd oneliners, for creating users in all the Linux and Unix systems.

Example 1.The most used useradd command:

$ sudo useradd -m -d /home/mike1 -s /bin/bash -c "the mike1 user" -U mike1

Explanation:

  • -m -d /home/mike1 : the -m argument creates the /home/mike1 homedirectory, specified by the -d argument
  • -s /bin/bash : the -s is used for specifing the user’s default shell, /bin/bash in this case
  • -c “message” : extra information about the user

Example 2:

$ sudo useradd -m -d /home/geeks/mike2 -s /bin/zsh -c "the mike2 user" -u 1099 -g 1050 mike2

Explanation:

  • -m -d /home/geeks/mike2 : the -m argument creates the /home/geeks/mike2 homedirectory, specified by the -d argument . as you can notice, the homedir can be different that /home/user_name
  • -s /bin/zsh : the -s is used for specifing the user’s default shell, /bin/zsh in the case
  • -c “the mike2 user” : extra information about the user
  • -u 1099 : the new user’s UID, in this case 1099
  • -g 1050 : the user belongs to the group with the 1050 GID

Example 3:

$ sudo useradd -m -d /home/mike3 -s /usr/sbin/nologin -c "nologin user" -u 1098 mike3

Explanation:

  • -m -d /home/mike3 : the -m argument creates the /home/mike3 homedirectory, specified by the -d argument
  • -s /usr/sbin/nologin : the -s is used for specifing the user’s default shell, in this case /usr/sbin/nologin . mike3 cannot login to the system with su, but can login by ssh. Read more about the nologin shells here.
  • -c “nologin user” : extra information about the user
  • -u 1098 : the new user’s UID, in this case 1098

Example 4:

$ sudo useradd -m -d /home/mike4 -k /etc/custom.skell -s /bin/tcsh -c "mike4 user" -u 1097 mike4

Explanation:

  • -m -d /home/mike4 : the -m argument creates the /home/mike4 homedirectory, specified by the -d argument
  • -s /bin/tcsh : the -s is used for specifing the user’s default shell, /bin/tcsh in this case
  • -k /etc/custom.skel : another skeleton directory, /etc/custom.skell in this case, different than the default skeleton directory /etc/skel
  • -c “mike4 user” : extra information about the user
  • -u 1097 : the new user’s UID, in this case 1097

Example 5:

$ sudo useradd -M -N -r -s /bin/false -c "system user" sys_user

Explanation:

  • -M : the -M argument tells the system not to create a home directory
  • -N : the -N argument tells the system not to create a group having the user’s name
  • -r : the -r arguments is for creating a system user
  • -s /bin/false : the -s is used for specifing the user’s default shell, /bin/false in this case. The users having /bin/false as the default shell, cannot login to the system. Read more about the nologin shells here.
  • -c “system user” : extra information about the user

Share and Enjoy

  • Twitter
  • Facebook
  • Google Plus
  • Email
  • RSS
Tagged with: , , , , , , , ,
Posted in The Linux and Unix Articles!
0 comments on “5 Useradd Command Examples, With Explanations
1 Pings/Trackbacks for "5 Useradd Command Examples, With Explanations"

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>