How to Access All Users in Linux Using Different Commands?

Linux allows multiple users with their own custom setting and configuration to work together on the same system, even at the same time. It can even allow a particular user to access several sessions from different locations in order to work on the system. Below is a list of different commands to access the list of users in Linux:

1. less command: Each local user’s information is stored in the “/etc/passwd/” file, where each line in the file represents login information for one user. less command extracts user information from that file.

Syntax:

$less /etc/passwd

Example:

using less command in Linux



Each line above has seven fields separated by colons which conatins the following information:

  1. Username
  2. Encrypted Password
  3. User ID number(UID)
  4. User group ID number(GID)
  5. Full name of the user(GECOS)
  6. user home directory and
  7. Login shell respectively.

2. getent command: 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

3. awk or cut command: If only username is what you want, use awk or cut commands to print only the field containing the username.

Syntax:

$awk -F: '{print$1}' /etc/passwd
$cut -d: -f1 /etc/passwd
 
$getent passwd | awk -F: '{print$1}'
$getent passwd | cut -d: -f1

Example:



using awk command

using cut command

4. compgen command: 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 don’t have the sudo access.

5. who command: This will print the info of the currently logged in user.

Syntax:

$who

Example:

using who command in Linux

6. wc Command: This command will get the total number of users on a particular linux system.

Syntax:

$getent passwd |wc -l

Example:

using wc command to find all the users in Linux System

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : nidhi_biet

Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.