Most of us use the id command
, and some users filter the user information from the /etc/passwd
file.
If you are a beginner for Linux operating systems and want to know more about the /etc/passwd file, please refer the following article.
In general, we use the above two commands to obtain user information. You may ask why to discuss this basic topic. People think there are no alternatives other other than these two commands, but we found that there are other ways to gather user information. Hence, we have created this article to guide you.
In this tutorial, we will discuss all these methods in detail. This is one of the basic commands that help the administrator to find information about a user in Linux.
Remember that everything is stored in a file on Linux, so be careful when handling the configuration file.
We have added several tweaks in this tutorial to collect user information with different aspect.
The following articles may help you to learn more about user management in Linux.
- How to Check User Created Date on Linux
- How to Check Which Groups a User Belongs To on Linux
- How to Force User to Change Password on Next Login on Linux
What kind of information is stored in the ‘/etc/passwd’ file
When creating users on Linux the user details are stored in the “/etc/passwd” file.
Each user information in this file is a single line with seven fields and the actual password is stored in the /etc/shadow file.
User information can be queried using these six methods:
id :
Print user and group information for the specified username.getent :
Get entries from Name Service Switch libraries./etc/passwd file :
The /etc/passwd file contain each/every user details as a single line with seven fields.finger :
User information lookup programlslogins :
lslogins display information about known users in the systemcompgen :
compgen is bash built-in command and it will show all available commands for the user.
1) How to check user information using id command?
The id command stands for identity. It prints real and effective user and group IDs. You can use the id command to print user and group information for the specified user, or for the current user.
Run id command without any username to print the current user information on your terminal.
# id uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
To collect information on a specific user, use the id command followed by the specific username as shown below:
# id daygeek uid=500(daygeek) gid=500(daygeek) groups=500(daygeek),10(wheel)
Details of the above output.
uid=500(daygeek):
It shows the user ID & namegid=500(daygeek):
It displays the user’s primary group ID & namegroups=500(daygeek),10(wheel):
It displays the user’s secondary groups ID & name
If you want to print multiple user information simultaneously using the id command, use the following small shell script. This script stores the list of users who have a home directory on the system in the variable.
To achieve this, write the following simple bash scripts.
# vi /opt/script/user-info.sh #!/bin/bash userids=$(getent passwd | grep '/home' | cut -d: -f1) for user in $userids do id $user done
Set an executable permission to “user-info.sh” file.
$ chmod +x /opt/script/user-info.sh
Finally run the script to get results:
# sh /opt/script/user-info.sh uid=500(daygeek) gid=500(daygeek) groups=500(daygeek),10(wheel) uid=501(user1) gid=501(user1) groups=501(user1),10(wheel) uid=502(cat) gid=502(cat) groups=502(cat) uid=503(user2) gid=503(user2) groups=503(user2) uid=504(sudha) gid=504(sudha) groups=504(sudha) uid=505(u1) gid=505(u1) groups=505(u1) uid=506(u2) gid=506(u2) groups=506(u2) uid=507(u3) gid=507(u3) groups=507(u3) uid=508(u4) gid=508(u4) groups=508(u4) uid=509(u5) gid=509(u5) groups=509(u5)
2) Checking user information using getent command
The getent command displays entries from databases supported by the Name Service Switch libraries, which are configured in /etc/nsswitch.conf. The getent command displays user information such as the /etc/passwd file, which displays each user information on a separate line with seven fields.
# getent passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin . . . sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin nslcd:x:65:55:LDAP Client User:/:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin daygeek:x:500:500:2daygeek:/home/daygeek:/bin/bash user1:x:501:501::/home/user1:/bin/bash cat:x:502:502::/home/cat:/bin/bash user2:x:503:503::/home/user2:/bin/bash sudha:x:504:504::/home/sudha:/bin/bash u1:x:505:505::/home/u1:/bin/bash u2:x:506:506::/home/u2:/bin/bash u3:x:507:507::/home/u3:/bin/bash u4:x:508:508::/home/u4:/bin/bash u5:x:509:509::/home/u5:/bin/bash
The seven fields are described below in detail:
magesh:x:502:503:2g Admin - Magesh M:/home/magesh:/bin/bash
Username (magesh):
Username of created user. Characters length should be between 1 to 32.Password (x):
It indicates that encrypted password is stored at /etc/shadow file.User ID (UID-502):
It indicates the user ID (UID) each user should be contain unique UID. UID (0-Zero) is reserved for root, UID (1-99) reserved for system users and UID (100-999) reserved for system accounts/groupsGroup ID (GID-503):
It indicates the group ID (GID) each group should be contain unique GID is stored at /etc/group file.User ID Info (2g Admin - Magesh M):
It indicates the command field. This field can be used to describe the user information.Home Directory (/home/magesh):
It indicates the user home directory.shell (/bin/bash):
It indicates the user’s bash shell.
If you want to display only the usernames in the getent command output, use the below format:
# getent passwd | cut -d: -f1 root bin daemon adm lp . . . apache unbound mysql gdm sshd nslcd tcpdump daygeek user1 cat user2 sudha u1 u2 u3 u4 u5
If you just want to display users that have home directory on system, use the below format:
# getent passwd | grep '/home' | cut -d: -f1 daygeek user1 cat user2 sudha u1 u2 u3 u4 u5
Use the following format to add a UID to the getent command output:
# getent passwd | grep '/home' | cut -d: -f1,3 | sed 's/:/ /' | column -t daygeek 500 user1 501 cat 502 user2 503 sudha 504 u1 505 u2 506 u3 507 u4 508 u5 509
3) Checking user name and related information in Linux, using /etc/passwd file
The /etc/passwd
is a text file containing every user information that is required to log in to the Linux system.
It holds useful information about users such as username, password, user ID, group ID, user ID information, home directory and shell. Each user profile in the “/etc/passwd” file is a single seperate lines with seven fields as described earlier in section #2.
# cat /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin . . . daygeek:x:500:500:2daygeek:/home/daygeek:/bin/bash user1:x:501:501::/home/user1:/bin/bash cat:x:502:502::/home/cat:/bin/bash user2:x:503:503::/home/user2:/bin/bash sudha:x:504:504::/home/sudha:/bin/bash u1:x:505:505::/home/u1:/bin/bash u2:x:506:506::/home/u2:/bin/bash u3:x:507:507::/home/u3:/bin/bash u4:x:508:508::/home/u4:/bin/bash u5:x:509:509::/home/u5:/bin/bash
If you only want to display usernames from the /etc/passwd file, use the below format:
# cut -d: -f1 /etc/passwd root bin daemon adm lp . . . daygeek user1 cat user2 sudha u1 u2 u3 u4 u5
If you only want to display users that have home directory on system, use the below format:
# cat /etc/passwd | grep '/home' | cut -d: -f1 daygeek user1 cat user2 sudha u1 u2 u3 u4 u5
Use the following format to add a UID to the following command output:
# cat /etc/passwd | grep '/home' | cut -d: -f1,3 | sed 's/:/ /' | column -t daygeek 500 user1 501 cat 502 user2 503 sudha 504 u1 505 u2 506 u3 507 u4 508 u5 509
If you only want to display users that have UID greater than 500 on the system, use the below format:
# awk -F: '$3 > 500' /etc/passwd | grep -v nologin | cut -d: -f1 user1 cat user2 sudha u1 u2 u3 u4 u5
4) How to check user information using finger Command
The finger command displays information about the system users. It displays the user’s real name, tty name, idle time, login time, home directory and shell name.
# finger magesh Login: magesh Name: 2g Admin - Magesh M Directory: /home/magesh Shell: /bin/bash Last login Tue Jul 17 22:46 (EDT) on pts/2 from 103.5.134.167 No mail. No Plan.
Details of the above output.
Login:
User’s login nameName:
Additional/Other information about the userDirectory:
User home directory informationShell:
User’s shell informationLAST-LOGIN:
Date of last login and other information
5) Checking user information in Linux, using lslogins command
It displays information about known users in the system. By default it will list information about all the users in the system.
The lslogins utility is inspired by the logins utility, which first appeared in FreeBSD 4.10.
# lslogins -u UID USER PWD-LOCK PWD-DENY LAST-LOGIN GECOS 0 root 0 0 00:17:28 root 500 centos 0 1 Cloud User 501 prakash 0 0 Apr12/04:08 2018/04/12 502 magesh 0 0 Jul17/22:46 2g Admin - Magesh M 503 thanu 0 0 Jul18/00:40 2g Editor - Thanisha M 504 sudha 0 0 Jul18/01:18 2g Editor - Sudha M
Details of the above output.
UID:
User idUSER:
Name of the userPWD-LOCK:
password defined, but lockedPWD-DENY:
login by password disabledLAST-LOGIN:
Date of last loginGECOS:
Other information about the user
6) How to check user & other information in Linux, using compgen command?
The compgen is a bash built-in command and it will show all available commands, aliases, and functions for you as shown below:
# compgen -u root bin daemon adm lp . . . daygeek user1 cat user2 sudha u1 u2 u3 u4 u5
Wrapping Up
In this guide, you learned several ways to find user information in Linux.
Please share this to your friends if it is helpful to you.