{"id":93,"date":"2023-12-26T12:04:41","date_gmt":"2023-12-26T12:04:41","guid":{"rendered":"https:\/\/chennaihosting.co.in\/kb\/?p=93"},"modified":"2024-01-24T01:18:31","modified_gmt":"2024-01-24T01:18:31","slug":"create-user-using-useradd-command-in-linux","status":"publish","type":"post","link":"https:\/\/chennaihosting.co.in\/kb\/create-user-using-useradd-command-in-linux\/","title":{"rendered":"5 useradd command to create users in linux"},"content":{"rendered":"\n<p>In&nbsp;Linux, the \u2018<strong>useradd<\/strong>\u2018 command is a low-level utility used for adding or creating user accounts in Linux and other Unix-like operating systems. The \u2018<strong>adduser<\/strong>\u2018 command is very similar to the \u2018<strong>useradd<\/strong>\u2018 command, as it is just a <a href=\"https:\/\/chennaihosting.co.in\/\" data-type=\"link\" data-id=\"https:\/\/chennaihosting.co.in\/\">symbolic <\/a>link to it.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"300\" src=\"https:\/\/chennaihosting.co.in\/kb\/wp-content\/uploads\/2023\/12\/useradd-command-featured-image.png\" alt=\"\" class=\"wp-image-113\" srcset=\"https:\/\/chennaihosting.co.in\/kb\/wp-content\/uploads\/2023\/12\/useradd-command-featured-image.png 700w, https:\/\/chennaihosting.co.in\/kb\/wp-content\/uploads\/2023\/12\/useradd-command-featured-image-300x129.png 300w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/figure>\n\n\n\n<p>In&nbsp;some Linux distributions, the \u2018<strong>useradd<\/strong>\u2018 command may have a slightly different version. I suggest reading your documentation before using our instructions to create new user accounts in Linux.<\/p>\n\n\n\n<p>When we run the \u2018<strong>useradd<\/strong>\u2018 command in the Linux terminal, it performs the following major tasks:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It edits&nbsp;<strong>\/etc\/passwd<\/strong>,&nbsp;<strong>\/etc\/shadow<\/strong>,&nbsp;<strong>\/etc\/group,<\/strong>&nbsp;and&nbsp;<strong>\/etc\/gshadow<\/strong>&nbsp;files for the newly created user accounts.<\/li>\n\n\n\n<li>Creates and populates a home directory for the new user.<\/li>\n\n\n\n<li>Sets permissions and ownerships to the home directory.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#useradd-command-syntax\">Useradd Command Syntax<\/a><\/li><li><a href=\"#1-how-to-add-a-new-user-in-linux\">1. How to Add a New User in Linux<\/a><\/li><li><a href=\"#2-how-to-create-a-user-with-a-different-home-directory\">2. How to Create a User with a Different Home Directory<\/a><\/li><li><a href=\"#3-how-to-create-a-user-with-a-specific-user-id\">3. How to Create a User with a Specific User ID<\/a><\/li><li><a href=\"#4-how-to-create-a-user-with-a-specific-group-id\">4. How to Create a User with a Specific Group ID<\/a><\/li><li><a href=\"#5-how-to-add-a-user-to-multiple-groups\">5. How to Add a User to Multiple Groups<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"useradd-command-syntax\">Useradd Command Syntax<\/h2>\n\n\n\n<p>The basic syntax of the \u2018<strong>useradd<\/strong>\u2018 command is:<\/p>\n\n\n\n<p>The basic syntax of the \u2018<strong>useradd<\/strong>\u2018 command is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>useradd &#91;options] username<\/code><\/pre>\n\n\n\n<p>In this article, we will demonstrate the 15 most commonly used \u2018<strong>useradd<\/strong>\u2018 commands with practical examples in Linux.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-how-to-add-a-new-user-in-linux\">1. How to Add a New User in Linux<\/h2>\n\n\n\n<p>To add or create a new user, you have to use the \u2018<strong>useradd<\/strong>\u2018 or \u2018<strong>adduser<\/strong>\u2018 command followed by the \u2018<strong>username<\/strong>\u2018. The \u2018<strong>username<\/strong>\u2018 is the login name a user uses to log into the system.<\/p>\n\n\n\n<p>Only one user can be added, and the username must be unique, and not already exist on the system.<\/p>\n\n\n\n<p>For example, to add a new user named \u2018<strong>chennaihosting<\/strong>\u2018 use the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>useradd chennaihosting<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"17\" src=\"https:\/\/chennaihosting.co.in\/kb\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-26-192730-1024x17.png\" alt=\"\" class=\"wp-image-100\" srcset=\"https:\/\/chennaihosting.co.in\/kb\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-26-192730-1024x17.png 1024w, https:\/\/chennaihosting.co.in\/kb\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-26-192730-300x5.png 300w, https:\/\/chennaihosting.co.in\/kb\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-26-192730-768x13.png 768w, https:\/\/chennaihosting.co.in\/kb\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-26-192730-1280x23.png 1280w, https:\/\/chennaihosting.co.in\/kb\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-26-192730.png 1410w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>When we add a new user in Linux with the \u2018<strong>useradd<\/strong>\u2018 command, it gets created in a locked state. To unlock that user account, we need to set a password for that account using the \u2018<strong>passwd<\/strong>\u2018 command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>passwd chennaihosting<\/code><\/pre>\n\n\n\n<p>Once a new user is created, its entry is automatically added to the \u2018<strong>\/etc\/passwd<\/strong>\u2018 file. This file is used to store the user\u2019s information, and the entry should be.<\/p>\n\n\n\n<p>chennaihosting:x:1000:1000:chennaihosting:\/home\/chennaihosting:\/bin\/bash<\/p>\n\n\n\n<p>The above entry contains a set of seven colon-separated fields, each field having its own meaning.<\/p>\n\n\n\n<p>Let\u2019s see what these fields are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Username<\/strong>&nbsp;\u2013 The user login name is used to log into the system. It should be between 1 and 32 characters long.<\/li>\n\n\n\n<li><strong>Password<\/strong>&nbsp;\u2013 The user password (or&nbsp;<code>'x'<\/code>&nbsp;character) is stored in the \u2018<strong>\/etc\/shadow<\/strong>\u2018 file in an encrypted format.<\/li>\n\n\n\n<li><strong>User ID (UID)<\/strong>&nbsp;\u2013 Every user must have a User ID (<strong>UID<\/strong>), which stands for&nbsp;<strong>User Identification Number<\/strong>. By default,&nbsp;<strong>UID 0<\/strong>&nbsp;is reserved for the root user, and UIDs ranging from 1 to 99 are reserved for other predefined accounts. Additionally, UIDs ranging from 100 to 999 are reserved for system accounts and groups.<\/li>\n\n\n\n<li><strong>Group ID (GID)<\/strong>&nbsp;\u2013 The primary Group ID (<strong>GID<\/strong>), which stands for&nbsp;<strong>Group Identification Number<\/strong>, is stored in the \u2018<strong>\/etc\/group<\/strong>\u2018 file.<\/li>\n\n\n\n<li><strong>User Info<\/strong>&nbsp;\u2013 This field is optional and allows you to define extra information about the user, such as the user\u2019s full name. This information can be filled in using the&nbsp;finger command.<\/li>\n\n\n\n<li><strong>Home Directory<\/strong>&nbsp;\u2013 The absolute location of the user\u2019s home directory.<\/li>\n\n\n\n<li><strong>Shell<\/strong>&nbsp;\u2013 The absolute location of a user\u2019s shell i.e.&nbsp;<strong>\/bin\/bash<\/strong>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-how-to-create-a-user-with-a-different-home-directory\">2. How to Create a User with a Different Home Directory<\/h2>\n\n\n\n<p>By default, the \u2018<strong>useradd<\/strong>\u2018 command creates a user\u2019s home directory under the \u2018<strong>\/home<\/strong>\u2018 directory with the username. For example, as seen above, the default home directory for the user \u2018<strong><a href=\"https:\/\/www.squarebrothers.com\/\" data-type=\"link\" data-id=\"https:\/\/chennaihosting.co.in\/\" target=\"_blank\" rel=\"noopener\">chennaihosting<\/a><\/strong>\u2018 is \u2018<strong>\/home\/chennaihosting<\/strong>\u2018.<\/p>\n\n\n\n<p>However, this behavior can be changed by using the&nbsp;<code>'-d'<\/code>&nbsp;option along with the location of the new home directory (e.g., \u2018<strong>\/data\/projects<\/strong>\u2018). For instance, the following command will create a user \u2018<strong>shruti<\/strong>\u2018 with a home directory set to \u2018<strong>\/data\/projects<\/strong>\u2018.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># useradd -d \/data\/projects <strong>shruti<\/strong>\n# passwd <strong>Shruti<\/strong><\/code><\/pre>\n\n\n\n<p>You can view the user\u2019s home directory and&nbsp;other user-related information, such as user ID, group ID, shell, and comments using the following&nbsp;cat command.cat \/etc\/passwd | grep anusha<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/etc\/passwd | grep <strong>shruti<\/strong>\n\n<strong>shruti<\/strong>:x:1001:1001::\/data\/projects:\/bin\/bash<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-how-to-create-a-user-with-a-specific-user-id\">3. How to Create a User with a Specific User ID<\/h2>\n\n\n\n<p>In Linux, every user has their own UID (Unique Identification Number). By default, when we create a new user account in Linux, it assigns user IDs&nbsp;<strong>500<\/strong>,&nbsp;<strong>501<\/strong>,&nbsp;<strong>502<\/strong>, and so on.<\/p>\n\n\n\n<p>However, we can create users with custom user IDs using the&nbsp;<code>'-u'<\/code>&nbsp;option. For example, the following command will create a user \u2018<strong>helen<\/strong>\u2018 with a custom user ID \u2018<strong>1002<\/strong>\u2018.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>useradd -u 1002 helen<\/code><\/pre>\n\n\n\n<p>Now, let\u2019s verify that the user created with a defined userid (<strong>1002<\/strong>) using the following command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/etc\/passwd | grep helen\n\nhelen:x:1002:1002::\/home\/helen:\/bin\/bash<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-how-to-create-a-user-with-a-specific-group-id\">4. How to Create a User with a Specific Group ID<\/h2>\n\n\n\n<p>Similarly, every user has their own&nbsp;<strong>GID<\/strong>&nbsp;(<strong>Group Identifier<\/strong>). We can create users with specific group IDs as well using the&nbsp;<code>'-g'<\/code>&nbsp;option.<\/p>\n\n\n\n<p>In this example, we will add a user \u2018<strong>jane<\/strong>\u2018 with a specific&nbsp;<strong>UID<\/strong>&nbsp;and&nbsp;<strong>GID<\/strong>&nbsp;simultaneously with the help of the&nbsp;<code>'-u'<\/code>&nbsp;and&nbsp;<code>'-g'<\/code>&nbsp;options.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>useradd -u 1005 -g chennaihosting jane<\/code><\/pre>\n\n\n\n<p>Now, check the assigned user ID and group ID in the \u2018<strong>\/etc\/passwd<\/strong>\u2018 file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/etc\/passwd | grep jane\n\njane:x:1005:1000::\/home\/jane:\/bin\/bash<\/code><\/pre>\n\n\n\n<p>To verify the user\u2019s GID, use the id command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>id -gn jane<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-how-to-add-a-user-to-multiple-groups\">5. How to Add a User to Multiple Groups<\/h2>\n\n\n\n<p>The&nbsp;<code>'-G'<\/code>&nbsp;option is used to add a user to additional groups. Each group name is separated by a comma, with no intervening spaces.<\/p>\n\n\n\n<p>In this example, we are adding a user \u2018<strong>chennaihosting<\/strong>\u2018 to multiple groups, such as&nbsp;<strong>admins<\/strong>,&nbsp;<strong>webadmin<\/strong>, and&nbsp;<strong>developers<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>groupadd admins\ngroupadd webadmin\ngroupadd developers\nusermod -a -G admins,webadmin,developers chennaihosting\nuseradd -G admins,webadmin,developers paddy<\/code><\/pre>\n\n\n\n<p>Next, verify that the multiple groups are assigned to the user with the&nbsp;<strong>id<\/strong>&nbsp;command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>id chennaihosting\n\nuid=1000(chennaihosting) gid=1000(chennaihosting)\ngroups=1000(chennaihosting),1007(admins),1008(webadmin),1009(developers)\ncontext=root:system_r:unconfined_t:SystemLow-SystemHigh<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>We hope you&#8217;ve found this useful!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In&nbsp;Linux, the \u2018useradd\u2018 command is a low-level utility used for [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":104,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[7,8],"class_list":["post-93","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","tag-add-user","tag-create-user"],"_links":{"self":[{"href":"https:\/\/chennaihosting.co.in\/kb\/wp-json\/wp\/v2\/posts\/93","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chennaihosting.co.in\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chennaihosting.co.in\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chennaihosting.co.in\/kb\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/chennaihosting.co.in\/kb\/wp-json\/wp\/v2\/comments?post=93"}],"version-history":[{"count":13,"href":"https:\/\/chennaihosting.co.in\/kb\/wp-json\/wp\/v2\/posts\/93\/revisions"}],"predecessor-version":[{"id":191,"href":"https:\/\/chennaihosting.co.in\/kb\/wp-json\/wp\/v2\/posts\/93\/revisions\/191"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/chennaihosting.co.in\/kb\/wp-json\/wp\/v2\/media\/104"}],"wp:attachment":[{"href":"https:\/\/chennaihosting.co.in\/kb\/wp-json\/wp\/v2\/media?parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chennaihosting.co.in\/kb\/wp-json\/wp\/v2\/categories?post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chennaihosting.co.in\/kb\/wp-json\/wp\/v2\/tags?post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}