Linux will list all users in the system

I’m a brand new Linux system administrator and I’m unable to search out the command to checklist all customers on my Linux server. What’s the command to checklist customers beneath Linux working programs? How do I checklist customers in Linux?

The /and so on/passwd file incorporates one line for every Linux consumer account, with seven fields delimited by colons. It is a textual content file. You’ll be able to simply checklist customers beneath Linux utilizing the cat command or different instructions equivalent to grep command/egrep command and extra. This web page describes varied Linux instructions to checklist all customers on the Linux working system, together with Ubuntu, Debian, RHEL, Arch, Fedora, CentOS, and different distros.

Linux checklist all customers account utilizing the /and so on/passwd file

Kind any one of many following command:
$ cat /and so on/passwd
Pattern outputs:

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
video games:x:5:60:video games:/usr/video games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
….
..

vnstat:x:131:137:vnstat daemon,,,:/var/lib/vnstat:/usr/sbin/nologin

Every line within the file has seven fields as follows. For instance, take into account the next line:
vnstat:x:131:137:vnstat daemon,,,:/var/lib/vnstat:/usr/sbin/nologin
The place,

  • vnstat – The consumer title or login title.
  • x – Encrypted password is saved within the /and so on/shadow file.
  • 131 – UID (consumer ID quantity)
  • 137 – Major GID (group ID quantity)
  • vnstat daemon – GECOS. It could contains consumer’s full title (or utility title, if the account is for a program), constructing and room quantity or contact individual, workplace phone quantity, dwelling phone quantity and every other contact data.
  • /var/lib/vnstat – Dwelling listing for the consumer.
  • /usr/sbin/nologin – Login shell for the consumer. Pathnames of legitimate login shells comes from the /and so on/shells file.

How you can checklist customers in Linux utilizing pagers

In fact we will use pagers equivalent to extra/much less instructions as follows to view the /and so on/passwd file:
$ extra /and so on/passwd
$ much less /and so on/passwd
Pattern outputs:
Linux will list all users in the systemFig.01: Listing customers utilizing /and so on/passwd
All fields are separated by a colon (:) image. Whole seven fields exists. The primary discipline is username. It’s used when consumer logs in. It must be between 1 and 32 characters in size. In fact we will restrict outputs utilizing the pinnacle command and tail command as follows:
tail -5 /and so on/passwd
head -5 /and so on/passwd

Linux checklist consumer names solely

To checklist solely usernames kind the next awk command:
$ awk -F’:’ ‘{ print $1}’ /and so on/passwd
Pattern outputs:

root
daemon
bin
sys
sync
video games
man
lp
mail
information
….
..
..hplip
vivek
bind
haldaemon
sshd
mysql
radvd

An alternative choice is to make use of the lower command:
$ lower -d: -f1 /and so on/passwd

Get a listing of all customers utilizing the getent command

To get a listing of all Linux customers you’ll be able to kind the next getent command:
$ getent passwd
$ getent passwd | grep tom
## get a listing all customers ##
$ getent passwd | lower -d: -f1
## depend all consumer accounts utilizing the wc ##
$ getent passwd | wc -l
One can use the compgen command on Linux to checklist customers and different assets too:
$ compgen -u

Discover out whether or not a consumer account exists within the Linux server

We are able to use above instructions to see whether or not a consumer exists within the Linux machine as follows utilizing the grep command:

compgen -u | grep vivek
getent passwd | grep -q sai && echo “Person sai discovered” || echo “Person sai not discovered”
compgen -u | grep -q ram && echo “Person ram discovered” || echo “Person ram not discovered”

A simplified command can be:

getent passwd {username}
getent passwd vivek

How you can depend consumer accounts within the Linux server

Wish to get consumer accounts depend in your system? Attempt the wc command as follows:
$ compgen -u | wc -l
$ getent passwd | wc -l
Linux will list all users in the system

A Notice About System and Common Customers

Every consumer has numerical consumer ID referred to as UID. It’s outlined in /and so on/passwd file. The UID for every consumer is mechanically chosen utilizing /and so on/login.defs file once you use useradd command. To see present worth, enter:
$ grep “^UID_MIN” /and so on/login.defs
$ grep UID_MIN /and so on/login.defs
Pattern outputs:

UID_MIN 1000
#SYS_UID_MIN 100

1000 is minimal values for computerized uid choice in useradd command. In different phrases all regular system customers will need to have UID >= 1000 and solely these customers are allowed to login into system if shell is bash/csh/tcsh/ksh and so on as outlined /and so on/shells file. Kind the next command to checklist all login customers:

