23

Is there a user-friendly command I can use to list users in a console on an Ubuntu system?

When I cat /etc/passwd I get a hard-to-read list of users. It would be nice to see an alphabetized list, with entries arranged into columns, and with group names in parentheses beside the group IDs.

1
  • 2
    well, /etc/passwd is arranged into columns... if you want to see only a few colums, perhaps use cut. For alphabetized, there is sort. If you need the group names, play with join (which may actually be able to show only a subset of columns, btw).
    – njsg
    Feb 3, 2013 at 13:15

5 Answers 5

27

Good way for nice output of /etc/passwd file:

$ column -nts: /etc/passwd

Now you can sort it:

$ column -nts: /etc/passwd | sort

With groups names in last column (no parenthesis):

$ paste -d: /etc/passwd <(groups $(cut -d: -f1 /etc/passwd) | sed 's/.*: //') | column -nts: | sort
2
  • Thanks, these commands do what I want. To a newbie this is very daunting though... I suppose I'll have to learn how to create an alias for them. Feb 3, 2013 at 17:07
  • On Centos column didn't like the n option. column -ts: /etc/passwd worked fine. Nov 10, 2015 at 13:19
9

If you have root access on the machine, you can do the following:

sudo grep -vE '^[^*!]+:[*!]:' /etc/shadow | sort | cut -d: -f1 | while read user; do id $user; done | column -ts' ,' | vi '+set nowrap' -

How it works

Become root to read the shadow file. You only require root privileges if you want to check if the user has a password set (human user), otherwise you can just cat /etc/passwd instead of sudo grep ...:

sudo 

Only show users which have a password set:

grep -vE '^[^*!]+:[*!]:' /etc/shadow

Sort by username:

sort 

Discard all information except for the username:

cut -d: -f1

Iterate through the usernames and enrich it with group infomration:

while read user; do id $user; done

Format the input into columns:

column -ts' ,'

Use vi to view the result:

vi '+set nowrap' - 

If you don't have root access,

try something like this:

cut -d: -f1 /etc/passwd | sort | while read user; do id $user; done | sed 's/\(\()\|^\)[^(]*(\|)\)/ /g' | column -t

Its output is a bit different, but I leave it as an exercise to the reader to combine the two parts in this answer into something that fully fits the job. (Don't you just love sed?)

1
  • "I leave it as an exercise to the reader..." :) Feb 3, 2013 at 21:13
2

Something that I do and it works for my purposes is

ls /home

Granted, that doesn't really give you a list of users rather a list of user's home directories and past user's directories but any command you want to do on a user that doesn't exist terminal will tell you and could be a hint to remove the home file that doesn't have a user or move it!

2
  • I like this. It gets the job done on a basic level.
    – aalaap
    Aug 31, 2017 at 10:54
  • This is the only truly user-friendly answer so far.
    – MRule
    Oct 2, 2023 at 11:22
1

In Ubuntu it can be as follows:

seven fields from /etc/passwd stored in $f1,f2...,$f7

while IFS=: read -r f1 f2 f3 f4 f5 f6 f7
do
 echo "User $f1 use $f7 shell and stores files in $f6 directory."
done < /etc/passwd
0

I thought this would be easy with join, but join requires the files to be sorted on the join field. So it required (?) a workaround with temporary files. The output is sorted on user, and displays user, group, and group ID.

uag () {
  TEMP_GROUPS=/var/tmp/sorted_groups
  TEMP_USERS=/var/tmp/sorted_users
  cat /etc/group  | tr ":" " " | sort -k 3 -o $TEMP_GROUPS
  cat /etc/passwd | tr ":" " " | sort -k 4 -o $TEMP_USERS
  join -1 4 -2 3 -o 1.1,2.1,2.3 $TEMP_USERS $TEMP_GROUPS | sort
  rm $TEMP_GROUPS $TEMP_USERS
}

Translate a character into another with tr; sort according to a key field with -k, output to file with -o; join with regard to fields in the first (-1) and second (-2) file, output certain fields in the first file (-o 1.1) as well as the second (,2.1,2.3).

1
  • note that /tmp would be better, because FHS states that /var/tmp won't be wiped on reboots, which we don't really need.
    – strugee
    Sep 9, 2013 at 3:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .