Users are one of the key pieces in any Linux system since with them we log in and we can perform tasks based on the permissions that have been assigned (administration, reading, writing) and based on that these users will have some authority in the system . An ideal way to take direct control over them and nothing better than doing it knowing how to list users on Linux , so we will know in detail which users we have which should be edited or simply deleted..
It is important to know the users that we have registered within a team, especially if we are an organization and we must unsubscribe those that are not operational. Also at the domestic level it is interesting to know how many users we have created to remove those that are not useful and that in the end are not exercising any function in our system.
TechnoWikis will give you the guidelines to list users in Linus and thus have a centralized control over each of them..
1. View users on Linux using the / etc / passwd file
In the file / etc / passwd the information of the local user is hosted, so this is a good mechanism to know who are registered in the Linux system, since each of the lines in this file gives indications of the startup information of session of each person.
Step 1
For this case we can use the cat or les command as follows:
less / etc / passwd
Step 2
If we use cat we will see the following:
Step 3
This information gives us specific details of each user such as:
- Encrypted password (the letter x indicates that the password is hosted in the / etc / shadow file)
- User Identification Number (UID)
- User Group Identification Number (GID)
- Login shell (default in / bin / bash)
Step 4
In case we do not want to access so many details but only see the username, the awk or cut commands are available to print only the first field (username) as follows:
awk -F: '{print $ 1}' / etc / passwd cut -d: -f1 / etc / passwd
2. List users on Linux with the getent command
Another option to use for user control is with the "getent" command which takes the database entries that have been configured in the file /etc/nsswitch.conf.
Step 1
This includes the passwd database, where user details are hosted, to use this method we must execute the following:
getent passwd
Step 2
As a result we will see the following:
Step 3
In case of using LDAP for the topic of user authentication, getent will deploy all Linux users directly from the / etc / passwd file and from the LDAP database, just like the previous command we can use awk or cut to visualize only the first field (username) with any of the following options:
getent passwd | awk -F: '{print $ 1}' getent passwd | cut -d: -f1
3. See the availability and existence of a user in Linux
With the previous commands we have seen how to deploy all the users of the system, but we also have the option to search for a particular user.
Step 1
For this we can use the following syntax:
getent passwd | grep user
Step 2
If this user exists, we simply access the login details, in case there is nothing printed on the terminal, another method to see the existence of a user is by executing the following:
getent passwd user
Step 3
Another option to use will be to define how many user accounts are currently in Linux, this is achieved by filtering the passtent getent output with the wc command as follows. This command will indicate the number of existing users.
getent passwd | wc -l
4. View normal users and system users in Linux
The users of the system are those users that are created at the time of installing the operating system and the new packages of this and the normal users are those users that are created by the root user or by another user with sudo privileges, this allows A normal user has a login shell and a home directory.
Step 1
For administration reasons, each user is assigned a numerical user ID called UID, in case this is not specified, the UID will be automatically selected from the file /etc/login.defs based on the values ​​UID_MIN and UID_MAX, We can verify the UID_MIN and UID_MAX values ​​with the following command:
grep -E '^ UID_MIN | ^ UID_MAX' /etc/login.defs
Step 2
With these values, it is determined that normal users are in the UID range between 1000 and 60000, now we can meet normal users in Linux with the following command:
getent passwd {1000..60000}
Step 3
Since the UID_MIN and UID_MIN values ​​of the system may vary, the following command displays the results in a global way:
eval getent passwd {$ (awk '/ ^ UID_MIN / {print $ 2}' /etc/login.defs)..$(awk '/ ^ UID_MAX / {print $ 2}' /etc/login.defs)}
Step 4
To list only the user names we execute:
eval getent passwd {$ (awk '/ ^ UID_MIN / {print $ 2}' /etc/login.defs)..$(awk '/ ^ UID_MAX / {print $ 2}' /etc/login.defs)} | cut -d: -f1
Thanks to TechnoWikis, you will now know how to fully know all users in Linux and thus have more complete control over them.