How To – List Users and Groups in Ubuntu Linux (Easy)

How To – List Users and Groups in Ubuntu Linux (Easy). In this guide, we show you how to list users in Linux using different methods. After that, we navigate into methods to list groups in the same way.

Primarily, Linux is an open source and very popular operating system to host different servers on the cloud and web. Linux popularity is due to its multi user environment. Used by multiple users at the same time. As a system administrator, you get the benefits of Linux features to control how users and groups interact with the system.

If you are a Linux system administrator then you may often need to create and manage users and groups on your system. This way, you assign user and group based access privileges to files and folders. In some cases, you may need to find out which users belong to which group to perform group management operations. This is the place, where you need to list users and groups in Linux.

In Linux, assign different tasks to users and add those users to a single group to assign proper access rights to all users. For security reasons, it is always a good idea to list all users from time to time and remove any unwanted users from the system. This helps you to defend your system against different types of attacks.

Shall we start the article  How To – List Users and Groups in Ubuntu Linux (Easy).

How to List Users and Group in Linux

In this section, we show you how to list users and groups in Linux using several command line methods.

List All Users in Linux

In Linux, all users are stored in the /etc/passwd file. Use the cat, more or less command followed by the /etc/passwd file to list all users in your system.

				
					cat /etc/passwd
				
			

You should get a list of all users on the following screen.

A brief explanation of each column in the above screenshot is shown below:

  • root: name of the user.
  • b: placeholder for the password file.
  • 0: specify the user ID of the root user.
  • 0: specify the group ID of the root user.
  • root: specify the comment field where you can write a short description of the user.
  • /root: define the home directory of the root user.
  • /bin/bash: specify the user shell. Which is used by the user to log in to the system.

List Users Using cut Command

By default, running the cat command without any filter option shows you the list of all users with other information. If you want to list only usernames then you need to filter the resulting output using the cut command.

				
					cat /etc/passwd | cut -d: -f1
				
			

This shows you all users in Linux:

List Users Using awk Command

Additionally, use the awk Linux command line utility to list only usernames from the /etc/passwd file.

				
					cat /etc/passwd | awk -F: '{print $1}'
				
			

You should see the following screen.

List Users Using getent Command

The getent is a powerful Linux utility that you use with passwd argument to list all users in Linux. By default, the getent command gets user information from the Name Service Switch databases. The Name Service Switch database is located at /etc/nsswitch.conf. See all available data sources using the following command.

				
					cat /etc/nsswitch.conf
				
			

You should see the name service database information on the following screen.

Now, use the getent command with passwd argument to list all users in Linux.

				
					getent passwd
				
			

You should see the list of all users on the following screen.

If you want to retrieve only the username rather than other information then use the cut command to filter the output.

				
					getent passwd | cut -d: -f1
				
			

This shows you only the usernames available on your system.

The getent command also shows you the list of normal and system users. In Linux, the system users have assigned UID from 0 to 999 while normal users’ UID is from 1000 to 60000.

To list all system users, run the following command.

				
					getent passwd {0..999}
				
			

You should see the list of all system users on the following screen.

If you want to see the list of all normal users, run the following command.

				
					getent passwd {1000..60000}
				
			

You should see the list of all normal users on the following screen.

List Only Login Users in Linux

As a system administrator, you also need to monitor which users are currently logged in to your system. In this case, use the who command line utility to list all login users in the Linux system.

				
					who
				
			

This shows you the list of users who are currently logged in to your system.

Use the following command to achieve the same result.

				
					w
				
			

You should see the list of all active login users on the following screen.

A brief explanation of each column is shown below.

  • User: Name of the current login user.
  • TTY: Name of the terminal.
  • From: The name of the remote host from the user connected.
  • Login@: Time of the user login.
  • Idle: Idle time
  • JCPU: Time used by processes attached to the TTY.
  • PCPU: Time used by the process displayed in the WHAT field.
  • WHAT: Active process of the user.

List All Groups in Linux

In Linux, group related information is stored in the file /etc/group. You can use the cat, less, or more command to see a list of all groups in Linux.

				
					cat /etc/group
				
			

This shows you the list of all groups in Linux as shown below.

list all groups in linux

List Groups Using cut Command

In the previous example, the cat command shows you the list of all groups with additional information. So, you can use the cat command with the cut argument to display only the group name.

				
					cat /etc/group | cut -d: -f1
				
			

You should see the list of all group names in the following screen.

The awk command is also used to filter the output similar to the cut command.

				
					cat /etc/group | awk -F: '{print $1}'
				
			

This also shows you the list of all group names in Linux.

List Groups Using getent Command

The getent command uses the group database to fetch the group information in Linux. Run the following command to get a list of all groups in Linux.

				
					getent group
				
			

This shows you the all group available in your Linux system.

You can use the getent command followed by group name to see the member of the specified group. For example, if you want to see the member of the sudo group, run the following command.

				
					getent group sudo
				
			

You should see the following screen.

List Groups for the User

You also use the groups command to see the list of all groups the user is a member of. For example, to see the list of all groups the user vyom is a member of, run the following command.

				
					groups vyom
				
			

You should see all group list on the following screen.

Thank you for reading How To – List Users and Groups in Ubuntu Linux (Easy). We shall conclude this article. 

How To - List Users and Groups in Ubuntu Linux (Easy) Conclusion

In this guide, we showed you different methods to list users and groups in Linux. We also showed you to list all login users in Linux. This helps you to identify who is currently using your Linux server. Now choose your desired method as per your requirement. Lastly, Linux is a very useful operating system for multi user environments. So as a system administrator, this guide aids in managing and listing users and groups in Linux. The above guide also applies to other Linux distributions including, Debian, CentOS, Rocky Linux, Fedora, and more.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x