{"id":1988,"date":"2015-06-13T09:45:31","date_gmt":"2015-06-13T01:45:31","guid":{"rendered":"https:\/\/webhostinggeeks.com\/howto\/?p=1988"},"modified":"2023-06-27T09:29:19","modified_gmt":"2023-06-27T09:29:19","slug":"how-to-add-remove-user-account-rhel-centos-oracle-linux","status":"publish","type":"post","link":"https:\/\/webhostinggeeks.com\/howto\/how-to-add-remove-user-account-rhel-centos-oracle-linux\/","title":{"rendered":"How to a Add and Remove User Account on RHEL, CentOS, Oracle Linux"},"content":{"rendered":"<p>In Linux server administration, one of the fundamental tasks that you will need to perform is the management of users and groups. It&#8217;s crucial to know how to add and remove users, set user permissions, and configure user settings.<\/p>\n<p>This tutorial will guide you through the process of adding and removing user accounts on RHEL, CentOS, and Oracle Linux servers.<\/p>\n<p>We will be using the useradd and userdel commands, which are essential tools in the arsenal of every server administrator. These commands allow you to create and delete user accounts from the command line, providing you with a quick and efficient way to manage users on your server.<\/p>\n<h2>Adding a New User to a Linux System<\/h2>\n<p>To understand the useradd command better, you can access its manual by typing the following command in the terminal:<\/p>\n<pre>\r\nman useradd\r\n<\/pre>\n<p>To create a new account and a home directory for the user, use the following command:<\/p>\n<pre>\r\nuseradd --home \/home\/username username\r\n<\/pre>\n<p>Replace &#8220;username&#8221; with the username of the new account you want to create.<\/p>\n<p>For example:<\/p>\n<pre>\r\n# useradd --home \/home\/geeks geeks\r\n<\/pre>\n<p>By default, the useradd command does not set any valid password, and the user cannot log in until a password is set. To set the password, use the following command:<\/p>\n<pre>\r\npasswd username\r\n<\/pre>\n<p>You will be prompted to enter and retype the new password.<\/p>\n<pre>\r\n# passwd geeks\r\nChanging password for user geeks.\r\nNew password:\r\nRetype new password:\r\npasswd: all authentication tokens updated successfully.\r\n<\/pre>\n<p>To verify the values in \/etc\/passwd, use the following command:<\/p>\n<pre>\r\ncat \/etc\/passwd | grep username\r\n<\/pre>\n<p>For example:<\/p>\n<pre>\r\n# cat \/etc\/passwd | grep geeks\r\ngeeks:x:501:501::\/home\/geeks:\/bin\/bash\r\n<\/pre>\n<p>To verify the values in \/etc\/group, use the following command:<\/p>\n<pre>\r\ncat \/etc\/group | grep username\r\n<\/pre>\n<pre>\r\n# cat \/etc\/group | grep geeks\r\ngeeks:x:501:\r\n<\/pre>\n<p>To verify if an email user has been created for the new user, use the following command:<\/p>\n<pre>\r\nls \/var\/spool\/mail | grep username\r\n<\/pre>\n<p>For example:<\/p>\n<pre>\r\n# ls \/var\/spool\/mail | grep ehowstuff\r\nehowstuff\r\n<\/pre>\n<p>The useradd command has several options that you can use to customize the new user account. Here are some of them:<\/p>\n<pre>\r\nNAME\r\n       userdel - delete a user account and related files\r\n\r\nSYNOPSIS\r\n       userdel [options] LOGIN\r\n\r\nDESCRIPTION\r\n       userdel is a low level utility for removing users. On Debian, administrators should usually use\r\n       deluser(8) instead.\r\n\r\n       The userdel command modifies the system account files, deleting all entries that refer to the user\r\n       name LOGIN. The named user must exist.\r\n\r\nOPTIONS\r\n       The options which apply to the userdel command are:\r\n\r\n       -f, --force\r\n           This option forces the removal of the user account, even if the user is still logged in. It also\r\n           forces userdel to remove the user's home directory and mail spool, even if another user uses the\r\n           same home directory or if the mail spool is not owned by the specified user. If USERGROUPS_ENAB\r\n           is defined to yes in \/etc\/login.defs and if a group exists with the same name as the deleted\r\n           user, then this group will be removed, even if it is still the primary group of another user.\r\n\r\n           Note: This option is dangerous and may leave your system in an inconsistent state.\r\n\r\n       -h, --help\r\n           Display help message and exit.\r\n\r\n       -r, --remove\r\n           Files in the user's home directory will be removed along with the home directory itself and the\r\n           user's mail spool. Files located in other file systems will have to be searched for and deleted\r\n           manually.\r\n\r\n           The mail spool is defined by the MAIL_DIR variable in the login.defs file.\r\n\r\n       -R, --root CHROOT_DIR\r\n           Apply changes in the CHROOT_DIR directory and use the configuration files from the CHROOT_DIR\r\n           directory.\r\n\r\n       -Z, --selinux-user\r\n           Remove any SELinux user mapping for the user's login.\r\n<\/pre>\n<p>Each of these options has a specific function, which you can learn more about by referring to the useradd manual.<\/p>\n<h2>Deleting a User from a Linux System<\/h2>\n<p>To understand the userdel command better, you can access its manual by typing the following command in the terminal:<\/p>\n<pre>\r\nman userdel\r\n<\/pre>\n<p>The userdel command removes the user from \/etc\/passwd, but leaves the home directory intact by default. To remove the user\u2019s account, user\u2019s home directory, and mail spool as part of the deletion process, use the following command:<\/p>\n<pre>\r\nuserdel --remove username\r\n<\/pre>\n<p>or<\/p>\n<pre>\r\nuserdel -r username\r\n<\/pre>\n<p>Please be aware that when a user is removed with userdel without the -r option specified, the system will have files that are owned by an unassigned user ID number. This can also happen when files created by a deleted user exist outside their home directory. This situation can lead to information leakage and other security issues.<\/p>\n<h2>Few Real-Life Examples<\/h2>\n<h3>Example 1: Creating a New User Account<\/h3>\n<p>Let&#8217;s say you want to create a new user account for a new employee named John. You can use the useradd command to create the account and the passwd command to set a password:<\/p>\n<pre>\r\nuseradd --home \/home\/john john\r\npasswd john\r\n<\/pre>\n<h3>Example 2: Adding a User to a Group<\/h3>\n<p>Suppose you have a group named &#8220;developers&#8221; and you want to add John to this group. You can use the usermod command to add John to the group:<\/p>\n<pre>\r\nusermod -aG developers john\r\n<\/pre>\n<h3>Example 3: Changing a User&#8217;s Login Shell<\/h3>\n<p>If John prefers to use the Zsh shell instead of the default Bash shell, you can change his login shell using the usermod command:<\/p>\n<pre>\r\nusermod -s \/bin\/zsh john\r\n<\/pre>\n<h3>Example 4: Locking a User Account<\/h3>\n<p>If John is going on vacation and you want to temporarily disable his account, you can lock it using the usermod command:<\/p>\n<pre>\r\nusermod -L john\r\n<\/pre>\n<h3>Example 5: Unlocking a User Account<\/h3>\n<p>When John returns from vacation, you can unlock his account using the usermod command:<\/p>\n<pre>\r\nusermod -U john\r\n<\/pre>\n<h3>Example 6: Changing a User&#8217;s Home Directory<\/h3>\n<p>If you want to move John&#8217;s home directory to a different location, you can use the usermod command:<\/p>\n<pre>\r\nusermod -d \/new\/path\/to\/home john\r\n<\/pre>\n<h3>Example 7: Deleting a User Account<\/h3>\n<p>If John leaves the company and you want to remove his account, you can use the userdel command:<\/p>\n<pre>\r\nuserdel --remove john\r\n<\/pre>\n<h3>Example 8: Checking a User&#8217;s Mail Spool<\/h3>\n<p>If you want to check if there are any mails for John, you can use the ls command:<\/p>\n<pre>\r\nls \/var\/spool\/mail | grep john\r\n<\/pre>\n<h2>Commands Mentioned<\/h2>\n<ul>\n<li><span class=\"fw-bold\">man useradd<\/span> \u2013 Displays the manual for the `useradd` command<\/li>\n<li><span class=\"fw-bold\">useradd &#8211;home \/home\/username username<\/span> \u2013 Creates a new user account and a home directory<\/li>\n<li><span class=\"fw-bold\">passwd username<\/span> \u2013 Sets a password for the user<\/li>\n<li><span class=\"fw-bold\">cat \/etc\/passwd | grep username<\/span> \u2013 Verifies the values in `\/etc\/passwd`<\/li>\n<li><span class=\"fw-bold\">cat \/etc\/group | grep username<\/span> \u2013 Verifies the values in `\/etc\/group`<\/li>\n<li><span class=\"fw-bold\">ls \/var\/spool\/mail | grep username<\/span> \u2013 Verifies if an email user has been created<\/li>\n<li><span class=\"fw-bold\">man userdel<\/span> \u2013 Displays the manual for the `userdel` command<\/li>\n<li><span class=\"fw-bold\">userdel &#8211;remove username<\/span> \u2013 Removes the user\u2019s account, user\u2019s home directory, and mail spool<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>The useradd and userdel commands are powerful tools that allow administrators to manage user accounts efficiently.<\/p>\n<p>With useradd, you can create a new user account, specify its home directory, set its login shell, and assign it to a user group. For instance, if you&#8217;re setting up a new <a href=\"https:\/\/webhostinggeeks.com\/blog\/apache-http-server-explained\/\">Apache<\/a> web server on a <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-dedicated-server-hosting\/\">dedicated server account<\/a>, you might create a new user account for each member of your web development team, giving them the access they need to contribute to the project.<\/p>\n<p>On the other hand, userdel allows you to remove a user account from the system. This is particularly useful when a team member leaves the project or when an account is no longer needed. For example, if you&#8217;re migrating from a <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-vps-hosting\/\">VPS<\/a> to a <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-cloud-hosting\/\">cloud<\/a> hosting environment, you might need to remove user accounts associated with the old server.<\/p>\n<p>However, it&#8217;s important to use userdel responsibly. If you remove a user without the -r option, the user&#8217;s home directory and mail spool are left intact. This can lead to files being owned by an unassigned user ID number, potentially causing information leakage and other security issues. Therefore, it&#8217;s recommended to always use the -r option when removing a user account to ensure that all associated files are also deleted.<\/p>\n<p>In conclusion, managing user accounts is a crucial aspect of maintaining a secure and efficient server environment. Whether you&#8217;re a seasoned administrator or a beginner, understanding how to add and remove users in Linux is a skill that will undoubtedly prove valuable in your work.<\/p>\n<p>Always remember to manage user accounts responsibly, remove accounts that are no longer needed, and regularly update passwords to maintain a secure environment.<\/p>\n<h2>FAQ<\/h2>\n<ol itemscope itemtype=\"https:\/\/schema.org\/FAQPage\">\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What is the purpose of the `useradd` command in Linux?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">The `useradd` command in Linux is used to create a new user account on the system. It allows administrators to specify various settings for the new user, such as the home directory, login shell, and user group.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">How can I set a password for a new user in Linux?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">You can set a password for a new user in Linux using the `passwd` command followed by the username. You will be prompted to enter and retype the new password.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What does the `userdel` command do in Linux?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">The `userdel` command in Linux is used to delete a user account from the system. It removes the user from the `\/etc\/passwd` file. If used with the `&#8211;remove` or `-r` option, it also removes the user&#8217;s home directory and mail spool.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What happens if a user is removed without the `-r` option?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">If a user is removed with the `userdel` command without the `-r` option, the user&#8217;s home directory and mail spool are left intact. The system will have files that are owned by an unassigned user ID number, which can lead to information leakage and other security issues.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">How can I verify if a new user has been created in Linux?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">You can verify if a new user has been created in Linux by checking the `\/etc\/passwd` and `\/etc\/group` files. You can use the `grep` command with the username to search for the user in these files. Additionally, you can check if an email user has been created for the new user by using the `ls \/var\/spool\/mail | grep username` command.<\/span>\n            <\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>In Linux server administration, one of the fundamental tasks that you will need to perform is the management of users and groups. It&#8217;s crucial to know how to add and&#8230;<\/p>\n","protected":false},"author":6,"featured_media":6280,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wds_primary_category":0,"footnotes":""},"categories":[2055,1020,2058],"tags":[1244,1536,2095,1546,2094,1713,2059,2092,2093],"class_list":["post-1988","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-oracle-linux-7","category-rhel","tag-centos","tag-linux","tag-linux-commands","tag-linux-utilities","tag-oracle","tag-rhel","tag-user","tag-useradd","tag-userdel"],"_links":{"self":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/1988","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/comments?post=1988"}],"version-history":[{"count":0,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/1988\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media\/6280"}],"wp:attachment":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media?parent=1988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/categories?post=1988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/tags?post=1988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}