tail -f findings.out

« Great open source virtualization with VirtualBox

Catching warnings from the MySQLdb module »

Viewing all users on a Linux system

There are a number of widely-used and stable utilities on Linux systems that allow you to view information related to users. You can see who’s logged in with who, get info on a particular user with finger, see who you are with whoami and see who logged in last with last and lastlog. And there are commands to create, remove, and change users and their groups.

But what if you just want to see all the users defined on the entire system? Looking in /home won’t show you users that don’t have a home dir or one not located there. All users are indeed listed in /etc/passwd, but having to look through this file every time you just want a list of users is tedious. I haven’t found a utility that performs this role.

This was somewhat surprising, as it’s not an uncommon need. Perhaps I’ve missed some essential program along my Linux journey. If so, feel free to enlighten me in a comment :-) In the meantime, let’s fill this gap:

1
2
3
4
5
6
7
8
9
10
11
12
# Prints all users, divided by login ability and homedir:
function userinfo {
    echo "----- Users that can login -----"
    awk -F":" '!/bin\/false/ { print "username: " $1 ", uid: " $3 ", homedir: " $6 }' /etc/passwd

    echo -e "\n----- And have /home dir -----"
    awk -F":" '!/bin\/false/ && /\/home/ { print "username: " $1 ", uid: " $3 ", homedir: " $6 }' /etc/passwd

    echo -e "\n----- Users that can't login -----"
    awk -F":" '/\/bin\/false/ { print "username: " $1 ", uid: " $3 ", homedir: " $6 }' /etc/passwd
    echo ""
}

This function should work on any *nix system. Place it inside your ~/.bashrc, reload that (. ~/.bashrc), and try out “userinfo”:

userinfo

Three sections are displayed showing usernames, user IDs, and homedirs for users in /etc/passwd who:

  • Don’t have /bin/false for a shell (i.e. users that can login)
  • Also have a homedir under /home (i.e. the users that aren’t for system processes)
  • Do have /bin/false for a shell (i.e. users that can’t login)

Note that the users that can login category only shows users to which one could switch with “sudo su – USERNAME” from a shell. This doesn’t mean anything about whether they can login via SSH or other access methods.

Share and Enjoy:
  • email
  • LinkedIn
  • Slashdot
  • StumbleUpon
  • Technorati
  • Netvibes

Post to Twitter Post to Delicious Post to Digg Post to Reddit

Possibly Related (no promises):

  1. Converting multiple images to one PDF on Linux
  2. Using a Juniper SSL VPN on Ubuntu
  3. Useful Bash functions to determine OS and more
  4. Easily sum matching file line counts
  5. Watching a fork bomb explode

Related posts brought to you by Yet Another Related Posts Plugin.

Tags: , , ,
October 4, 2009 - 3:35 PM
7 comments »
  • Niall

    October 4, 2009 | 4:40 PM

    /etc/passwd isn’t the only place to define users. getent(1) is probably a far safer/reliable mechanism for getting users.

  • Samuel Huckins

    October 4, 2009 | 6:04 PM

    @Niall I hadn’t seen that command actually, very cool. Doesn’t solve the problem though. This just provides easy access to all the locations users can be specified. You still need a utility to wrap these, parsing out their entries and displaying one unified view.

    Version 2.0 of my function I suppose :-)

  • slinger

    October 4, 2009 | 6:25 PM

    /etc/passwd will only list the local users. What about LDAP or NIS users? The getent command is the only way to do that.

  • Samuel Huckins

    October 4, 2009 | 9:18 PM

    @slinger LDAP and NIS already have tools to list their users readily though. Optimally, I’ll re-write this function to pull all the possible sources with getent and parse them the same.

  • Jason

    October 5, 2009 | 8:41 AM

    Nice idea. You can remove the cat, and grep pipes with awk by using something similar to:

    awk -F”:” ‘!/bin\/false/{ print “username: ” $1 “, uid: ” $3 “, homedir: ” $6 }’ /etc/passwd

  • Samuel Huckins

    October 5, 2009 | 7:46 PM

    @Jason Thanks for the improvements! I’ve adjusted the function in the post accordingly, much more elegant.

    I’ve tried to purge the desire to needlessly cat things, but it has been stubborn :-)

  • Hire Skip

    October 27, 2009 | 11:26 PM

    Excellent. I’ve been struggling with this so a big help. Thanks.

Leave a reply

Subscribe without commenting

Twitter links powered by Tweet This v1.6.1, a WordPress plugin for Twitter.