Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How to Get the List of All Users in a Linux System

November 11, 2022
in Blog, Linux
0
ADVERTISEMENT

Table of Contents

You’re a new Linux system administrator and you’re unable to find the command to list all users on your Linux server. What is the command to list users under Linux operating systems? How do I list users in Linux?

The /etc/passwd file contains one line for each Linux user account, with seven fields delimited by colons. This is a text file. You can easily list users under Linux using the cat command or other commands such as grep command/egrep command and more.

This post describes various Linux commands for Linux to list all users and options on the Linux operating system, including Ubuntu, Debian, RHEL, Arch, Fedora, CentOS, and other distros.

Linux list all users account using the /etc/passwd file

cat /etc/passwd
[root@vps01 ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:20:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
...

Each line in the file has seven fields as follows. For example, consider the following line:

daemon:x:2:20:daemon:/sbin:/sbin/nologin
vnstat:x:131:137:vnstat daemon,,,:/var/lib/vnstat:/usr/sbin/nologin
  • : The username or login name.
  • : Encrypted password is stored in the /etc/shadow file.
  • : UID (user ID number)
  • : Primary GID (group ID number)
  • :  Home directory for the user.
  • : Login shell for the user. Pathnames of valid login shells comes from the /etc/shells file.

Of course we can use pagers such as more/less commands as follows to view the /etc/passwd file:

more /etc/passwd
less /etc/passwd
[root@vps01 ~]# more /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
...

Linux list usernames only, to list only usernames, type the following awk command:

awk -F':' '{ print $1}' /etc/passwd
[root@vps01 ~]# awk -F':' '{ print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
...

Get a list of all users using the getent command

getent passwd
getent passwd | grep tom

## get a list all users ##
getent passwd | cut -d: -f1

## count all user accounts using the wc ##
getent passwd | wc -l

How to count user accounts in the Linux server

Want to get user accounts count on your system? Try the wc command as follows:

[root@vps01 ~]# compgen -u | wc -l
34
[root@vps01 ~]# getent passwd | wc -l
34
[root@vps01 ~]#

System and General Users

#!/bin/bash

_l="/etc/login.defs"
_p="/etc/passwd"
 
## get mini UID limit ##
l=$(grep "^UID_MIN" $_l)
 
## get max UID limit ##
l1=$(grep "^UID_MAX" $_l)
 
## use awk to print if UID >= $MIN and UID <= $MAX and shell is not /sbin/nologin   ##
echo "----------[ Normal User Accounts ]---------------"
awk -F':' -v "min=${l##UID_MIN}" -v "max=${l1##UID_MAX}" '{ if ( $3 >= min && $3 <= max  && $7 != "/sbin/nologin" ) print $0 }' "$_p"
 
echo ""
echo "----------[ System User Accounts ]---------------"
awk -F':' -v "min=${l##UID_MIN}" -v "max=${l1##UID_MAX}" '{ if ( !($3 >= min && $3 <= max  && $7 != "/sbin/nologin")) print $0 }' "$_p"
[root@vps01 ~]# chmod +x list.sh
[root@vps01 ~]# ./list.sh
----------[ Normal User Accounts ]---------------
cyberpanel:x:1000:1000::/home/cyberpanel:/bin/false
docker:x:1001:1001::/home/docker:/bin/bash
ftpuser:x:2001:2001:pureftpd user:/bin/null:/bin/false
vmail:x:5000:5000::/home/vmail:/bin/bash
lscpd:x:5001:5001::/usr/local/lscp:/bin/bash
store6519:x:5002:5002::/home/store.soft365.vn:/bin/bash

----------[ System User Accounts ]---------------
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
...
opendkim:x:993:991:OpenDKIM Milter:/var/run/opendkim:/sbin/nologin
saslauth:x:992:76:Saslauthd user:/run/saslauthd:/sbin/nologin
redis:x:991:990:Redis Database Server:/var/lib/redis:/sbin/nologin
ADVERTISEMENT
5/5 - (1 vote)
Previous Post

How to Change Windows 11 Screen Resolution with Command or Batch File

Next Post

How to Get Back the Windows 10 Context Menu in Windows 11

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory