For example, typing compgen -u
on an Ubuntu box returns a list of users. This includes more users than are listed in /etc/passwd
. So the question is when using bash completion to list out users, where does the list come from?

- 27,486
- 9
- 86
- 108

- 3
- 1
-
It seems to come from `/etc/passwd` on my machine here. – Etan Reisner Jul 22 '14 at 23:18
-
@EtanReisner: Presumably your machine isn't using NIS or LDAP; see my answer. – Keith Thompson Jul 22 '14 at 23:44
-
@KeithThompson Indeed. I almost indicated that additional sources (such as NIS and LDAP) exist and how they might be found via `nsswitch.conf`/etc. but decided that the generic "go find out" answer was better. – Etan Reisner Jul 23 '14 at 02:34
2 Answers
/etc/passwd
is the default source of user account information on UNIX-like systems, but it's often supplemented by other sources, paricularly on machines that are part of some larger organization that needs to keep that information consistent.
NIS (formerly known as "YP") is one common system. LDAP is another. I'm sure there are others.
The getent passwd
command should show you all the relevant account information. On my machine (which doesn't use NIS or LDAP), it's equivalent to cat /etc/passwd
; on yours, it will probably show additional information.
The various getpw*()
functions (getpwuid()
, getpwnam()
, getpwent()
) retrieve user account information, equivalent to what's in /etc/passwd
plus whatever supplement your system uses. Presumably both the getent
command and bash
use this mechanism to obtain the relevant information.

- 254,901
- 44
- 429
- 631
You can run strace -o compgen.out bash -c 'compgen -u'
and then look at the compgen.out
file to try to find out what it is using.
On my machine that file ends with an open("/etc/passwd", O_RDONLY)
followed by the writing of the output.

- 77,877
- 8
- 106
- 148
-
This is the method I ended up using and was able to identify configuration files used and the unix socket that was queried to retrieve the user list. – user3866591 Jul 24 '14 at 17:13