{"id":6275,"date":"2020-08-05T00:15:39","date_gmt":"2020-08-04T21:15:39","guid":{"rendered":"https:\/\/kifarunix.com\/?p=6275"},"modified":"2024-03-14T22:46:07","modified_gmt":"2024-03-14T19:46:07","slug":"create-user-account-using-useradd-adduser-commands-in-linux","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/create-user-account-using-useradd-adduser-commands-in-linux\/","title":{"rendered":"Create User Account using useradd\/adduser commands in Linux"},"content":{"rendered":"\n<p>In this tutorial, you will learn how to create user account using useradd\/adduser commands in Linux. User management is one of the most common task in Linux system administration. Creating users, setting up their environments, setting passwords, managing their groups, deleting users etc are all tasks surrounding user management in Linux. Having the ability to manage users in LInux is one of the most paramount basic skill in Linux administration.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"639\" height=\"472\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/08\/useradd-adduser-commands.png\" alt=\"Create User Account using useradd\/adduser commands in Linux\" class=\"wp-image-6623\" title=\"\"><\/figure><\/div>\n\n\n<p>Note that user account management in Linux requires elevated privileges; the use of root account or a standard account with sudo rights.<\/p>\n\n\n\n<p>Below is what we are going to cover in regards to user account creation in Linux;<\/p>\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=\"#creating-user-accounts-using-useradd-adduser-commands-in-linux\">Creating User Accounts using useradd\/adduser commands in Linux<\/a><ul><li><a href=\"#using-useradd-command-in-linux\">Using useradd command in Linux<\/a><\/li><li><a href=\"#using-adduser-command-in-linux\">Using adduser command in Linux<\/a><\/li><li><a href=\"#setting-user-account-password-in-linux\">Setting User Account Password in Linux<\/a><\/li><li><a href=\"#viewing-user-account-information-in-linux\">Viewing User Account Information in Linux<\/a><ul><li><a href=\"#viewing-general-user-account-information\">Viewing General User Account Information<\/a><\/li><li><a href=\"#viewing-user-account-password-information-in-linux\">Viewing User Account Password Information in Linux<\/a><\/li><\/ul><\/li><li><a href=\"#reference\">Reference<\/a><\/li><li><a href=\"#other-related-tutorials\">Other Related Tutorials<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"creating-user-accounts-using-useradd-adduser-commands-in-linux\"><a href=\"#create-user-account-using-useradd-adduser-command\">Creating User Accounts using useradd\/adduser commands in Linux<\/a><\/h2>\n\n\n\n<p>There are various way in which you can create user accounts in Linux. If you are using desktop based system, you can do user account creation from the GUI (or console if you want), while on the headless servers you can do user account creation from the console, here in called the terminal. In this tutorial, we will focus on creating user management in Linux from terminal.<\/p>\n\n\n\n<p>Linux provides various commands for creating user accounts, with the most common ones being <code><strong><a href=\"https:\/\/linux.die.net\/man\/8\/useradd\" target=\"_blank\" aria-label=\"undefined (opens in a new tab)\" rel=\"noreferrer noopener\">useradd<\/a><\/strong><\/code> and <code><strong><a href=\"https:\/\/linux.die.net\/man\/8\/adduser\" target=\"_blank\" aria-label=\"undefined (opens in a new tab)\" rel=\"noreferrer noopener\">adduser<\/a><\/strong><\/code> utilities.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"using-useradd-command-in-linux\"><a href=\"#create-account-with-useradd-command\">Using <code><strong>useradd<\/strong><\/code> command in Linux<\/a><\/h3>\n\n\n\n<p>The command line syntax for <code>useradd<\/code> utility is;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>useradd [-c comment] [-d home-dir] [-e expire-date] [-f inactive-days] [-g default-group] [-G group[,...]] [-m [-k skeleton-dir] | -M] [-p password] [-s shell] [-u UID [-o]] [-r] [-N] username<\/code><\/pre>\n\n\n\n<p>In its simplest form, you would simply run the useradd as shown below to create a user;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>useradd USERNAME<\/code><\/pre>\n\n\n\n<p>For example, to create an account for user johndoe;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>useradd johndoe<\/code><\/pre>\n\n\n\n<p>This creates a user account with the default options defined on the <code>\/etc\/login.defs<\/code> file. You can view the defaults from the <code>passwd<\/code> database. The useradd command default options are also defined in <code><strong>\/etc\/default\/useradd<\/strong><\/code> file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>getent passwd johndoe<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>johndoe:x:1002:1002::\/home\/johndoe:\/bin\/sh<\/code><\/pre>\n\n\n\n<p>By default,  a group will also be created for the new user with the same  group ID (GID) as user ID (UID) and same group name as username;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>getent group johndoe<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>johndoe:x:1002:<\/code><\/pre>\n\n\n\n<p>You can pass multiple options to the useradd utility to customize your user account during creation. For example, see the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>useradd -m -c \"Jane Doe\" -s \/bin\/bash -g level1 -G level1,level2 janedoe<\/code><\/pre>\n\n\n\n<p>Where the options:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><strong>-m<\/strong><\/code>: tells the useradd command to create user&#8217;s home directory (\/home\/janedoe). <\/li>\n\n\n\n<li><code><strong>-c<\/strong><\/code>: defines a short description of the user, and is currently used as the field for the user&#8217;s full name (Jane Doe).<\/li>\n\n\n\n<li><strong><code>-s<\/code><\/strong>: defines a custom user&#8217;s login shell, bash is used above. Check \/etc\/login.defs for the default value, usually \/bin\/sh<\/li>\n\n\n\n<li><strong><code>-g<\/code><\/strong>: defines a custom primary group for user instead of creating a group similar to login name (username). The group must already be existing.<\/li>\n\n\n\n<li><strong><code>-G<\/code><\/strong>: adds a user to additional groups specified. Groups must also be already existing.<\/li>\n<\/ul>\n\n\n\n<p>To list the default options defined on the useradd defaults file, <code><strong>\/etc\/default\/useradd<\/strong><\/code>;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>useradd -D<\/code><\/pre>\n\n\n\n<p>For a complete description of other command line options, refer to <code><strong>man useradd<\/strong><\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"using-adduser-command-in-linux\"><a href=\"#create-account-with-adduser-command\">Using adduser command in Linux<\/a><\/h3>\n\n\n\n<p><code><strong>adduser<\/strong><\/code> command, unlike <code>useradd<\/code> command, helps you to interactively add user account to your linux system. You would simply execute it from your Linux terminal as follows;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>adduser &lt;username&gt;<\/code><\/pre>\n\n\n\n<p>For example, to create a user called janedoe;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>adduser janedoe<\/code><\/pre>\n\n\n\n<p>On Debian based systems, this command will run interactively asking you about various details about the user;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Adding user `janedoe' ...\nAdding new group `janedoe' (1002) ...\nAdding new user `janedoe' (1001) with group `janedoe' ...\nCreating home directory `\/home\/janedoe' ...\nCopying files from `\/etc\/skel' ...\nNew password: \nRetype new password: \npasswd: password updated successfully\nChanging the user information for janedoe\nEnter the new value, or press ENTER for the default\n\tFull Name []: Jane Doe\n\tRoom Number []: \n\tWork Phone []: \n\tHome Phone []: \n\tOther []: \nIs the information correct? [Y\/n] y<\/code><\/pre>\n\n\n\n<p>Both adduser and useradd commands copies the initial user profile\/environment settings defined under the <code><strong>\/etc\/skel<\/strong><\/code> directory to the user login\/home directory.<\/p>\n\n\n\n<p>By default, adduser command uses the default settings defined under <code><strong>\/etc\/adduser.conf<\/strong><\/code> file.<\/p>\n\n\n\n<p>You can specify various options on the command line;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>adduser --home \/home\/janedoe --shell \/bin\/bash --gecos \"\" janedoe<\/code><\/pre>\n\n\n\n<p>On RHEL derivatives, the adduser command is a <strong>symbolic link<\/strong> to useradd command and will just run non-interactively as useradd command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ls -alh \/usr\/sbin\/adduser<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>l<\/strong>rwxrwxrwx. 1 root root 7 Nov  8  2019 <strong>\/usr\/sbin\/adduser -&gt; useradd<\/strong><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"setting-user-account-password-in-linux\"><a href=\"#set-account-password-with-passwd-command\">Setting User Account Password in Linux<\/a><\/h3>\n\n\n\n<p>Before a user account becomes usable, you need to have set a password for it. <code><strong>useradd<\/strong><\/code> command doesn&#8217;t prompt for password. <code>adduser<\/code> command however, prompts you to set the password.<\/p>\n\n\n\n<p>You can set\/reset user account password using <code>passwd<\/code> utility.<\/p>\n\n\n\n<p>As as administrator\/super user (root), you can set user password as simple as executing the command;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>passwd username<\/code><\/pre>\n\n\n\n<p>There are other account details you can control using the passwd utility. Read more on <strong><code>man passwd<\/code><\/strong> about the command line options.<\/p>\n\n\n\n<p>As a standard user, you can only reset your own password. While resetting a password, you need to supply your old password.<\/p>\n\n\n\n<p>With <strong><code>useradd<\/code><\/strong> command, you can specify your password on the command line using the <code><strong>-p ENCRYPTED_PASSWORD<\/strong><\/code> option.<\/p>\n\n\n\n<p>You can generate an encrypted password using openssl or other tools such as crypt.<\/p>\n\n\n\n<p>See example command below to use <code>openssl<\/code> with <code>passwd<\/code> command to generate encrypted password.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>openssl passwd password<\/code><\/pre>\n\n\n\n<p>This will generate an hash for <code>password<\/code> password.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>QqjgPLfXQD8Zk<\/code><\/pre>\n\n\n\n<p>You can then pass this to -p option as the hash;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>useradd -m -p QqjgPLfXQD8Zk username<\/code><\/pre>\n\n\n\n<p>You can simply achieve this using one command;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>useradd -m -p $(openssl passwd password) username<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"viewing-user-account-information-in-linux\"><a href=\"#view-user-account-information\">Viewing User Account Information in Linux<\/a><\/h3>\n\n\n\n<p>There are two files that stores user information records in Linux;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>\/etc\/passwd<\/code><\/strong>: Stores general user information such as username, user ID, group ID, location of home directory, login shell, the Geckos information. The file can be read by standard users.<\/li>\n\n\n\n<li><strong><code>\/etc\/shadow<\/code><\/strong>: Stores user password information such as expiry date, the password hash&#8230;The file cannot be read by standard users.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"viewing-general-user-account-information\"><a href=\"#view-general-user-account-information-linux\">Viewing General User Account Information<\/a><\/h4>\n\n\n\n<p>To view the general user information from the <code><strong>passwd<\/strong><\/code> database, use the <code><strong>getent<\/strong><\/code> tool.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>getent passwd<\/code><\/pre>\n\n\n\n<p>This will list all the users and their account information. If you want to view specific user account information, you can grep the user or simply pass the username as the argument.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>getent passwd | grep janedoe<\/code><\/pre>\n\n\n\n<p>or<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>getent passwd janedoe<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>janedoe:x:1002:1002::\/home\/janedoe:\/bin\/bash<\/code><\/pre>\n\n\n\n<p>Where:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Field No.<\/strong><\/td><td><strong>Field<\/strong><\/td><td><strong>Description<\/strong><\/td><\/tr><tr><td>1<\/td><td>janedoe<\/td><td>Account username<\/td><\/tr><tr><td>2<\/td><td>x<\/td><td><code><strong>letter x<\/strong><\/code> is a placeholder for user&#8217;s encrypted password which is stored in shadow file.<\/td><\/tr><tr><td>3<\/td><td>1002<\/td><td>User ID<\/td><\/tr><tr><td>4<\/td><td>1002<\/td><td>Group ID<\/td><\/tr><tr><td>5<\/td><td>(blank)<\/td><td>This field should contain general user information (GECOS) such as real name (Jane Doe), phone number, location<\/td><\/tr><tr><td>6<\/td><td>\/home\/janedoe<\/td><td>This is the home directory for the user<\/td><\/tr><tr><td>7<\/td><td>\/bin\/bash<\/td><td>This is the shell assigned to the user<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"viewing-user-account-password-information-in-linux\"><a href=\"#view-user-password-information-in-linux\">Viewing User Account Password Information in Linux<\/a><\/h4>\n\n\n\n<p>To view user&#8217;s password information, you can similarly read from the <code><strong>shadow<\/strong><\/code> database using the getent command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>getent shadow janedow<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>janedoe:!!:18478:0:99999:7:::<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Field No<\/strong><\/td><td><strong>Field Value<\/strong><\/td><td><strong>Description<\/strong><\/td><\/tr><tr><td>1<\/td><td>janedoe<\/td><td>Account Username<\/td><\/tr><tr><td>2<\/td><td><strong>!!<\/strong>\/<strong>!<\/strong>\/<strong>*<\/strong>\/<strong>blank<\/strong>\/<br><strong>password hash<\/strong><\/td><td><code>two exclamation marks (<strong>!!<\/strong>)<\/code>: No password set for the account<br><code>one exclamation marks (<strong>!<\/strong>)<\/code>: shows that the account is locked<br><code>an asterisk (<strong>*<\/strong>)<\/code>: Indicates that the account doesn&#8217;t accept login.<br><strong><code>blank<\/code><\/strong>: The password has been deleted for the account and thus, you can login without being prompted for the password.<br><strong><code>hash<\/code><\/strong>: An encrypted password has been set for the account<\/td><\/tr><tr><td>3<\/td><td>18478<\/td><td>Last password change date. Expressed in number of days since 01\/01\/1970. Value of -1 means the feature is disabled.<\/td><\/tr><tr><td>4<\/td><td>0<\/td><td>Number of days before password can be changes. 0 shows that password can be changed at any time.<\/td><\/tr><tr><td>5<\/td><td>99999<\/td><td>How long in days after which the password must be changed. 99999 indicates that the account doesn&#8217;t expire and hence the password can be kept for as many years.<\/td><\/tr><tr><td>6<\/td><td>7<\/td><td>Number of warning days before a password expires. This show 7 days<\/td><\/tr><tr><td>7<\/td><td><\/td><td>Number of days an account can stay after it has expired before it is disabled\/deactivated completely.<\/td><\/tr><tr><td>8<\/td><td><\/td><td>How long in days, since 01\/01\/1970 since the account is disabled<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>And that is how easy it is to create and view user account information in Linux.<\/p>\n\n\n\n<p>The commands that we used in this tutorial, supports other wide number of command line options. Be sure to check the man pages of each individual command to get a comprehensive description of these options.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"reference\">Reference<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>man useradd<\/li>\n\n\n\n<li>man adduser<\/li>\n\n\n\n<li>man passwd<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"other-related-tutorials\">Other Related Tutorials<\/h3>\n\n\n\n<p><a aria-label=\"undefined (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/using-find-command-to-search-for-files-and-directories-in-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\">Using Find Command to Search for Files and Directories in Linux<\/a><\/p>\n\n\n\n<p><a aria-label=\"undefined (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/delete-lines-matching-a-specific-pattern-in-a-file-using-sed\/\" target=\"_blank\" rel=\"noreferrer noopener\">Delete Lines Matching a Specific Pattern in a File using SED<\/a><\/p>\n\n\n\n<p><a aria-label=\"undefined (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/delete-lines-matching-specific-pattern-in-a-file-using-vim\/\" target=\"_blank\" rel=\"noreferrer noopener\">Delete Lines Matching Specific Pattern in a File using VIM<\/a><\/p>\n\n\n\n<p><a aria-label=\"undefined (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/how-to-check-hardware-information-block-devices-and-filesystems-on-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to check Hardware information, block devices and filesystems on a Linux System<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/viewing-system-processes-using-ps-and-top-commands\/\" target=\"_blank\" aria-label=\"undefined (opens in a new tab)\" rel=\"noreferrer noopener\">Viewing System Processes using ps and top commands<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to create user account using useradd\/adduser commands in Linux. User management is one of the most common task<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,126,49,127],"tags":[1865,1864,1862,1863,1868,1107,1870,1867,1869,1866],"class_list":["post-6275","post","type-post","status-publish","format-standard","hentry","category-howtos","category-certifications","category-command-cheatsheets","category-lpic-1","tag-adduser","tag-adduser-vs-useradd-command","tag-create-user-accounts-with-adduser-command-in-linux","tag-create-user-accounts-with-useradd-command-in-linux","tag-getent-passwd","tag-linux","tag-linux-user-accounts","tag-passwd","tag-shadow","tag-useradd","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/6275"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=6275"}],"version-history":[{"count":13,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/6275\/revisions"}],"predecessor-version":[{"id":21503,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/6275\/revisions\/21503"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=6275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=6275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=6275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}