194

I want to display:

  1. All users and

  2. All groups

    in my system using command-line.

users and groups commands display users currently logged in, and groups a user belongs to respectively.

How to display a list of all users and all groups by command-line?

Pandya
  • 34,843
  • 42
  • 126
  • 186

2 Answers2

274

You can display with the help of compgen builtin command as follows:

  1. To display all users run following command:

    compgen -u
    
  2. To display all groups run following command:

    compgen -g
    

However you can also display all users by cut -d ":" -f 1 /etc/passwd.

wjandrea
  • 14,109
  • 4
  • 48
  • 98
Pandya
  • 34,843
  • 42
  • 126
  • 186
  • 17
    Nice! it might be preferable to use `getent passwd` / `getent group` instead of cat'ing the local files (`getent` should work for non-local accounts as well) – steeldriver Aug 23 '14 at 14:31
  • @steeldriver `compgen` does seem to work for non-local accounts (at least for LDAP). – muru Aug 23 '14 at 14:32
  • 1
    @muru I was referring to the second method specifically (`cat /etc/passwd | cut -d ...`) – steeldriver Aug 23 '14 at 14:46
  • @steeldriver Ah, sorry. I thought that was a more general observation. – muru Aug 23 '14 at 14:57
  • Well, on my ubuntu, I have some files created by docker mount with `999:999` as `user:group`, but unfortunately none of the above commands prints them. – Marinos An Mar 19 '19 at 11:31
22

Here we are going to use getent for the detailed the info

We can list the user with the following command:

getent passwd

We can list the group as follows:

getent group

To fetch detail a specific user

getent passwd lalit

Replace the lalit with your user name. Lalit will not be in every system :)

You can read the more into about getent here

Lalit Mohan
  • 321
  • 3
  • 4