

{"id":110954,"date":"2022-10-14T10:00:13","date_gmt":"2022-10-14T04:30:13","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=110954"},"modified":"2022-10-14T10:24:17","modified_gmt":"2022-10-14T04:54:17","slug":"userdel-command-in-linux","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/userdel-command-in-linux\/","title":{"rendered":"Userdel Command in Linux"},"content":{"rendered":"<p>In this article, you will learn all there is to the userdel command in Linux-based operating systems. We will go through what userdel is, why it is used, and the syntax and options used with the userdel command. In the end, we will also look at some beautiful examples of the userdel command in the terminal of Ubuntu 20.04. So pay attention, grab a snack if you want to, and read right till the end!<\/p>\n<h3>What is Linux userdel command?<\/h3>\n<p>Userdel is the abbreviation for \u201cUser Delete\u201d, It is a command-line-based utility in Linux-based operating systems that helps in removing or deleting users from the system. Userdel basically modifies the system account files, deleting all the entries which refer to the username LOGIN.<\/p>\n<p>We all know that Linux is a multi-user architecture, meaning more than one person can interact with the same system at the same time. It is the system administrator&#8217;s job to wisely manage these users, tasks like creating users, removing users, creating groups, removing groups, and assigning users to different groups come under user management. One such user management command is useradd.<\/p>\n<p>When you are in the need of deleting a user may be the user moved away from the organization, or it was created for a specific service that no longer runs on the system, you can use the userdel command. It is mostly advised for system administrators to use the deluser command which is slightly more user-friendly.<\/p>\n<h3>Syntax of userdel command in Linux<\/h3>\n<p>The syntax of the userdel command is pretty easy:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">usedel &lt;options&gt; &lt;username&gt;<\/pre>\n<p>Let us take a closer look at the fields present in the syntax of the userdel command:<\/p>\n<h4>1. &lt;options&gt;<\/h4>\n<p>This field takes in a range of options that specify how the userdel command should work and print the output. We will look at all of these options in the next section.<\/p>\n<h4>2. &lt;username&gt;<\/h4>\n<p>This field takes in the name of the user\u2019s account on the computer of which you want to delete.<\/p>\n<h3>Options used with the userdel command<\/h3>\n<p>In typical Linux fashion, userdel also has a long list of options that we can use to perform various tasks, let us look at some of them.<\/p>\n<h4>1. -f<\/h4>\n<p>This option forces the removal of the user\u2019s account. It removes the account, even if the user is still active and logged in! You can also write this option as \u201c&#8211;force\u201d<\/p>\n<h4>2. -r<\/h4>\n<p>This option removes the files in the user&#8217;s home directory, along with the home directory itself and the user&#8217;s mail spool. You can also write this option as \u201c&#8211;remove\u201d<\/p>\n<h4>3. -R &lt;chroot_dir&gt;<\/h4>\n<p>This option applies the changes in the chroot directory you specified and uses the configuration files from the same directory. You can also write this option as \u201c&#8211;root\u201d<\/p>\n<h4>4. -Z<\/h4>\n<p>This option removes any SELinux (Security-Enhanced Linux) user mapping for the user&#8217;s login. You can also write this option as \u201c&#8211;selinux-user\u201d<\/p>\n<h4>5. -P<\/h4>\n<p>This option prefixes the directories which are located in the \/etc\/* files. You can also write this option as \u201c&#8211;prefix\u201d<\/p>\n<h4>6. -h<\/h4>\n<p>This option prints out the help menu as shown below. It contains information about the usage and information about the command and options of the userdel command. You can also write this option as \u201c&#8211;help\u201d<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/help-menu-of-the-userdel-command.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-111036\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/help-menu-of-the-userdel-command.webp\" alt=\"help menu of the userdel command\" width=\"780\" height=\"262\" \/><\/a><\/p>\n<h3>Configuration variables of the userdel command<\/h3>\n<p>The userdel has a configuration file \/etc\/login.defs. In this file there are a couple of configuration files that change the behavior of the command, let us look into each of the variables and what it does.<\/p>\n<h4>1. MAIL_DIR<\/h4>\n<p>This string-type configuration variable manipulates the mailbox when its corresponding user account is modified or deleted.<\/p>\n<h4>2. MAIL_FILE<\/h4>\n<p>This string-type configuration variable defines the location of the user&#8217;s mail spool files relatively to their home directory.<\/p>\n<h4>3. MAX_MEMBERS_PER_GROUP<\/h4>\n<p>This number-type configuration variable specifies the maximum number of members per group entry. If and when the maximum number is reached, a new group entry is started in \/etc\/group<\/p>\n<h4>4. USERDEL_CMD<\/h4>\n<p>This string-type configuration variable runs when removing a user when it is defined.<\/p>\n<h4>5. USERGROUPS_ENAB<\/h4>\n<p>If this boolean-type configuration variable is set to \u201cyes\u201d, the userdel command removes the user&#8217;s group if it contains no more members, and useradd creates by default a group with the name of the user.<\/p>\n<h3>Files used by the userdel command<\/h3>\n<p>The userdel command takes in some data from a specific file in the system, let us look at what these files are and what exactly does the userdel command read from them.<\/p>\n<h4>1. \/etc\/group<\/h4>\n<p>The userdel command takes group account information from this file.<\/p>\n<h4>2. \/etc\/login.defs<\/h4>\n<p>The userdel command takes the shadow password suite configuration from this file.<\/p>\n<h4>3. \/etc\/passwd<\/h4>\n<p>The userdel command takes user account information from this file.<\/p>\n<h4>4. \/etc\/shadow<\/h4>\n<p>The userdel command takes secure user account information (like the password) from this file.<\/p>\n<h3>Exit status<\/h3>\n<p>Upon executing a userdel command, it will exit with one of the following exit codes, let us look at what they are and what they mean.<\/p>\n<p>0 &#8211; Success<br \/>\n1 &#8211; Failed to update the passwd file.<br \/>\n2 &#8211; Invalid syntax<br \/>\n6 &#8211; The user you specified does not exist<br \/>\n8 &#8211; Could not delete the user as the user you specified is currently logged in.<br \/>\n10 &#8211; Failed to update group file<br \/>\n12 &#8211; Could not remove the home directory.<\/p>\n<p>Now that we have covered the fundamentals of the userdel command, let us look at some practical examples of it to understand how the options and the command itself work.<\/p>\n<h3>Force removing a user<\/h3>\n<p>If you want to force remove a user, meaning remove a user even if he or she is logged in and active, you can do so by either using the option \u201c-f\u201d or \u201c&#8211;force\u201d as shown below<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo userdel -f &lt;username&gt;<\/pre>\n<h3><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/force-removing-a-user.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-111037\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/force-removing-a-user.webp\" alt=\"force removing a user\" width=\"728\" height=\"145\" \/><\/a><\/h3>\n<h3>Removing the home directory when removing the user account<\/h3>\n<p>We can remove the home directory of a user and the files present in it at the same time as removing the account of the user by using the option \u201c-r\u201d. However, you will have to search and manually delete all the other files located in other file systems. Use the syntax shown below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo userdel -r &lt;username&gt;<\/pre>\n<h3><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/removing-the-home-directory-when-removing-the-user-account.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-111039\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/removing-the-home-directory-when-removing-the-user-account.webp\" alt=\"removing the home directory when removing the user account\" width=\"726\" height=\"166\" \/><\/a><\/h3>\n<h3>Changing chroot configuration directory<\/h3>\n<p>You can change the configuration file of chroot by using the option \u201c-R\u201d a shown:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo userdel -R &lt;username&gt;<\/pre>\n<h3><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/changing-chroot-configuration-directory.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-111040\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/changing-chroot-configuration-directory.webp\" alt=\"changing chroot configuration directory\" width=\"752\" height=\"126\" \/><\/a><\/h3>\n<h3>Removing SE Linux mapping<\/h3>\n<p>If you want to remove Security Linux for the user&#8217;s login, simply use the option \u201c-Z\u201d as shown<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">sudo userdel -Z &lt;username&gt;<\/pre>\n<h3><\/h3>\n<h3>Summary<\/h3>\n<p>As you have seen, the userdel command is a simple command that lets you remove users from your system. You have now learned what userdel is, why it is used, the syntax, options, configuration variables, exit codes, and the file used by the userdel command. We have also looked at a couple of examples to get a basic idea of how the userdel command works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, you will learn all there is to the userdel command in Linux-based operating systems. We will go through what userdel is, why it is used, and the syntax and options used&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":110955,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[27115,27116],"class_list":["post-110954","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","tag-linux-userdel","tag-userdel-command-in-linux"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Userdel Command in Linux - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn about userdel command in linux. See syntax, options and files used by the userdel command with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/userdel-command-in-linux\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Userdel Command in Linux - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn about userdel command in linux. See syntax, options and files used by the userdel command with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/userdel-command-in-linux\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-14T04:30:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-10-14T04:54:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/linux-userdel-command.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Userdel Command in Linux - DataFlair","description":"Learn about userdel command in linux. See syntax, options and files used by the userdel command with examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/userdel-command-in-linux\/","og_locale":"en_US","og_type":"article","og_title":"Userdel Command in Linux - DataFlair","og_description":"Learn about userdel command in linux. See syntax, options and files used by the userdel command with examples.","og_url":"https:\/\/data-flair.training\/blogs\/userdel-command-in-linux\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2022-10-14T04:30:13+00:00","article_modified_time":"2022-10-14T04:54:17+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/linux-userdel-command.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/userdel-command-in-linux\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/userdel-command-in-linux\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Userdel Command in Linux","datePublished":"2022-10-14T04:30:13+00:00","dateModified":"2022-10-14T04:54:17+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/userdel-command-in-linux\/"},"wordCount":1128,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/userdel-command-in-linux\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/linux-userdel-command.webp","keywords":["linux userdel","userdel command in linux"],"articleSection":["Linux Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/userdel-command-in-linux\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/userdel-command-in-linux\/","url":"https:\/\/data-flair.training\/blogs\/userdel-command-in-linux\/","name":"Userdel Command in Linux - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/userdel-command-in-linux\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/userdel-command-in-linux\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/linux-userdel-command.webp","datePublished":"2022-10-14T04:30:13+00:00","dateModified":"2022-10-14T04:54:17+00:00","description":"Learn about userdel command in linux. See syntax, options and files used by the userdel command with examples.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/userdel-command-in-linux\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/userdel-command-in-linux\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/userdel-command-in-linux\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/linux-userdel-command.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/linux-userdel-command.webp","width":1200,"height":628,"caption":"linux userdel command"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/userdel-command-in-linux\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Linux Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/linux\/"},{"@type":"ListItem","position":3,"name":"Userdel Command in Linux"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/110954","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=110954"}],"version-history":[{"count":7,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/110954\/revisions"}],"predecessor-version":[{"id":111043,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/110954\/revisions\/111043"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/110955"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=110954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=110954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=110954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}