How can I display all users and groups with a command?

410,373

Solution 1

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.

Solution 2

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

Share:
410,373

Related videos on Youtube

Pandya
Author by

Pandya

Started using Linux and StackExchange since Ubuntu 12.04 LTS. Then Upgraded to 14.04 LTS. Now I am using Debian GNU/Linux on my Laptop and PureOS on old Desktop computer. I recommend visiting the Philosophy of GNU Project As I've replaced Ubuntu with Debian GNU/Linux, Now my question(s) are became off-topic on AskUbuntu. So, I continue to Unix & Linux. The second reason for my shifting to U & L is I found U & L more interesting than AU since AU is only Ubuntu specific whereas U & L is a broad concept and in my opinion U & L deserves generic questions. (I know why SE has AU & U & L both).

Updated on September 18, 2022

Comments

  • Pandya
    Pandya over 1 year

    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?

  • steeldriver
    steeldriver over 9 years
    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)
  • muru
    muru over 9 years
    @steeldriver compgen does seem to work for non-local accounts (at least for LDAP).
  • steeldriver
    steeldriver over 9 years
    @muru I was referring to the second method specifically (cat /etc/passwd | cut -d ...)
  • muru
    muru over 9 years
    @steeldriver Ah, sorry. I thought that was a more general observation.
  • Marinos An
    Marinos An about 5 years
    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.