{"id":6846,"date":"2019-11-30T13:02:10","date_gmt":"2019-11-30T13:02:10","guid":{"rendered":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/?p=6846"},"modified":"2019-11-30T13:02:10","modified_gmt":"2019-11-30T13:02:10","slug":"how-to-list-and-filter-users-in-linux","status":"publish","type":"post","link":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/how-to-list-and-filter-users-in-linux\/","title":{"rendered":"How to List and Filter Users in Linux"},"content":{"rendered":"<p style=\"text-align: justify;\">In this KB article, you will learn how to list and filter users in the Linux system and what are the main differences between system and normal Linux users. The same commands can be run on any Linux distro, including Ubuntu, CentOS, Linux Mint, Debian and RHEL.<\/p>\n<h2>Get a List of All Users using the \/etc\/passwd File<\/h2>\n<p style=\"text-align: justify;\">Local user information is saved in the <em><strong>\/etc\/passwd<\/strong><\/em> file. Every line describes login information for one user in this file. You can either use <em><strong>cat<\/strong> <\/em>or <em><strong>less<\/strong> <\/em>to open the file:<\/p>\n<div class=\"vlt-box\">\n<div class=\"box-content\">less \/etc\/passwd<\/div>\n<\/div>\n<div class=\"vlt-box\">\n<div class=\"box-title\">Sample outputs<\/div>\n<div class=\"box-content\">\n<p>root:x:0:0:root:\/root:\/bin\/bash<br \/>\ndaemon:x:1:1:daemon:\/usr\/sbin:\/bin\/sh<br \/>\nbin:x:2:2:bin:\/bin:\/bin\/sh<br \/>\nsys:x:3:3:sys:\/dev:\/bin\/sh<br \/>\nsync:x:4:65534:sync:\/bin:\/bin\/sync<br \/>\ngames:x:5:60:games:\/usr\/games:\/bin\/sh<br \/>\nman:x:6:12:man:\/var\/cache\/man:\/bin\/sh<br \/>\n&#8230;.<br \/>\n..<br \/>\n&#8230;<br \/>\nvihan:x:11:11:Vihan Rege,,,:\/home\/cache\/vihan:\/bin\/sh<\/p>\n<\/div>\n<\/div>\n<p style=\"text-align: justify;\">As you can see from the output above, total seven fields exists and all fields are delimited by colon (:) symbol that have the following information.<\/p>\n<ul>\n<li>User name<\/li>\n<li>x means that the Encrypted password is stored in the \/etc\/shadow file<\/li>\n<li>User ID number (UID)<\/li>\n<li>User\u2019s group ID number (GID)<\/li>\n<li>Full name of the user (GECOS)<\/li>\n<li>User home directory<\/li>\n<li>Login shell (defaults to \/bin\/bash)<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">If you want to display only the username, you can use either <em><strong>awk<\/strong><\/em> or <em><strong>cut<\/strong><\/em> commands to print only the first field containing the username:<\/p>\n<p>Type the <em><strong>awk<\/strong> <\/em>or <em><strong>cut<\/strong> <\/em>commands to list only usernames:<\/p>\n<div class=\"vlt-box\">\n<div class=\"box-content\">\n<p>awk -F: &#8216;{ print $1}&#8217; \/etc\/passwd<\/p>\n<p>cut -d: -f1 \/etc\/passwd<\/p>\n<\/div>\n<\/div>\n<div class=\"vlt-box\">\n<div class=\"box-title\">Output<\/div>\n<div class=\"box-content\">\n<p>root<br \/>\ndaemon<br \/>\nbin<br \/>\nsys<br \/>\nsync<br \/>\n&#8230;<br \/>\n&#8230;<br \/>\nvihan<\/p>\n<\/div>\n<\/div>\n<h2>Get a List of all Users using the getent Command<\/h2>\n<p style=\"text-align: justify;\">The <em><strong>getent<\/strong><\/em> command shows entries from databases configured in \/etc\/nsswitch.conf file as well as the <em><strong>passwd<\/strong><\/em> database which we can use to request a list of all users.<\/p>\n<p>Type the following <em><strong>getent<\/strong><\/em> command to get a list of all Linux users:<\/p>\n<div class=\"vlt-box\">\n<div class=\"box-content\">\n<p>getent passwd<\/p>\n<\/div>\n<\/div>\n<p style=\"text-align: justify;\">You will see the output is the same as when showing the content of the <em><strong>\/etc\/passwd<\/strong><\/em> file. The <em><strong>getent<\/strong><\/em> will show all Linux users from both <em><strong>\/etc\/passwd<\/strong><\/em> file and LDAP database, if you are using LDAP for user authentication.<\/p>\n<p>You can also use <em><strong>awk<\/strong><\/em> or <em><strong>cut<\/strong> <\/em>to print only the first field containing the username:<\/p>\n<div class=\"vlt-box\">\n<div class=\"box-content\">\n<p>getent passwd | awk -F: &#8216;{ print $1}&#8217;<\/p>\n<p>getent passwd | cut -d: -f1<\/p>\n<\/div>\n<\/div>\n<h2>Check if a user exists in the Linux system<\/h2>\n<p style=\"text-align: justify;\">Now we learn how to list all users. We can easily filter the users\u2019 list by piping the list to the grep command to verify, if the user exists in our Linux box.<\/p>\n<p style=\"text-align: justify;\">As an example, we can use the following command to find out, if the user with the name <em><strong>Vihan<\/strong><\/em> exists in our Linux system.<\/p>\n<div class=\"vlt-box\">\n<div class=\"box-content\">\n<p>getent passwd | grep vihan<\/p>\n<\/div>\n<\/div>\n<div class=\"vlt-box\">\n<div class=\"box-title\">Output<\/div>\n<div class=\"box-content\">\n<p>vihan:x:11:11:Vihan Rege,,,:\/home\/vihan:\/bin\/bash<\/p>\n<\/div>\n<\/div>\n<p style=\"text-align: justify;\">The command above displays the user\u2019s login information, if the user exists. If no output is displayed that means the user doesn\u2019t exist.<\/p>\n<p>We can also check user existence without using the <em><strong>grep<\/strong><\/em> command as follows:<\/p>\n<div class=\"vlt-box\">\n<div class=\"box-content\">\n<p>getent passwd jack<\/p>\n<\/div>\n<\/div>\n<p>Like before, the command will show the user\u2019s login information, if the user exists.<\/p>\n<p>You can run the following command to find out the numbers of user accounts you have on your system:<\/p>\n<div class=\"vlt-box\">\n<div class=\"box-content\">\n<p>getent passwd | wc -l<\/p>\n<\/div>\n<\/div>\n<div class=\"vlt-box\">\n<div class=\"box-title\">Output<\/div>\n<div class=\"box-content\">\n<p>22<\/p>\n<\/div>\n<\/div>\n<p style=\"text-align: justify;\">You can see the numbers of user accounts from the output as above in the Linux system (my Linux system has 22).<\/p>\n<h2>System and Normal Users<\/h2>\n<p style=\"text-align: justify;\">The system and normal (regular) users are technically the same. Normally, system users are created while installing the operating system and new packages. In some circumstances, you can create a system user that will be used by a few applications.<\/p>\n<p style=\"text-align: justify;\">Normal users are created through root or another user with sudo privileges. Usually, a normal user has a genuine login shell and a home directory.<\/p>\n<p style=\"text-align: justify;\">Each user has a numerical user ID named UID. If not defined while creating a new user with the <em><strong>useradd<\/strong><\/em> command, the UID will be automatically picked from the <em><strong>\/etc\/login.defs<\/strong><\/em> file depending on the <em><strong>UID_MIN<\/strong><\/em> and <em><strong>UID_MAX<\/strong> <\/em>values.<\/p>\n<p style=\"text-align: justify;\">You can run the following command to check the <em><strong>UID_MIN<\/strong> <\/em>and <em><strong>UID_MIX<\/strong> <\/em>values on your Linux system:<\/p>\n<div class=\"vlt-box\">\n<div class=\"box-content\">\n<p>grep -E &#8216;^UID_MIN|^UID_MAX&#8217; \/etc\/login.defs<\/p>\n<\/div>\n<\/div>\n<div class=\"vlt-box\">\n<div class=\"box-title\">Output<\/div>\n<div class=\"box-content\">\n<p>UID_MIN\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1000<br \/>\nUID_MAX \u00a0 \u00a0 \u00a0\u00a0 20000<\/p>\n<\/div>\n<\/div>\n<p>We can see in the above output that all normal users should have a UID between 1000 and 20000.<\/p>\n<p>The following command will list all normal users in our Linux system:<\/p>\n<div class=\"vlt-box\">\n<div class=\"box-content\">\n<p>getent passwd {1000..20000}<\/p>\n<\/div>\n<\/div>\n<div class=\"vlt-box\">\n<div class=\"box-title\">Output<\/div>\n<div class=\"box-content\">\n<p>james:x:1000:1000:james,,,:\/home\/james:\/bin\/bash<br \/>\nvihan:x:1001:1001:,,,:\/home\/vihan:\/bin\/bash<br \/>\nana:x:1002:1002:Ana Joe,,,:\/home\/ana:\/bin\/bash<\/p>\n<\/div>\n<\/div>\n<p>Your system <em><strong>UID_MIN<\/strong> <\/em>and <em><strong>UID_MIX<\/strong> <\/em>values may be varied.<\/p>\n<div class=\"vlt-box\">\n<div class=\"box-content\">eval getent passwd {$(awk &#8216;\/^UID_MIN\/ {print $2}&#8217; \/etc\/login.defs)..$(awk &#8216;\/^UID_MAX\/ {print $2}&#8217; \/etc\/login.defs)}<\/div>\n<\/div>\n<p>You can also print the usernames only by adding pipe to the <em><strong>cut<\/strong> <\/em>command:<\/p>\n<div class=\"vlt-box\">\n<div class=\"box-content\">\n<p>eval getent passwd {$(awk &#8216;\/^UID_MIN\/ {print $2}&#8217; \/etc\/login.defs)..$(awk &#8216;\/^UID_MAX\/ {print $2}&#8217; \/etc\/login.defs)} | cut -d: -f1<\/p>\n<\/div>\n<\/div>\n<p>Feel free to contact us if you have any questions.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this KB article, you will learn how to list and filter users in the Linux system and what are the main differences between system and normal Linux users. The same commands can be run on any Linux distro, including Ubuntu, CentOS, Linux Mint, Debian and RHEL. Get a List of All Users using the [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[1118,1119],"class_list":["post-6846","post","type-post","status-publish","format-standard","placeholder-for-hentry","category-howtos","tag-linux-commands","tag-linux-list-users"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to List and Filter Users in Linux<\/title>\n<meta name=\"description\" content=\"In this KB article, you will learn how to list and filter users in any Linux distro including Ubuntu, CentOS, Linux Mint, Debian and RHEL.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/how-to-list-and-filter-users-in-linux\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to List and Filter Users in Linux\" \/>\n<meta property=\"og:description\" content=\"In this KB article, you will learn how to list and filter users in any Linux distro including Ubuntu, CentOS, Linux Mint, Debian and RHEL.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/how-to-list-and-filter-users-in-linux\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Hosting FAQs by MilesWeb\" \/>\n<meta property=\"article:published_time\" content=\"2019-11-30T13:02:10+00:00\" \/>\n<meta name=\"author\" content=\"Ajit\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ajit\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/how-to-list-and-filter-users-in-linux\/\",\"url\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/how-to-list-and-filter-users-in-linux\/\",\"name\":\"How to List and Filter Users in Linux\",\"isPartOf\":{\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#website\"},\"datePublished\":\"2019-11-30T13:02:10+00:00\",\"author\":{\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/7cd39cafedd7652b49b849e5e331cd48\"},\"description\":\"In this KB article, you will learn how to list and filter users in any Linux distro including Ubuntu, CentOS, Linux Mint, Debian and RHEL.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/how-to-list-and-filter-users-in-linux\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/how-to-list-and-filter-users-in-linux\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/how-to-list-and-filter-users-in-linux\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to List and Filter Users in Linux\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#website\",\"url\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/\",\"name\":\"Web Hosting FAQs by MilesWeb\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/7cd39cafedd7652b49b849e5e331cd48\",\"name\":\"Ajit\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/589d86e7a8ab32a9165a816c5a907b0d?s=96&d=blank&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/589d86e7a8ab32a9165a816c5a907b0d?s=96&d=blank&r=g\",\"caption\":\"Ajit\"},\"description\":\"With over 8+ years of experience in the digital marketing industry, I have achieved extensive exposure to result-oriented methodologies. And expertise in different domains like Datacentre Services, Cloud computing technology, Web hosting industry and many more.\",\"url\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/author\/ajit\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to List and Filter Users in Linux","description":"In this KB article, you will learn how to list and filter users in any Linux distro including Ubuntu, CentOS, Linux Mint, Debian and RHEL.","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:\/\/www.milesweb.co.uk\/hosting-faqs\/how-to-list-and-filter-users-in-linux\/","og_locale":"en_GB","og_type":"article","og_title":"How to List and Filter Users in Linux","og_description":"In this KB article, you will learn how to list and filter users in any Linux distro including Ubuntu, CentOS, Linux Mint, Debian and RHEL.","og_url":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/how-to-list-and-filter-users-in-linux\/","og_site_name":"Web Hosting FAQs by MilesWeb","article_published_time":"2019-11-30T13:02:10+00:00","author":"Ajit","twitter_misc":{"Written by":"Ajit","Estimated reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/how-to-list-and-filter-users-in-linux\/","url":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/how-to-list-and-filter-users-in-linux\/","name":"How to List and Filter Users in Linux","isPartOf":{"@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#website"},"datePublished":"2019-11-30T13:02:10+00:00","author":{"@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/7cd39cafedd7652b49b849e5e331cd48"},"description":"In this KB article, you will learn how to list and filter users in any Linux distro including Ubuntu, CentOS, Linux Mint, Debian and RHEL.","breadcrumb":{"@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/how-to-list-and-filter-users-in-linux\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.milesweb.co.uk\/hosting-faqs\/how-to-list-and-filter-users-in-linux\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/how-to-list-and-filter-users-in-linux\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/"},{"@type":"ListItem","position":2,"name":"How to List and Filter Users in Linux"}]},{"@type":"WebSite","@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#website","url":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/","name":"Web Hosting FAQs by MilesWeb","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/7cd39cafedd7652b49b849e5e331cd48","name":"Ajit","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/589d86e7a8ab32a9165a816c5a907b0d?s=96&d=blank&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/589d86e7a8ab32a9165a816c5a907b0d?s=96&d=blank&r=g","caption":"Ajit"},"description":"With over 8+ years of experience in the digital marketing industry, I have achieved extensive exposure to result-oriented methodologies. And expertise in different domains like Datacentre Services, Cloud computing technology, Web hosting industry and many more.","url":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/author\/ajit\/"}]}},"views":842,"_links":{"self":[{"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/posts\/6846","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/comments?post=6846"}],"version-history":[{"count":15,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/posts\/6846\/revisions"}],"predecessor-version":[{"id":6861,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/posts\/6846\/revisions\/6861"}],"wp:attachment":[{"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/media?parent=6846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/categories?post=6846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/tags?post=6846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}