11

How to print list of users and groups on FreeBSD?

HopelessN00b
  • 54,273
Eonil
  • 11,009

2 Answers2

23

users and groups stored in /etc/group and /etc/passwd. To print:

awk -F":" '{print $1}' /etc/passwd
awk -F":" '{print $1}' /etc/group

for more details

awk -F":" '{print $0 $1 $2}' /etc/passwd
ooshro
  • 11,502
1
  • Developers of the FreeBSD operating system recognized that /etc/passwd is

    • a frequently accessed file (each time someone logs in, you perform an ls [unless resolving numeric IDs is disabled] or similar operation)
    • that can grow quite large (like thousands of users)

    and as such it would be a good idea to optimize read accesses. Therefore since at least FreeBSD version 2.0 (released in 1994), the passwd file is stored in Berkeley DB format at /etc/pwd.db. This file, however, is not human‑readable. For backward compatibility (and to some extent humans) the plaintext database file /etc/passwd is still generated by the pwd_mkdb utility, but it is not necessary anymore for proper functioning of the base system. The /etc/group file does not follow this pattern.

  • If you ensure that /etc/passwd is kept up‑to‑date (i. e. you exclusively use pw [or vipw] for user management, which implicitly call pwd_mkdb), you can directly utilize text processing utilities such as awk as ooshro already presented. Otherwise you need to unwind the Berkeley DB format first (= convert into text). This can be achieved with FreeBSD’s utility pw:

    pw usershow  -a   # -a stands for all
    

    Similarly there is a sub‑command for /etc/group although this database is (as of FreeBSD version 14.1) not stored in Berkeley DB format.

    pw groupshow -a   # `user show` and `group show` are recognized, too
    

    Combining the two into a list (the { … ; } is a list) and applying some cosmetic filtering:

    { pw usershow -a ; pw groupshow -a ; } | cut -d':' -f1 \
                                           | sort -u \
                                           | tr '\n' ' ' \
                                           | fmt
    

    The cut command splits the texts into fields using a colon as the field separator (‑d':') and prints only the first field (‑f1). sort then performs an alphabetical sorting and prints only unique lines (‑u). The transliterate command merges everything into one long line, and fmt wraps this monster line to a reasonable length.

  • However, pw operates only on local users/groups. It inspects only the files in /etc (or the directory specified via the ‑V parameter, available since FreeBSD version 3.4). In an environment with centrally‑managed users/groups distributed via the network, you may have other data sources defined in /etc/nsswitch.conf (name service switch). Replace pw with getent to obtain a comprehensive list:

    { getent passwd ; getent group ;     } | cut -d':' -f1 \
                                           | sort -u \
                                           | tr '\n' ' ' \
                                           | fmt
    

    The getent utility does respect /etc/nsswitch.conf so users and groups from other sources such as an LDAP directory are listed, too.

  • You may be wondering about the purpose of the ‑unique flag to sort. It is a somewhat common pattern to have one group for (almost) every user account (esp. for every user account used by humans as the primary login account); so you have a user eonil and also a group by the name eonil. If output is printed to your terminal (as opposed to a regular text file), you maybe want to highlight users that have a group by the same name:

    {
        getent passwd
        getent group
    } | cut -d':' -f1 | sort | uniq -c | while read -r count identifier
    do
        # We unnnecessarily emit Set Graphics Rendition Zero so the
        # character count as determined by `fmt` is balanced across lines.
        [ ${count:?} -gt 1 ] && tput bold || tput sgr0
        printf '%s' "${identifier:?}"
        tput sgr0
        printf ' '
    done | fmt -w $((2 * $(tput cols)))
    

    The -c option instructs unique to precede every line with a count of occurrences before duplicate lines were removed. If an identifier was present both in the output of getent passwd and getent group, the count is greater than one.

: pwd.db is the world‑readable redacted version, /etc/spwd.db contains password hashes, too.
: Although NIS (Network Information Service) does create Berkeley DB files for all its databases.