6 Simple ways to check Username and related information in Linux

Most of the admins use the ID command, and some users filter the user information from the /etc/passwd file.

For more information about the /etc/passwd file, go to the following article.

We use the above two methods to obtain user information.You may ask why we are discussing this basic topic. We thought there was no other way than these two. But we have found many ways to gather this information.

We will discuss those methods in detail. This is one of the basic commands that help the administrator to find information about a user on Linux.

Everything is stored in a file on Linux, even user information.

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 on Linux.

What information is stored in the /etc/passwd file

All users are added in the “/etc/passwd” file. It keeps the username and other related details. When you create a user on Linux the details of the users are stored in the /etc/passwd file.

Each user information in this file is a single line with seven fields. The actual password is stored in the /etc/shadow file.

A user information can be found using the below 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 program
  • lslogins :lslogins display information about known users in the system
  • compgen :compgen is bash built-in command and it will show all available commands for the user.

1) How to check username and related information in Linux using the 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 the 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

Use the ID command followed by the username you want to collect information for to print specific user information.

# 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 & name
  • gid=500(daygeek): It displays the user’s primary group ID & name
  • groups=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. Use the following command to export a list of current active users from the system.

To achieve this, write the following simple bash scripts.

# vi /opt/script/user-info.sh

#!/bin/bash
getent passwd | grep '/home' | cut -d: -f1 > /tmp/user-list.txt
for user in `cat /tmp/user-list.txt`
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) How to check username and related information in Linux 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.The getent command displays user information such as the /etc/passwd file, which displays each user information as a single 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/groups
  • Group 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 only want to display users that has 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) How to check username 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 line with seven fields as described above.

# 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
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

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
.
.
.
apache
unbound
mysql
gdm
sshd
nslcd
tcpdump
daygeek
user1
cat
user2
sudha
u1
u2
u3
u4
u5

If you only want to display users that has 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 receiving 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 has 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 username and related information in Linux using the finger Command

The finger command displays information about the system users. It displays the user’s real name, terminal name and write status (as a ‘‘*’’ after the terminal name if write permission is denied), idle time and login time.

# 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 name
  • Name: Additional/Other information about the user
  • Directory: User home directory information
  • Shell: User’s shell information
  • LAST-LOGIN: Date of last login and other information

5) How to check username and related information in Linux using the 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 id
  • USER: Name of the user
  • PWD-LOCK: password defined, but locked
  • PWD-DENY: login by password disabled
  • LAST-LOGIN: Date of last login
  • GECOS: Other information about the user

6) How to check username and related information in Linux using the compgen Command

The compgen is a bash built-in command and it will show all available commands, aliases, and functions for you.

# compgen -u

root
bin
daemon
adm
lp
.
.
.
apache
unbound
mysql
gdm
sshd
nslcd
tcpdump
daygeek
user1
cat
user2
sudha
u1
u2
u3
u4
u5

Prakash Subramanian

Prakash Subramanian is a Linux lover and has 3.5+ years of experience in linux server administration with major Linux distribution such as (RHEL, CentOS, Ubuntu). He is currently working as a Senior L2 Linux Server administrator.

You may also like...