There are two types of users in Linux, system users created by default with the system and regular users created by the system administrator. There are different ways to get information about these users, and in this article, you will learn about how to list users on Ubuntu 20.04.
Users are stored in a text file on the system called passwd
file. This file is located in the /etc
directory.
In this file, you can find all the information about the users in the system.
Get the list of all users using /etc/passwd file
Local user information is stored in the /etc/passwd file. Each line in this file represents login information for a user.
A quick and accurate way to list all users of the system is to run the cat or less command on this file. Please, open the terminal from the main menu and run
less /etc/passwd
You will get a screen output similar to this:
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/usr/sbin/nologin man:x:6:12:man:/var/cache/man:/usr/sbin/nologin lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin mail:x:8:8:mail:/var/mail:/usr/sbin/nologin news:x:9:9:news:/var/spool/news:/usr/sbin/nologin uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin proxy:x:13:13:proxy:/bin:/usr/sbin/nologin www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin backup:x:34:34:backup:/var/backups:/usr/sbin/nologin list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
As you can see in the output above, each line has seven delimited fields containing the following information:
- User name
- Encrypted password (Keep in mind that "x" means that the password is stored in the /etc/shadow file)
- User Identification Number (UID)
- User Group Identification Number (GID)
- Full user name (GECOS)
- User home directory
- Login shell (default is /bin/bash)
List the users of the system without more information
This information is quite complete but may be unnecessary in many cases. If you want to show the names of the users without more, you can run
cut -d: -f1 /etc/passwd
Sample Output:
systemd-timesync systemd-network systemd-resolve messagebus rtkit systemd-coredump dnsmasq uuidd geoclue avahi-autoipd usbmux saned colord speech-dispatcher nm-openvpn pulse avahi hplip lightdm
Now you only have the usernames, and the information is much easier to understand.
Check if a user exists in the system
Now that we know how to list all the users and show all the information about them. It's time to check if a user exists in our Ubuntu. To do this, we can filter the list of users thanks to the grep
command.
cat /etc/passwd | grep [username]
If the previous command's execution does not produce any screen output, then the user does not exist. If you do see the information, then it does.
Listing users in Ubuntu 20.04 using the getent command
Another way to list users on Ubuntu 20.04 and other Linux distributions is by using the getent command
The getent
command displays entries from databases supported by the Name Service Switch libraries, which are configured in /etc/nsswitch.conf
So, it is very simple to use
getent passwd
And you will get the same screen output as in the first example.
Even though the output on the screen is the same, we prefer to use this tool instead of working directly with the passwd
file.
Now you know how to get users information in your Ubuntu Shell and the process is quite simple.
Comments
0 comments
Please sign in to leave a comment.