## get UID restrict ##
l=$(grep “^UID_MIN” /and so on/login.defs)
## use awk to print if UID >= $UID_LIMIT ##
awk -F’:’ -v “restrict=${l##UID_MIN}” ” /and so on/passwd

To see most values for computerized uid choice within the useradd command, enter:
$ grep “^UID_MAX” /and so on/login.defs
Pattern outputs:

UID_MAX 60000

In different phrases, all regular system customers will need to have UID >= 1000 (MIN) and UID /and so on/shells file. Right here is an up to date code to get particulars:

## get mini UID restrict ##
l=$(grep “^UID_MIN” /and so on/login.defs)## get max UID restrict ##
l1=$(grep “^UID_MAX” /and so on/login.defs)

## use awk to print if UID >= $MIN and UID <= $MAX ## awk -F’:’ -v “min=${l##UID_MIN}” -v “max=${l1##UID_MAX}” ‘{ if ( $3 >= min && $3 <= max ) print $0}’ /and so on/passwd

Pattern outputs:

vivek:x:500:500::/dwelling/vivek:/bin/bash
raj:x:501:501::/dwelling/raj:/bin/ksh
ash:x:502:502::/dwelling/ash:/bin/zsh
jadmin:x:503:503::/dwelling/jadmin:/bin/sh
jwww:x:504:504::/htdocs/html:/sbin/nologin
wwwcorp:x:505:505::/htdocs/corp:/sbin/nologin
wwwint:x:506:506::/htdocs/intranet:/bin/bash
scpftp:x:507:507::/htdocs/ftpjail:/bin/bash
rsynftp:x:508:508::/htdocs/projets:/bin/bash
mirror:x:509:509::/htdocs:/bin/bash
jony:x:510:510::/dwelling/jony:/bin/ksh
amyk:x:511:511::/dwelling/amyk:/bin/ksh

/sbin/nologin is used to politely refuse a login i.e. /sbin/nologin shows a message that an account just isn’t accessible and exits non-zero. It’s supposed as a alternative shell discipline for accounts which were disabled or you don’t want consumer to login into system utilizing ssh. To filter /sbin/nologin, enter:

#!/bin/bash
# Title: listusers.bash
# Goal: Listing all regular consumer accounts within the system. Examined on RHEL / Debian Linux to Listing All Customers on Linux
# Creator: Vivek Gite , beneath GPL v2.0+
# ———————————————————————————–
_l=”/and so on/login.defs”
_p=”/and so on/passwd”## get mini UID restrict ##
l=$(grep “^UID_MIN” $_l)

## get max UID restrict ##
l1=$(grep “^UID_MAX” $_l)

## use awk to print if UID >= $MIN and UID <= $MAX and shell just isn’t /sbin/nologin ## awk -F’:’ -v “min=${l##UID_MIN}” -v “max=${l1##UID_MAX}” ‘{ if ( $3 >= min && $3 <= max && $7 != “/sbin/nologin” ) “$_p”

Pattern outputs:

vivek:x:500:500::/dwelling/vivek:/bin/bash
raj:x:501:501::/dwelling/raj:/bin/ksh
ash:x:502:502::/dwelling/ash:/bin/zsh
jadmin:x:503:503::/dwelling/jadmin:/bin/sh
wwwint:x:506:506::/htdocs/intranet:/bin/bash
scpftp:x:507:507::/htdocs/ftpjail:/bin/bash
rsynftp:x:508:508::/htdocs/projets:/bin/bash
mirror:x:509:509::/htdocs:/bin/bash
jony:x:510:510::/dwelling/jony:/bin/ksh
amyk:x:511:511::/dwelling/amyk:/bin/ksh

Lastly, this script lists each system and customers accounts:

#!/bin/bash
# Title: listusers.bash
# Goal: Listing all regular consumer and system accounts within the system. Examined on RHEL / Debian Linux
# Creator: Vivek Gite , beneath GPL v2.0+
# ———————————————————————————–
_l=”/and so on/login.defs”
_p=”/and so on/passwd”## get mini UID restrict ##
l=$(grep “^UID_MIN” $_l)

## get max UID restrict ##
l1=$(grep “^UID_MAX” $_l)

## use awk to print if UID >= $MIN and UID <= $MAX and shell just isn’t /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”

Pattern outputs:

———-[ Normal User Accounts ]—————
vivek:x:500:500::/dwelling/vivek:/bin/bash
raj:x:501:501::/dwelling/raj:/bin/ksh
ash:x:502:502::/dwelling/ash:/bin/zsh
jadmin:x:503:503::/dwelling/jadmin:/bin/sh
wwwint:x:506:506::/htdocs/intranet:/bin/bash
scpftp:x:507:507::/htdocs/ftpjail:/bin/bash
rsynftp:x:508:508::/htdocs/projets:/bin/bash
mirror:x:509:509::/htdocs:/bin/bash
jony:x:510:510::/dwelling/jony:/bin/ksh
amyk:x:511:511::/dwelling/amyk:/bin/ksh

———-[ Linux List All Users (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
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
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
video games:x:12:100:video games:/usr/video games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP Person:/var/ftp:/sbin/nologin
no person:x:99:99:No one:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
vcsa:x:69:69:digital console reminiscence proprietor:/dev:/sbin/nologin
abrt:x:173:173::/and so on/abrt:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/and so on/ntp:/sbin/nologin
saslauth:x:499:499:”Saslauthd consumer”:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
webalizer:x:67:67:Webalizer:/var/www/utilization:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
memcached:x:498:496:Memcached daemon:/var/run/memcached:/sbin/nologin
squid:x:23:23::/var/spool/squid:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service Person:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Nameless NFS Person:/var/lib/nfs:/sbin/nologin

Conclusion

You discovered how you can get a listing of customers in Linux machine. We are able to use the getent, cat, extra, lower and different instructions to fetch checklist of consumer accounts on a Linux system. See how you can examine checklist of customers in Unix and man pages as follows:
man 5 passwd
man getent
man lower
man awk
assist compgen

Posted by: Vivek Gite

The creator is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a coach for the Linux working system/Unix shell scripting. Get the most recent tutorials on SysAdmin, Linux/Unix and open supply subjects through RSS/XML feed or weekly electronic mail e-newsletter.

linux list users and groups,linux command to list all users,linux list users logged in,display all the users whose login shell is not bash,linux create user,linux remove user,linux change user password,ubuntu list users and groups