username Command in Linux With Examples
Linux as an operating system holds the capabilities of handling multiple users. So it is important to keep a check on the users and their related information in order to maintain the integrity and security of the system. Whenever a user is added its information is stored in the “/etc/passwd” file. It keeps the username and other related details. Here are some commands to fetch username and its configurations from the Linux host.
There is no specific “username” command in Linux but there are other several sets of commands that let the user access the various users on the machine.
How to Check User Name in Linux
1. Using `whoami` Command to Check User Name in Linux
Type whoami
 in your terminal. This simple command instantly displays the username of the effective user currently running the shell.
whoami
whoami command to check username
2. Using `id` Command to Check User Name in Linux
id: This command basically prints the information of real and effective user or in other words the current user.
Syntax:
id
Example:
Use the id command followed by the username to print specific user information.
Syntax:
id username
Example:
3. Using `getent` Command to Check User Name in Linux
getent : This command fetches user information from database configured in /etc/nsswitch.conf. file which also includes passwd database.
Syntax:
getent passwd
Example:
Each line above has seven fields separated by colons which contains the following information-
- Username
- Encrypted Password
- User ID number(UID)
- User group ID number(GID)
- Full name of the user(GECOS)
- user home directory and
- Login shell respectively.
4. Using `finger` Command to Check User Name in Linux
finger: It displays the current user’s real name along with the terminal name, write status, idle time, and login time.
Syntax:
finger
Example:
Use the finger command followed by the username to print specific user information.
Syntax:
finger username
Example:
5. Using `lslogins` Command to Check User Name in Linux
It displays information about known users in the system. By default, it will list information about all the users in the system.
Syntax:
lslogins -u
Example:
6. Using `cat` Command to Check User Name in Linux
cat : This command fetches and prints user information from /etc/passwd/ file, where each line contains seven fields as in the output of getent and less command.
Syntax:
cat /etc/passwd/
Example:
6. Using `compgen` Command to Check User Name in Linux
This command also displays the name of all the users without any additional information.
Syntax:
compgen -u
Example:
Note: One can use compgen -c command to list all commands available if he/she is not the admin on a Linux system and doesn’t have the sudo access.
7. Using `$USER` Command to Check User Name in Linux
The $USER
 environment variable holds your username’s value. Run echo
$USER
 to retrieve it.
echo $USER
Check User Name
Conclusion
In this article we discussed that there isn’t a specific command called ‘username’, there are other commands like ‘whoami’, ‘id’, ‘getent’, ‘finger’, ‘lslogins’, ‘cat’, and ‘compgen’ that help you see user info. These commands tell you stuff like who’s currently using the system, what their username is, and other details stored in a file called ‘/etc/passwd’. They’re handy tools for system admins to make sure everything is running smoothly and securely on a Linux system. Plus, you can use simple tricks like ‘$USER’ to easily find out your own username.
Last Updated :
07 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...