{"id":13262,"date":"2019-04-01T10:05:55","date_gmt":"2019-04-01T10:05:55","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13262"},"modified":"2019-04-01T10:05:55","modified_gmt":"2019-04-01T10:05:55","slug":"the-complete-guide-to-useradd-command-in-linux-15-practical-examples","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/the-complete-guide-to-useradd-command-in-linux-15-practical-examples\/","title":{"rendered":"The Complete Guide to \u201cuseradd\u201d Command in Linux \u2013 15 Practical Examples"},"content":{"rendered":"<p>We all are aware about the most popular command called \u2018<strong>useradd<\/strong>\u2018 or \u2018<strong>adduser<\/strong>\u2018 in Linux. There are times when a Linux System Administrator asked to create user accounts on Linux \u00a0with some specific properties, limitations or comments.<\/p>\n<p>In Linux, a \u2018<strong>useradd<\/strong>\u2018 command is a low-level utility that is used for adding\/creating user accounts in\u00a0<strong>Linux<\/strong>\u00a0and other\u00a0<strong>Unix-like<\/strong>\u00a0operating systems. The \u2018<strong>adduser<\/strong>\u2018 is much similar to\u00a0<strong>useradd<\/strong>\u00a0command, because it is just a symbolic link to it.<\/p>\n<div id=\"attachment_5903\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/03\/useradd-command.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5903\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/03\/useradd-command.png\" alt=\"add users in linux\" width=\"442\" height=\"317\" aria-describedby=\"caption-attachment-5903\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-5903\" class=\"wp-caption-text\">useradd command examples<\/p>\n<\/div>\n<p>In some other Linux distributions,\u00a0<strong>useradd<\/strong>\u00a0command may comes with lightly difference version. I suggest you to read your documentation, before using our instructions to create new user accounts in\u00a0<strong>Linux<\/strong>.<\/p>\n<p>When we run \u2018<strong>useradd<\/strong>\u2018 command in Linux terminal, it performs following major things:<\/p>\n<ol>\n<li>It edits \/etc\/passwd, \/etc\/shadow, \/etc\/group and \/etc\/gshadow files for the newly created User account.<\/li>\n<li>Creates and populate a home directory for the new user.<\/li>\n<li>Sets permissions and ownerships to home directory.<\/li>\n<\/ol>\n<p>Basic syntax of command is:<\/p>\n<pre>useradd [options] username<\/pre>\n<p>In this article we will show you the most used\u00a0<strong>15 useradd commands<\/strong>\u00a0with their practical examples in\u00a0<strong>Linux<\/strong>. We have divided the section into two parts from\u00a0<strong>Basic<\/strong>\u00a0to\u00a0<strong>Advance<\/strong>\u00a0usage of command.<\/p>\n<ol>\n<li><strong>Part I<\/strong>: Basic usage with\u00a0<strong>10<\/strong>\u00a0examples<\/li>\n<li><strong>Part II<\/strong>: Advance usage with\u00a0<strong>5<\/strong>\u00a0examples<\/li>\n<\/ol>\n<div align=\"center\"><strong>Part I \u2013\u00a010 Basic Usage of useradd Commands<\/strong><\/div>\n<h3>1. How to Add a New User in Linux<\/h3>\n<p>To add\/create a new user, all you\u2019ve to follow the command \u2018<strong>useradd<\/strong>\u2018 or \u2018<strong>adduser<\/strong>\u2018 with \u2018username\u2019. The \u2018username\u2019 is a user login name, that is used by user to login into the system.<\/p>\n<p>Only one user can be added and that username must be unique (different from other username already exists on the system).<\/p>\n<p>For example, to add a new user called \u2018<strong>tecmint<\/strong>\u2018, use the following command.<\/p>\n<pre>[root@tecmint ~]# useradd tecmint<\/pre>\n<p>When we add a new user in Linux with \u2018<strong>useradd<\/strong>\u2018 command it gets created in locked state and to unlock that user account, we need to set a password for that account with \u2018<strong>passwd<\/strong>\u2018 command.<\/p>\n<pre>[root@tecmint ~]# passwd tecmint\r\nChanging password for user tecmint.\r\nNew UNIX password:\r\nRetype new UNIX password:\r\npasswd: all authentication tokens updated successfully.<\/pre>\n<p>Once a new user created, it\u2019s entry automatically added to the \u2018<strong>\/etc\/passwd<\/strong>\u2018 file. The file is used to store users information and the entry should be.<\/p>\n<pre>tecmint:x:504:504:tecmint:\/home\/tecmint:\/bin\/bash<\/pre>\n<p>The above entry contains a set of seven colon-separated fields, each field has it\u2019s own meaning. Let\u2019s see what are these fields:<\/p>\n<ol>\n<li><strong>Username<\/strong>: User login name used to login into system. It should be between 1 to 32 charcters long.<\/li>\n<li><strong>Password<\/strong>: User password (or x character) stored in \/etc\/shadow file in encrypted format.<\/li>\n<li><strong>User ID (UID)<\/strong>: Every user must have a User ID (UID) User Identification Number. By default UID 0 is reserved for root user and UID\u2019s ranging from 1-99 are reserved for other predefined accounts. Further UID\u2019s ranging from 100-999 are reserved for system accounts and groups.<\/li>\n<li><strong>Group ID (GID)<\/strong>: The primary Group ID (GID) Group Identification Number stored in \/etc\/group file.<\/li>\n<li><strong>User Info<\/strong>: This field is optional and allow you to define extra information about the user. For example, user full name. This field is filled by \u2018finger\u2019 command.<\/li>\n<li><strong>Home Directory<\/strong>: The absolute location of user\u2019s home directory.<\/li>\n<li><strong>Shell<\/strong>: The absolute location of a user\u2019s shell i.e. \/bin\/bash.<\/li>\n<\/ol>\n<h3>2. Create a User with Different Home Directory<\/h3>\n<p>By default \u2018<strong>useradd<\/strong>\u2018 command creates a user\u2019s home directory under<strong>\u00a0\/home<\/strong>\u00a0directory with username. Thus, for example, we\u2019ve seen above the default home directory for the user \u2018<strong>tecmint<\/strong>\u2018 is \u2018<strong>\/home\/tecmint<\/strong>\u2018.<\/p>\n<p>However, this action can be changed by using \u2018<strong>-d<\/strong>\u2018 option along with the location of new home directory (i.e.<strong>\/data\/projects<\/strong>). For example, the following command will create a user \u2018<strong>anusha<\/strong>\u2018 with a home directory \u2018<strong>\/data\/projects<\/strong>\u2018.<\/p>\n<pre>[root@tecmint ~]# useradd -d \/data\/projects anusha<\/pre>\n<p>You can see the user home directory and other user related information like user id, group id, shell and comments.<\/p>\n<pre>[root@tecmint ~]# cat \/etc\/passwd | grep anusha\r\n\r\n<strong>anusha<\/strong>:x:505:505::<strong>\/data\/projects<\/strong>:\/bin\/bash<\/pre>\n<h3>3. Create a User with Specific User ID<\/h3>\n<p>In Linux, every user has its own\u00a0<strong>UID<\/strong>\u00a0(<strong>Unique Identification Number<\/strong>). By default, whenever we create a new user accounts in\u00a0<strong>Linux<\/strong>, it assigns userid\u00a0<strong>500<\/strong>,\u00a0<strong>501<\/strong>,\u00a0<strong>502<\/strong>\u00a0and so on\u2026<\/p>\n<p>But, we can create user\u2019s with custom userid with \u2018<strong>-u<\/strong>\u2018 option. For example, the following command will create a user \u2018<strong>navin<\/strong>\u2018 with custom userid \u2018<strong>999<\/strong>\u2018.<\/p>\n<pre>[root@tecmint ~]# useradd -u 999 navin<\/pre>\n<p>Now, let\u2019s verify that the user created with a defined userid (<strong>999<\/strong>) using following command.<\/p>\n<pre>[root@tecmint ~]# cat \/etc\/passwd | grep tecmint\r\n\r\nnavin:x:<strong>999<\/strong>:999::\/home\/tecmint:\/bin\/bash<\/pre>\n<p><strong>NOTE<\/strong>: Make sure the value of a user ID must be unique from any other already created users on the system.<\/p>\n<h3>4. Create a User with Specific Group ID<\/h3>\n<p>Similarly, every user has its own\u00a0<strong>GID<\/strong>\u00a0(<strong>Group Identification Number<\/strong>). We can create users with specific group ID\u2019s as well with\u00a0<strong>-g<\/strong>\u00a0option.<\/p>\n<p>Here in this example, we will add a user \u2018<strong>tarunika<\/strong>\u2018 with a specific\u00a0<strong>UID<\/strong>\u00a0and\u00a0<strong>GID<\/strong>\u00a0simultaneously with the help of \u2018<strong>-u<\/strong>\u2018 and \u2018<strong>-g<\/strong>\u2018 options.<\/p>\n<pre>[root@tecmint ~]# useradd -u 1000 -g 500 tarunika<\/pre>\n<p>Now, see the assigned user id and group id in \u2018<strong>\/etc\/passwd<\/strong>\u2018 file.<\/p>\n<pre>[root@tecmint ~]# cat \/etc\/passwd | grep tarunika\r\n\r\n<strong>tarunika<\/strong>:x:<strong>1000<\/strong>:<strong>500<\/strong>::\/home\/tarunika:\/bin\/bash<\/pre>\n<h3>5. Add a User to Multiple Groups<\/h3>\n<p>The \u2018<strong>-G<\/strong>\u2018 option is used to add a user to additional groups. Each group name is separated by a comma, with no intervening spaces.<\/p>\n<p>Here in this example, we are adding a user \u2018<strong>tecmint<\/strong>\u2018 into multiple groups like\u00a0<strong>admins<\/strong>,\u00a0<strong>webadmin<\/strong>\u00a0and\u00a0<strong>developer<\/strong>.<\/p>\n<pre>[root@tecmint ~]# useradd -G admins,webadmin,developers tecmint<\/pre>\n<p>Next, verify that the multiple groups assigned to the user with id command.<\/p>\n<pre>[root@tecmint ~]# id tecmint\r\n\r\nuid=1001(tecmint) gid=1001(tecmint)\r\ngroups=1001(<strong>tecmint<\/strong>),500(<strong>admins<\/strong>),501(<strong>webadmin<\/strong>),502(<strong>developers<\/strong>)\r\ncontext=root:system_r:unconfined_t:SystemLow-SystemHigh<\/pre>\n<h3>6. Add a User without Home Directory<\/h3>\n<p>In some situations, where we don\u2019t want to assign a home directories for a user\u2019s, due to some security reasons. In such situation, when a user logs into a system that has just restarted, its home directory will be root. When such user uses\u00a0<a title=\"su command\" href=\"https:\/\/www.tecmint.com\/su-vs-sudo-and-how-to-configure-sudo-in-linux\/\" target=\"_blank\" rel=\"noopener noreferrer\">su command<\/a>, its login directory will be the previous user home directory.<\/p>\n<p>To create user\u2019s without their home directories, \u2018<strong>-M<\/strong>\u2018 is used. For example, the following command will create a user \u2018<strong>shilpi<\/strong>\u2018 without a home directory.<\/p>\n<pre>[root@tecmint ~]# useradd -M shilpi<\/pre>\n<p>Now, let\u2019s verify that the user is created without home directory, using ls command.<\/p>\n<pre>[root@tecmint ~]# ls -l \/home\/shilpi\r\n\r\nls: cannot access <strong>\/home\/shilpi<\/strong>: No such file or directory<\/pre>\n<h3>7. Create a User with Account Expiry Date<\/h3>\n<p>By default, when we add user\u2019s with \u2018<strong>useradd<\/strong>\u2018 command user account never get expires i.e their expiry date is set to\u00a0<strong>0<\/strong>\u00a0(means never expired).<\/p>\n<p>However, we can set the expiry date using \u2018<strong>-e<\/strong>\u2018 option, that sets date in\u00a0<strong>YYYY-MM-DD<\/strong>\u00a0format. This is helpful for creating temporary accounts for a specific period of time.<\/p>\n<p>Here in this example, we create a user \u2018<strong>aparna<\/strong>\u2018 with account expiry date i.e.\u00a0<strong>27th April 2014<\/strong>\u00a0in\u00a0<strong>YYYY-MM-DD<\/strong>format.<\/p>\n<pre>[root@tecmint ~]# useradd -e 2014-03-27 aparna<\/pre>\n<p>Next, verify the age of account and password with \u2018<strong>chage<\/strong>\u2018 command for user \u2018<strong>aparna<\/strong>\u2018 after setting account expiry date.<\/p>\n<pre>[root@tecmint ~]# chage -l aparna\r\n\r\nLast password change\t\t\t\t\t\t: Mar 28, 2014\r\nPassword expires\t\t\t\t\t\t: never\r\nPassword inactive\t\t\t\t\t\t: never\r\nAccount expires\t\t\t\t\t\t\t: Mar 27, 2014\r\nMinimum number of days between password change\t\t        : 0\r\nMaximum number of days between password change\t\t        : 99999\r\nNumber of days of warning before password expires\t\t: 7<\/pre>\n<h3>8. Create a User with Password Expiry Date<\/h3>\n<p>The \u2018<strong>-f<\/strong>\u2018 argument is used to define the number of days after a password expires. A value of\u00a0<strong>0<\/strong>\u00a0inactive the user account as soon as the password has expired. By default, the password expiry value set to\u00a0<strong>-1<\/strong>\u00a0means never expire.<\/p>\n<p>Here in this example, we will set a account password expiry date i.e.\u00a0<strong>45 days<\/strong>\u00a0on a user \u2018tecmint\u2019 using \u2018<strong>-e<\/strong>\u2018 and \u2018<strong>-f<\/strong>\u2018 options.<\/p>\n<pre>[root@tecmint ~]# useradd -e 2014-04-27 -f 45 tecmint<\/pre>\n<h3>9. Add a User with Custom Comments<\/h3>\n<p>The \u2018<strong>-c<\/strong>\u2018 option allows you to add custom comments, such as user\u2019s\u00a0<strong>full name<\/strong>,\u00a0<strong>phone number<\/strong>, etc to\u00a0<strong>\/etc\/passwd<\/strong>\u00a0file. The comment can be added as a single line without any spaces.<\/p>\n<p>For example, the following command will add a user \u2018<strong>mansi<\/strong>\u2018 and would insert that user\u2019s full name,\u00a0<strong>Manis Khurana<\/strong>, into the comment field.<\/p>\n<pre>[root@tecmint ~]# useradd -c \"Manis Khurana\" mansi<\/pre>\n<p>You can see your comments in \u2018<strong>\/etc\/passwd<\/strong>\u2018 file in comments section.<\/p>\n<pre>[root@tecmint ~]# tail -1 \/etc\/passwd\r\n\r\n<strong>mansi<\/strong>:x:1006:1008:<strong>Manis Khurana<\/strong>:\/home\/mansi:\/bin\/sh<\/pre>\n<h3>10. Change User Login Shell:<\/h3>\n<p>Sometimes, we add users which has nothing to do with login shell or sometimes we require to assign different shells to our users. We can assign different login shells to a each user with \u2018<strong>-s<\/strong>\u2018 option.<\/p>\n<p>Here in this example, will add a user \u2018<strong>tecmint<\/strong>\u2018 without login shell i.e. \u2018<strong>\/sbin\/nologin<\/strong>\u2018 shell.<\/p>\n<pre>[root@tecmint ~]# useradd -s \/sbin\/nologin tecmint<\/pre>\n<p>You can check assigned shell to the user in \u2018<strong>\/etc\/passwd<\/strong>\u2018 file.<\/p>\n<pre>[root@tecmint ~]# tail -1 \/etc\/passwd\r\n\r\n<strong>tecmint<\/strong>:x:1002:1002::\/home\/tecmint:<strong>\/sbin\/nologin\r\n<\/strong><\/pre>\n<div align=\"center\"><strong>Part II \u2013\u00a05 Advance Usage of useradd Commands<\/strong><\/div>\n<h3>11. Add a User with Specific Home Directory, Default Shell and Custom Comment<\/h3>\n<p>The following command will create a user \u2018<strong>ravi<\/strong>\u2018 with home directory \u2018<strong>\/var\/www\/tecmint<\/strong>\u2018, default shell\u00a0<strong>\/bin\/bash<\/strong>and adds extra information about user.<\/p>\n<pre>[root@tecmint ~]# useradd -m -d \/var\/www\/ravi -s \/bin\/bash -c \"TecMint Owner\" -U ravi<\/pre>\n<p>In the above command \u2018<strong>-m -d<\/strong>\u2018 option creates a user with specified home directory and the \u2018<strong>-s<\/strong>\u2018 option set the user\u2019s default shell i.e.\u00a0<strong>\/bin\/bash<\/strong>. The \u2018<strong>-c<\/strong>\u2018 option adds the extra information about user and \u2018<strong>-U<\/strong>\u2018 argument create\/adds a group with the same name as the user.<\/p>\n<h3>12. Add a User with Home Directory, Custom Shell, Custom Comment and UID\/GID<\/h3>\n<p>The command is very similar to above, but here we defining shell as \u2018<strong>\/bin\/zsh<\/strong>\u2018 and custom\u00a0<strong>UID<\/strong>\u00a0and\u00a0<strong>GID<\/strong>\u00a0to a user \u2018<strong>tarunika<\/strong>\u2018. Where \u2018<strong>-u<\/strong>\u2018 defines new user\u2019s\u00a0<strong>UID<\/strong>\u00a0(i.e.\u00a0<strong>1000<\/strong>) and whereas \u2018<strong>-g<\/strong>\u2018 defines\u00a0<strong>GID<\/strong>\u00a0(i.e.\u00a0<strong>1000<\/strong>).<\/p>\n<pre>[root@tecmint ~]# useradd -m -d \/var\/www\/tarunika -s \/bin\/zsh -c \"TecMint Technical Writer\" -u 1000 -g 1000 tarunika<\/pre>\n<h3>13. Add a User with Home Directory, No Shell, Custom Comment and User ID<\/h3>\n<p>The following command is very much similar to above two commands, the only difference is here, that we disabling login shell to a user called \u2018<strong>avishek<\/strong>\u2018 with custom\u00a0<strong>User ID<\/strong>\u00a0(i.e.\u00a0<strong>1019<\/strong>).<\/p>\n<p>Here \u2018<strong>-s<\/strong>\u2018 option adds the default shell\u00a0<strong>\/bin\/bash<\/strong>, but in this case we set login to \u2018<strong>\/usr\/sbin\/nologin<\/strong>\u2018. That means user \u2018<strong>avishek<\/strong>\u2018 will not able to login into the system.<\/p>\n<pre>[root@tecmint ~]# useradd -m -d \/var\/www\/avishek -s \/usr\/sbin\/nologin -c \"TecMint Sr. Technical Writer\" -u 1019 avishek<\/pre>\n<h3>14. Add a User with Home Directory, Shell, Custom Skell\/Comment and User ID<\/h3>\n<p>The only change in this command is, we used \u2018<strong>-k<\/strong>\u2018 option to set custom skeleton directory i.e.\u00a0<strong>\/etc\/custom.skell<\/strong>, not the default one\u00a0<strong>\/etc\/skel<\/strong>. We also used \u2018<strong>-s<\/strong>\u2018 option to define different shell i.e.\u00a0<strong>\/bin\/tcsh<\/strong>\u00a0to user \u2018<strong>navin<\/strong>\u2018.<\/p>\n<pre>[root@tecmint ~]# useradd -m -d \/var\/www\/navin -k \/etc\/custom.skell -s \/bin\/tcsh -c \"No Active Member of TecMint\" -u 1027 navin<\/pre>\n<h3>15. Add a User without Home Directory, No Shell, No Group and Custom Comment<\/h3>\n<p>This following command is very different than the other commands explained above. Here we used \u2018<strong>-M<\/strong>\u2018 option to create user without user\u2019s home directory and \u2018<strong>-N<\/strong>\u2018 argument is used that tells the system to only create username (without group). The \u2018<strong>-r<\/strong>\u2018 arguments is for creating a system user.<\/p>\n<pre>[root@tecmint ~]# useradd -M -N -r -s \/bin\/false -c \"Disabled TecMint Member\" clayton<\/pre>\n<p>For more information and options about useradd, run \u2018<strong>useradd<\/strong>\u2018 command on the terminal to see available options.<\/p>\n<p><b>Read Also<\/b>:\u00a0<a href=\"https:\/\/www.tecmint.com\/usermod-command-examples\/\" target=\"_blank\" rel=\"noopener noreferrer\">15 usermod Command Examples<\/a><\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/add-users-in-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We all are aware about the most popular command called \u2018useradd\u2018 or \u2018adduser\u2018 in Linux. There are times when a Linux System Administrator asked to create user accounts on Linux \u00a0with some specific properties, limitations or comments. In Linux, a \u2018useradd\u2018 command is a low-level utility that is used for adding\/creating user accounts in\u00a0Linux\u00a0and other\u00a0Unix-like\u00a0operating &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/the-complete-guide-to-useradd-command-in-linux-15-practical-examples\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;The Complete Guide to \u201cuseradd\u201d Command in Linux \u2013 15 Practical Examples&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13262","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13262"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13262\/revisions"}],"predecessor-version":[{"id":13263,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13262\/revisions\/13263"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}