The Wayback Machine - https://web.archive.org/web/20240118180710/https://www.geeksforgeeks.org/username-command-in-linux-with-examples/
Open In App
Related Articles

username Command in Linux With Examples

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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.

1. id: This command basically prints the information of real and effective user or in other words the current user.

Syntax:

$id

Example:

id command in Linux

Use the id command followed by the username to print specific user information.

Syntax:

$id username

Example:

id command followed by the username to print specific user information

2. getent: This command fetches user information from database configured in /etc/nsswitch.conf. file which also includes passwd database.

Syntax:

$getent passwd

Example:

getent command in Linux

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.

3. finger: It displays the current user’s real name along with the terminal name, write status, idle time, and login time.

Syntax:

$finger

Example:

finger command in Linux

Use the finger command followed by the username to print specific user information.

Syntax:

$finger username

Example:

finger command followed by the username to print specific user information

4. lslogins: 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:

lslogins command in Linux

5. 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:

cat command in Linux

6. compgen: This command also displays the name of all the users without any additional information.

Syntax:

$compgen -u

Example:

compgen command in Linux

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.

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 22 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials