2011-02-16

How to dump the full /etc/passwd and /etc/group files when NIS (YP) or LDAP is used

This blog post explains how to dump the flattened version of /etc/passwd and /etc/group files on a Unix system when some user and group entries are stored in a NIS (YP), LDAP or other server.

To dump these fules, run the following commands (not as root, without the leading $).

$ getent group  >/tmp/group
$ getent passwd >/tmp/passwd

The equivalent commands in Perl (use them if getent(1) doesn't work for you):

$ perl -le'$,=":";while(@_=getgrent){$_[3]=~y@ @,@;print@_}'  >/tmp/group
$ perl -le'$,=":";while(@_=getpwent){print@_[0,1,2,3,6,7,8]}' >/tmp/passwd

For NIS (YP) users and groups only, one can use ypcat passwd and ypcat group, respectively.

2 comments:

  1. I haven't tried this out yet, but does this yield an output different to running 'getent passwd' and 'getent group'?

    ReplyDelete
  2. @Roshan: Thanks for mentioning getent. It's equivalent to the Perl scripts. Added it to my blog post.

    ReplyDelete