On Linux systems there's the getent
program, which utilizes the standard get*ent(2)
functions (getpwent()
being the one to use here). What you want to retrieve are the contents of the passwd
database (try man nsswitch.conf
for a further understanding):
$ getent passwd
root:x:0:0:root:/root:/bin/bash
sashroot:x:0:0:root:/root:/bin/sash
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:65004:65534:sync:/bin:/bin/sync
games:x:65005:65060:games:/usr/games:/bin/sh
man:x:65006:65012:man:/var/cache/man:/bin/sh
lp:x:65007:7:lp:/var/spool/lpd:/bin/sh
mail:x:65008:65008:mail:/var/mail:/bin/sh
...
It gets a lot more interesting after the standard system accounts scrolled by.
If you're not an a Linux system, you can get this rather easy by writing something that dumps the user database on its own by using the getpwent(2)
function. An example in C would be something like this:
#include <sys/types.h>
#include <pwd.h>
#include <stdio.h>
int
main(void)
{
struct passwd *pwd;
while ((pwd = getpwent()) != NULL) {
printf("user = `%s', uid = %d, gid = %d, name = `%s'\n",
pwd->pw_name, pwd->pw_uid, pwd->pw_gid, pwd->pw_gecos);
}
return 0;
}
Put this in a file test.c
and build it with
$ gcc -o test test.c
Then you can call ./test
and look what you get:
$ ./test
user = `root', uid = 0, gid = 0, name = `root'
user = `sashroot', uid = 0, gid = 0, name = `root'
user = `daemon', uid = 1, gid = 1, name = `daemon'
user = `bin', uid = 2, gid = 2, name = `bin'
user = `sys', uid = 3, gid = 3, name = `sys'
user = `sync', uid = 65004, gid = 65534, name = `sync'
user = `games', uid = 65005, gid = 65060, name = `games'
user = `man', uid = 65006, gid = 65012, name = `man'
user = `lp', uid = 65007, gid = 7, name = `lp'
user = `mail', uid = 65008, gid = 65008, name = `mail'
...
As above, it'll get more interesting later on.
The approach of using getwent()
has the big advantage that you don't have to make any assumptions on how the login system is configured (/etc/passwd
, LDAP, NIS, …?) but just let the system retrieve the information with its actual configuration.
Edit: It might happen that the passwd
database's storage backend, e.g. LDAP, doesn't permit enumerating the database (and thus getpwent()
) but only hand you out data sets explicitly requested by key (e.g. login name or UID, so getpwuid()
or getpwnam()
might work). In this case (and since your target user names are so neatly named) you could still "enumerate" manually with a modification of @masegaloeh's script:
#!/bin/bash
for i in {000000..999999}; do
getent passwd "e${i}" 2> /dev/null
done
How to do this in absence of getent
with the system API (getpwnam()
, getpwuid()
) is left as an exercise to the reader.
$user, $uid, $gid, $name
. Some of that information will need a lookup outside of /etc/passwd.getent passwd
might work.