Skip to content

getent

Query NSS databases (users, groups, hosts, services) with practical system administration and security-focused examples


Show user information

getent passwd $(whoami)

Show group information

getent group <groupname>

Determine enxt available UID

getent passwd | awk -F: '($3>600) && ($3<10000) && ($3>maxuid) { maxuid=$3; } END { print maxuid+1; }'

get just the ip for a hostname

getent hosts linux-shell.se | awk '{print $1}'

Get ip from host

getent hosts linux-shell.se | cut -d' ' -f1

Get hostname from host

getent hosts linux-shell.se | awk '{print $2}'

Find which service was used by which port number

getent services 22

Force change password for all users

getent passwd|cut -d: -f1|xargs -n1 passwd -e

Sometimes you just want a quick way to find out if a certain user account is locked

getent shadow | while IFS=: read a b c; do grep -q '!' <<< "$b" && echo "$a LOCKED" || echo "$a not locked"; done

Show crontabs for all users

for user in $(getent passwd|cut -f1 -d:); do echo "### Crontabs for $user ####"; crontab -u $user -l; done

Display the current user’s full name (GECOS field)

getent passwd $(whoami) | cut -d ':' -f 5

Perform a reverse dns lookup

getent hosts linux-shell.se

Find a username by uid

getent passwd 1000

Get list of all groups

getent group

See the member of a group

getent group <group_name>

Get list of all services

getent services

Get all username

getent passwd| awk '{FS="[:]"; print $1}'

Delete all users from sudo group

gpasswd -d $(getent group sudo | cut -d: -f4 | tr "," "\n") sudo

Delete all users from sudo group (the best way)

me=$(id -un)
getent group sudo | awk -F: '{split($4,a,","); for (i in a) print a[i]}' \
| while read -r user; do
    [ -n "$user" ] && [ "$user" != "$me" ] && gpasswd -d "$user" sudo
  done