• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to List Users in Linux

Written by Admin, Updated On May 17, 2019
terminal, users
List Users in Linux

To configure and manage permission system admin needs list of all users. There are commands to create user, delete user but it’s critical task to list all users in Linux system. This tutorial will show you how to list users in Linux systems.

List all users using /etc/passwd file#

User information is stored in /etc/passwd file. It contains one line with username for each user account on system. You can use less or cat command to see file contents:

cat /etc/passwd

It will show you output as below :

User List Using etc passwd

In above output you can see that, each line has seven fields delimited by colons that contain the following information:

  • Username
  • Encrypted password where x means password is saved in etc/shadow file.
  • UID (User ID)
  • GID (User’s Group ID)
  • Full Name of User
  • User $HOME directory
  • Login shell path

If you want to list only username then you can use awk or cut commands to print only the username:

awk -F: '{ print $1}' /etc/passwd
cut -d: -f1 /etc/passwd

Output will be as following:

Output
root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp
proxy
www-data
...
...
geoclue
gdm
gnome-initial-setup
sshd

List all users using getent command#

Getent command will read entries from databases. It will list users from both LDAP database and /etc/passwd file.

To get a list of all Linux users type the following command:

getent passwd
User List Using Getent

Output will be same as contents of /etc/passwd file. You can also use awk or cut to print only the username:

getent passwd | awk -F: '{ print $1}'
getent passwd | cut -d: -f1

Check If a user exists in the Linux system#

Now we know how to get list of users. But what if you want to check if a user is exists on current system or not. You can check using grep command and without using grep command.

For example, we want to check that tecnstuff user is exists or not then you should issue command as below:

getent passwd | grep tecnstuff
getent passwd tecnstuff
Output
tecnstuff:x:1001:1001:,,,:/home/tecnstuff:/bin/bash

If user will exists in system then it will print details of that user otherwise it will not print anything.

If you wants total number of users exists on your system then you can get it by :

getent passwd | wc -l

It will return number of users as following output:

Output
47

Differentiate System and Normal Users#

System users are created when you are installing OS or updating new packages. There is no more difference between system users and normal users.

Normal users are the users created by the root or another user with sudo privileges. Usually, a normal user has a real login shell and a home directory.

Each user has a UID (User ID) either he is system user or normal user. When normal user adds user by using adduser command. User will be added and UID will be assigned automatically with reference of UID_MIN and UID_MAX from file /etc/login.defs.

You can check UID_MIN and UID_MAX by below command:

grep -E '^UID_MIN|^UID_MAX' /etc/login.defs

It will show output as below:

Output
UID_MIN 1000
UID_MAX 60000

From the above output, we can see that all the normal users should have unique ID between 1000 and 60000.

Now we will issue command to get list of normal users whose unique ID between 1000 and 60000.

getent passwd {1000..60000}
Output
tecnstuff:x:1000:1000:TecNStuff,,,:/home/tecnstuff:/bin/bash
demouser:x:1001:1001:,,,:/home/demouser:/bin/bash

To print only usernames type following command:

eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)..$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)} | cut -d: -f1
Output
tecnstuff
demouser

Get Groups List in Linux#

You can list all groups in Linux by using the following command.

getent group

You can get list all groups with specific user using following command:

getent group | grep tecnstuff

Conclusion#

You learned how to list users in Linux system and also differentiate difference between normal user and system user. Same commands will be used for all the Linux distributions.

If you have any question or suggestion leave comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Check CentOS Version With Different Methods
Next Article   How to Change User Password in Linux

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook