Bash script to get list of local users on Linux

Scenario: How to get the list of normal users and System users in Linux.
System : CentOS/RHEL 6/7

Steps:
1) Login as local users (work without login to root), example I login as bachem.
2) Create the bash script file with file name listusers.sh
$ vi /home/bachem/listusers.sh

#!/bin/bash
# Name: listusers.bash
# Purpose: List all normal user and system accounts in the system. Tested on RHEL / Debian Linux
# Author: Vivek Gite , under GPL v2.0+
# -----------------------------------------------------------------------------------
_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"

3) Make the file executable
$ chmod +x /home/bachem/listusers.sh

4) Finish,running the script and see the output.

[bachem@horse ~]$ vi listusers.sh
[bachem@horse ~]$ ./listusers.sh
----------[ Normal User Accounts ]---------------
bowie:x:1000:1000::/home/bowie:/bin/bash
bachem:x:1001:1001::/home/bachem:/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
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
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:997:995:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
sssd:x:995:993:User for sssd:/:/sbin/nologin
nginx:x:994:990:Nginx web server:/var/lib/nginx:/sbin/nologin
[bachem@horse ~]$

Here steps to save the listusers.sh output to a filename called hostname_users.txt.
1) Create the bash script file with file name listusers_host.sh
$ vi /home/bachem/listusers_host.sh

#!/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 ]---------------" >> /home/bachem/$(hostname)_users.txt
awk -F':' -v "min=${l##UID_MIN}" -v "max=${l1##UID_MAX}" '{ if ( $3 >= min && $3 < = max && $7 != "/sbin/nologin" ) print $0 }' "$_p" >> /home/bachem/$(hostname)_users.txt

echo "" >> /home/bachem/$(hostname)_users.txt
echo "----------[ System User Accounts ]---------------" >> /home/bachem/$(hostname)_users.txt
awk -F':' -v "min=${l##UID_MIN}" -v "max=${l1##UID_MAX}" '{ if ( !($3 >= min && $3 < = max && $7 != "/sbin/nologin")) print $0 }' "$_p" >> /home/bachem/$(hostname)_users.txt

echo "Generate Done,file save as /home/bachem/$(hostname)_users.txt"

2) Make the file executable,
$ chmod +x /home/bachem/listusers_host.sh

3) Finish,running the script and see the output.

[bachem@horse ~]$ ./listusers_host.sh
Generate Done,file save as /home/bachem/horse_users.txt
[bachem@horse ~]$
[bachem@horse ~]$ cat /home/bachem/horse_users.txt
----------[ Normal User Accounts ]---------------
bowie:x:1000:1000::/home/bowie:/bin/bash
bachem:x:1001:1001::/home/bachem:/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
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
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:997:995:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
sssd:x:995:993:User for sssd:/:/sbin/nologin
nginx:x:994:990:Nginx web server:/var/lib/nginx:/sbin/nologin
[bachem@horse ~]$

Here steps to save the listusers.sh output to a filename called ipaddress_users.txt.
1) Create the bash script file with file name listusers_ip.sh
$ vi /home/bachem/listusers_ip.sh

#!/bin/bash
_l="/etc/login.defs"
_p="/etc/passwd"

# Generate active Ip Address
ipaddress=$(ip route get 1 | awk '{print $NF;exit}')

## 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 ]---------------" >> /home/bachem/"$ipaddress"_users.txt
awk -F':' -v "min=${l##UID_MIN}" -v "max=${l1##UID_MAX}" '{ if ( $3 >= min && $3 < = max && $7 != "/sbin/nologin" ) print $0 }' "$_p" >> /home/bachem/"$ipaddress"_users.txt

echo "" >> /home/bachem/"$ipaddress"_users.txt
echo "----------[ System User Accounts ]---------------" >> /home/bachem/"$ipaddress"_users.txt
awk -F':' -v "min=${l##UID_MIN}" -v "max=${l1##UID_MAX}" '{ if ( !($3 >= min && $3 < = max && $7 != "/sbin/nologin")) print $0 }' "$_p" >> /home/bachem/"$ipaddress"_users.txt

echo "Generate Done,file save as /home/bachem/"$ipaddress"_users.txt"

2) Make the file executable,
$ chmod +x /home/bachem/listusers_ip.sh

3) Finish,running the script and see the output.

[bachem@horse ~]$ ./listusers_ip.sh
Generate Done,file save as /home/bachem/192.168.99.11_users.txt
[bachem@horse ~]$ cat 192.168.99.11_users.txt
----------[ Normal User Accounts ]---------------
bowie:x:1000:1000::/home/bowie:/bin/bash
bachem:x:1001:1001::/home/bachem:/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
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
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:997:995:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
sssd:x:995:993:User for sssd:/:/sbin/nologin
nginx:x:994:990:Nginx web server:/var/lib/nginx:/sbin/nologin
[bachem@horse ~]$

Reference:
https://www.cyberciti.biz/faq/linux-list-users-command/


Leave a Reply

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