Have you ever ever needed to checklist all customers in your Linux system or to depend the variety of customers within the system? There are instructions to create a consumer, delete a consumer, checklist logged in customers, however what’s the command to checklist all customers in Linux?
This tutorial will present you the best way to checklist customers in Linux techniques.
Get a Record of All Customers utilizing the /etc/passwd
File
Native consumer data is saved within the /etc/passwd
file. Every line on this file represents login data for one consumer. To open the file you’ll be able to both use cat
or less
:
$ less /etc/passwd
Every line within the file has seven fields delimited by colons that include the next data:
- Person title.
- Encrypted password (
x
signifies that the password is saved within the/etc/shadow
file). - Person ID quantity (UID).
- Person’s group ID quantity (GID).
- Full title of the consumer (GECOS).
- Person dwelling listing.
- Login shell (defaults to
/bin/bash
).
If you wish to show solely the username you need to use both awk
or cut
instructions to print solely the primary area containing the username:
$ awk -F: '{ print $1}' /etc/passwd
$ cut -d: -f1 /etc/passwd
Output:
root
daemon
bin
sys
sync
...
...
sshd
anwen
satish
layla
Get a Record of all Customers utilizing the getent Command
The getent
command shows entries from databases configured in /etc/nsswitch.conf file, together with the passwd
database, which can be utilized to question an inventory of all customers.
To get an inventory of all Linux userr, enter the next command:
$ getent passwd
As you’ll be able to see, the output is similar as when displaying the content material of the /etc/passwd file. If you’re utilizing LDAP for consumer authentication, the getent
will show all Linux customers from each /etc/passwd
file and LDAP database.
You can even use awk
or reduce
to print solely the primary area containing the username:
$ getent passwd | awk -F: '{ print $1}'
$ getent passwd | cut -d: -f1
Examine whether or not a consumer exists within the Linux system
Now that we all know the best way to checklist all customers, to verify whether or not a consumer exists in our Linux field we, can merely filter the customers’ checklist by piping the checklist to the grep
command.
For instance, to search out out if a consumer with title jack
exists in our Linux system we will use the next command:
$ getent passwd | grep satish
If the consumer exists, the command above will print the consumer’s login data. No output which means the consumer doesn’t exist.
We will additionally verify whether or not a consumer exists with out utilizing the grep
command as proven beneath:
$ getent passwd jack
Identical as earlier than, if the consumer exists, the command will show the consumer’s login data.
If you wish to learn the way many customers accounts you might have in your system, pipe the getent passwd
output to the wc
command:
$ getent passwd | wc -l
Output:
35
As you’ll be able to see from the output above, my Linux system has 35 consumer accounts.
System and Regular Customers
There isn’t any actual technical distinction between the system and common (regular) customers. Usually system customers are created when putting in the OS and new packages. In some instances, you’ll be able to create a system consumer that will probably be utilized by some purposes.
Regular customers are the customers created by the foundation or one other consumer with sudo privileges. Normally, a traditional consumer has an actual login shell and a house listing.
Every consumer has a numeric consumer ID known as UID. If not specified when creating a brand new consumer with the useradd
command, the UID will probably be routinely chosen from the /etc/login.defs
file relying on the UID_MIN
and UID_MIN
values.
To verify the UID_MIN
and UID_MIN
values in your system, you need to use the next command:
$ grep -E '^UID_MIN|^UID_MAX' /etc/login.defs
Output:
UID_MIN 1000
UID_MAX 60000
From the output above, we will see that every one regular customers ought to have a UID between 1000 and 60000. Realizing the minimal and maximal worth permit us to question an inventory of all regular customers in our system.
The command beneath will checklist all regular customers in our Linux system:
$ getent passwd {1000..60000}
Output:
anwen:x:1000:1000:Anwen,,,:/dwelling/anwen:/bin/bash
bailey:x:1001:1001:,,,:/dwelling/bailey:/bin/bash
satish:x:1002:1002:Satish Kumar,,,:/dwelling/satish:/bin/bash
kumar:x:1003:1003:Kumar Raj,,,:/dwelling/kumar:/usr/sbin/nologin
Your system UID_MIN
and UID_MIN
values could also be totally different so the extra generic model of the command above can be:
$ eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)..$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)}
If you wish to print solely the usernames simply pipe the output to the cut
command:
$ eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /and so forth/login.defs)..$(awk '/^UID_MAX/ {print $2}' /and so forth/login.defs)} | reduce -d: -f1
Conclusion
On this tutorial, you realized the best way to checklist and filter customers in your Linux system and what are the primary variations between system and regular Linux customers.
The identical instructions apply for any Linux distribution, together with Ubuntu, CentOS, RHEL, Debian, and Linux Mint.
Be happy to go away a remark when you’ve got any questions.
0 Comments