Advertisement
Tritonio

The Ultimate Guide to Create Users in Linux / Unix

Mar 15th, 2021
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 KB | None | 0 0
  1. Method 1: Linux useradd Command — Create User With Default Configurations
  2.  
  3. This is a fundamental low level tool for user creation. To create user with default configurations use useradd as shown below.
  4.  
  5. Syntax: # useradd LOGIN-NAME
  6.  
  7.  
  8. While creating users as mentioned above, all the default options will be taken except group id. To view the default options give the following command with the option -D.
  9.  
  10. $ useradd -D
  11. GROUP=1001
  12. HOME=/home
  13. INACTIVE=-1
  14. EXPIRE=
  15. SHELL=/bin/sh
  16. SKEL=/etc/skel
  17. CREATE_MAIL_SPOOL=no
  18.  
  19.  
  20.  
  21. GROUP: This is the only option which will not be taken as default. Because if you don’t specify -n option a group with same name as the user will be created and the user will be added to that group. To avoid that and to make the user as the member of the default group you need to give the option -n.
  22. HOME: This is the default path prefix for the home directory. Now the home directory will be created as /home/USERNAME.
  23. INACTIVE: -1 by default disables the feature of disabling the account once the user password has expired. To change this behavior you need to give a positive number which means if the password gets expired after the given number of days the user account will be disabled.
  24. EXPIRE: The date on which the user account will be disabled.
  25. SHELL: Users login shell.
  26. SKEL: Contents of the skel directory will be copied to the users home directory.
  27. CREATE_MAIL_SPOOL: According to the value creates or does not create the mail spool.
  28.  
  29. Example 1: Creating user with all the default options, and with his own group.
  30.  
  31. Following example creates user ramesh with group ramesh. Use Linux passwd command to change the password for the user immediately after user creation.
  32.  
  33. # useradd ramesh
  34.  
  35. # passwd ramesh
  36. Changing password for user ramesh.
  37. New UNIX password:
  38. Retype new UNIX password:
  39. passwd: all authentication tokens updated successfully.
  40.  
  41. # grep ramesh /etc/passwd
  42. ramesh:x:500:500::/home/ramesh:/bin/bash
  43.  
  44. # grep ramesh /etc/group
  45. ramesh:x:500:
  46. [Note: default useradd command created ramesh as username and group]
  47.  
  48. Example 2: Creating an user with all the default options, and with the default group.
  49.  
  50. # useradd -n sathiya
  51.  
  52. # grep sathiya /etc/passwd
  53. sathiya:x:511:100::/home/sathiya:/bin/bash
  54.  
  55. # grep sathiya /etc/group
  56. [Note: No rows returned, as group sathiya was not created]
  57.  
  58. # grep 100 /etc/group
  59. users:x:100:
  60. [Note: useradd -n command created user sathiya with default group id 100]
  61.  
  62. # passwd sathiya
  63. Changing password for user sathiya.
  64. New UNIX password:
  65. Retype new UNIX password:
  66. passwd: all authentication tokens updated successfully.
  67. [Note: Always set the password immediately after user creation]
  68.  
  69. Example 3: Editing the default options used by useradd.
  70.  
  71. The following example shows how to change the default shell from /bin/bash to /bin/ksh during user creation.
  72.  
  73. Syntax: # useradd -D --shell=<SHELLNAME>
  74.  
  75. # useradd -D
  76. GROUP=100
  77. HOME=/home
  78. INACTIVE=-1
  79. EXPIRE=
  80. SHELL=/bin/bash
  81. SKEL=/etc/skel
  82. [Note: The default shell is /bin/bash]
  83.  
  84. # useradd -D -s /bin/ksh
  85.  
  86. # useradd -D
  87. GROUP=100
  88. HOME=/home
  89. INACTIVE=-1
  90. EXPIRE=
  91. SHELL=/bin/ksh
  92. SKEL=/etc/skel
  93. [Note: Now the default shell changed to /bin/ksh]
  94.  
  95. # adduser priya
  96.  
  97. # grep priya /etc/passwd
  98. priya:x:512:512::/home/priya:/bin/ksh
  99. [Note: New users are getting created with /bin/ksh]
  100.  
  101. # useradd -D -s /bin/bash
  102. [Note: Set it back to /bin/bash, as the above is only for testing purpose]
  103.  
  104. Method 2: Linux useradd Command — Create Users With Custom Configurations
  105.  
  106. Instead of accepting the default values (for example, group, shell etc.) that is given by the useradd command as shown in the above method, you can specify custom values in the command line as parameters to the useradd command.
  107.  
  108. Syntax: # useradd -s <SHELL> -m -d <HomeDir> -g <Group> UserName
  109.  
  110.  
  111.  
  112. -s SHELL : Login shell for the user.
  113. -m : Create user’s home directory if it does not exist.
  114. -d HomeDir : Home directory of the user.
  115. -g Group : Group name or number of the user.
  116. UserName : Login id of the user.
  117.  
  118. Example 4: Create Linux User with Custom Configurations Using useradd Command
  119.  
  120. The following example creates an account (lebron) with home directory /home/king, default shell as /bin/csh and with comment “LeBron James”.
  121.  
  122. # useradd -s /bin/csh -m -d /home/king -c "LeBron James" -g root lebron
  123.  
  124. # grep lebron /etc/passwd
  125. lebron:x:513:0:LeBron James:/home/king:/bin/csh
  126.  
  127.  
  128. Note: You can give the password using -p option, which should be encrypted password. Or you can use the passwd command to change the password of the user.
  129. Method 3: Linux adduser Command – Create Users Interactively
  130.  
  131. These are the friendlier tools to the low level useradd. By default it chooses the Debian policy format for UID and GID. A very simple way of creating user in the command line interactively is using adduser command.
  132.  
  133. Syntax: # adduser USERNAME
  134.  
  135. Example 5: Creating an User Interactively With adduser Command
  136.  
  137. # adduser spidey
  138.  
  139. Adding user `spidey' ...
  140. Adding new group `spidey' (1007) ...
  141. Adding new user `spidey' (1007) with group `spidey' ...
  142. Creating home directory `/home/spidey' ...
  143. Copying files from `/etc/skel' ...
  144. Enter new UNIX password:
  145. Retype new UNIX password:
  146. passwd: password updated successfully
  147. Changing the user information for spidey
  148. Enter the new value, or press ENTER for the default
  149. Full Name []: Peter Parker
  150. Room Number []:
  151. Work Phone []:
  152. Home Phone []:
  153. Other []:
  154. Is the information correct? [y/N] y
  155.  
  156. Method 4: Linux newusers Command — Creating bulk users
  157.  
  158. Sometimes you may want to to create multiple users at the same time. Using any one of the above 3 methods for bulk user creation can be very tedious and time consuming. Fortunately, Linux offers a way to upload users using newusers command. This can also be executed in batch mode as it cannot ask any input.
  159.  
  160. # newusers FILENAME
  161.  
  162.  
  163. This file format is same as the password file.
  164.  
  165. loginname:password:uid:gid:comment:home_dir:shell
  166.  
  167. Example 6: Creating Large Number of Users Using newusers Command
  168.  
  169. If Simpson family decides to join your organization and need access to your Linux server, you can create account for all of them together using newusers command as shown below.
  170.  
  171. # cat homer-family.txt
  172. homer:HcZ600a9:1008:1000:Homer Simpson:/home/homer:/bin/bash
  173. marge:1enz733N:1009:1000:Marge Simpson:/home/marge:/bin/csh
  174. bart:1y5eJr8K:1010:1000:Bart Simpson:/home/bart:/bin/ksh
  175. lisa:VGz638i9:1011:1000:Lisa Simpson:/home/lisa:/bin/sh
  176. maggie:5lj3YGQo:1012:1000:Maggie Simpson:/home/maggie:/bin/bash
  177.  
  178.  
  179. Note: While specifying passwords for users, please follow the password best practices including the 8-4 password rule that we discussed a while back.
  180.  
  181. Now create accounts for Simpsons family together using the newusers command as shown below.
  182.  
  183. # newusers homer-family.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement