YouTube Icon

Code Playground.

How to List Users in Linux

CFG

How to List Users in Linux

Have you ever needed to list all clients in your Linux framework or to include the quantity of clients in the framework? There are orders to make a client, erase a client, list signed in clients, yet what is the order to list all clients in Linux? 

This instructional exercise will tell you the best way to list clients in Linux frameworks. 

Get a List of All Users using the /etc/passwd File

Nearby client data is put away in the/and so forth/passwd record. Each line in this record speaks to login data for one client. To open the document you can either utilize feline or less : 

less /etc/passwd

linux and so forth passwd list clients 

Each line in the record has seven fields delimited by colons that contain the accompanying data: 

Client name. 

Encoded secret key (x implies that the secret word is put away in the/and so on/shadow document). 

Client ID number (UID). 

Client's gathering ID number (GID). 

Complete name of the client (GECOS). 

Client home registry. 

Login shell (defaults to/receptacle/slam). 

On the off chance that you need to show just the username you can utilize either awk or slice orders to print just the main field containing the username: 

awk -F: '{ print $1}' /etc/passwd
cut -d: -f1 /etc/passwd
root
daemon
bin
sys
sync
...
...
sshd
vagrant
jack
anne

Get a List of all Users using the getent Command 

The getent order shows passages from infCheck whether a user exists in the Linux systemormation bases designed in/and so on/nsswitch.conf document, including the passwd data set, which can be utilized to question a rundown, everything being equal. 

To get a rundown of all Linux userr, enter the accompanying order: 

getent passwd

linux getent list clients 

As should be obvious, the yield is equivalent to while showing the substance of the/and so on/passwd document. On the off chance that you are utilizing LDAP for client verification, the getent will show all Linux clients from both/and so on/passwd document and LDAP information base. 

You can likewise utilize awk or slice to print just the main field containing the username: 

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

Check whether a user exists in the Linux system

Since we realize how to list all clients, to check whether a client exists in our Linux box we, can just channel the clients' rundown by funneling the rundown to the grep order. 

For instance, to see whether a client with name jack exists in our Linux framework we can utilize the accompanying order: 

getent passwd | grep jack

Check whether a client exists in the Linux framework 

On the off chance that the client exists, the order above will print the client's login data. No yield that implies the client doesn't exist. 

We can likewise check whether a client exists without utilizing the grep order as demonstrated as follows: 

getent passwd jack

 

Same as in the past, if the client exists, the order will show the client's login data. 

In the event that you need to discover the number of clients accounts you have on your framework, pipe the getent passwd yield to the wc order: 

getent passwd | wc -l
33

As should be obvious from the yield over, my Linux framework has 33 client accounts. 

System and Normal Users

There is no genuine specialized contrast between the framework and ordinary (typical) clients. Ordinarily framework clients are made when introducing the OS and new bundles. At times, you can make a framework client that will be utilized by certain applications. 

Typical clients are the clients made by the root or another client with sudo advantages. Typically, an ordinary client has a genuine login shell and a home registry. 

Every client has a numeric client ID called UID. If not determined while making another client with the useradd order, the UID will be naturally chosen from the/and so forth/login.defs record contingent upon the UID_MIN and UID_MIN esteems. 

To check the UID_MIN and UID_MIN esteems on your framework, you can utilize the accompanying order: 

grep -E '^UID_MIN|^UID_MAX' /etc/login.defs
UID_MIN          1000
UID_MAX         60000

From the yield above, we can see that all typical clients ought to have a UID somewhere in the range of 1000 and 60000. Realizing the insignificant and maximal worth permit us to question a rundown of all typical clients in our framework. 

The order underneath will list all typical clients in our Linux framework: 

getent passwd {1000..60000}

Linux System and Normal Users 

vagrant:x:1000:1000:vagrant,,,:/home/vagrant:/bin/bash
jack:x:1001:1001:,,,:/home/jack:/bin/bash
anne:x:1002:1002:Anne Stone,,,:/home/anne:/bin/bash
patrick:x:1003:1003:Patrick Star,,,:/home/patrick:/usr/sbin/nologin

Your framework UID_MIN and UID_MIN esteems might be extraordinary so the more nonexclusive variant of the order above would be: 

eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)..$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)}

If you want to print only the usernames just pipe the output to the cut command:

eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)..$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)} | cut -d: -f1

Conclusion

In this instructional exercise, you figured out how to rundown and channel clients in your Linux framework and what are the fundamental contrasts among framework and typical Linux clients. 

Similar orders apply for any Linux dispersion, including Ubuntu, CentOS, RHEL, Debian, and Linux Mint. 

Don't hesitate to leave a remark on the off chance that you have any inquiries.




CFG