Linux List Users
At any time, more than one user can access an individual computer system. With these types of distributed systems, the system administrators should take accurate security measures so that a user can't break the other's privacy.
Using the access control mechanism, that defines the privileges of all the users.
A modification in the privileges of users may be mandatory. For example, the user may require her or his privileges grown for a specific task, or a capability of a specific user for accessing the system might have to be entirely revoked.
In these types of scenarios, it is essential for a system administrator for having enough knowledge of each user of a system.
In this topic, we will explore the techniques used for listing the users of the Linux operating system. Both command-line interface-based techniques and the graphical user interface-based techniques could be used for this operation.
The details of the users are stored inside the /etc/passwd file. This file is a plain text file. Hence, we can read this file using a cat or less command.
In this file, every line includes various details about that particular user isolated by a colon (:).
A single line has the following details isolated by a colon (:).
Username- It is a login name of a user. It cannot have spaces or other unique characters. There are alphanumeric characters are permitted only.
Password- The password is saved on the /etc/shadow file in a Linux-based operating system.
GID- All the groups have a numeric ID and a group name which is known as GID or Group ID on Linux-based operating systems. All the users on Linux should be a member of at least a single group.
Hence, for all the users created, the group is also created along with a similar name as the username. Then the user is added to the group. The group ID of the main group is added in this specific field. This main group is the group of the user (default).
When the user is a member of several groups, she/he can modify it while she/he is logged inside with the newgrp command. The GUD begins from 1000 for ordinary groups.
If our Linux is not configured specifically, the Group ID must be similar to the UID.
Note: The root group contains the GID 0.
UID- All the users have a numeric ID and username, or an alphanumeric name which is known as UID or User ID. The UID begins from 1000 for ordinary users and the root user contains the UID 0.
Home Directory Path- It contains a path to the directory in which every user file would be saved. A shell variable that is $HOME is set through this field.
User Details- It contains the complete name and other details about the user as a comma isolated list. One of the most basic uses of this step is to set the user's full name. Just type inside the full name of our user name here and it must be set.
Default Shell- We should have a set of the shell for our users on Linux-based systems. By default, the bash and sh (/bin/sh) shell must be set for users who are capable of logging into the system. However, there are also other shells like csh, zsh, etc.
The users of the system have the /sbin/nologin shell set; hence, they would not be capable of logging into the system. If we wish to disable the login process for a few users, set this field for these types of users to /sbin/nologin.
Sort and list users by their name
We can sort and list the users in alphabetical order. For doing this, we would apply the previous command and also include the sort command.
The command would be as follows:
After using the above command the users are displayed sorted.
List the users with no password
It is necessary to know those users who haven't password and for taking the necessary action. For listing users who don't have any password, we can apply the below command:
After using the above command, we will get the list of users without a password.
List the users through disk usage
We can apply the du command for getting the disk usage if we have a large directory and we wish to aware of which user is overflowing it.
Using this command, we can find which of the users are corrupting the disk space.
The command will be as follows:
After using the above command, we will have the list of users ordered through the disk usage for the directory /home.
List the logged users (currently)
We have a lot of ways for listing the logged-in users (currently). In the first way we can apply the user command:
The above command will list the users along with open sessions inside the system.
However, this information is a bit common. But, we have other commands which provide more details. Simply, the command is w.
We will get more details like the exact time in which the session was begun and the terminal session was available.
Now, there is another command known as who. This command exists to the whole family of UNIX. So, we can apply it to another system such as FreeBSD.
Using the above command, we have also some details about logged-in users currently. We can include the -a option and display every detail.
List the logged users (recently)
We examined how to get the logged-in users currently, but what about list out the user's login history?
We can apply the last command for getting more details of the logins:
Also, we can get the logins of a specific user by using the following command:
List the logins of the users on a particular time or date
For achieving this, we can apply the last command with the -t parameter:
All we have to do is select an exact time and date for listing who logged at that specific time.
List the root users
Usually, there is only a single root user in Linux operating system. When there are several then how to list them?
For doing this, we can apply the following command:
We can filter the file for getting users along with zero UID root users by using the above command.
Another method by inspecting the /etc/group file:
Here we will get the users inside the group route through the /etc/passwd file.
We can also check when any user can run commands as the root by inspecting the file:
List every user of a group
In Linux, there are two important ways for listing the users of a group. The most direct and easiest way for getting the members through the /etc/group files like below:
The above command will list members inside the group1 group.
The second way is by applying commands such as the member's command inside the Debian-based distros. By default, it is not installed in Linux distributions.
List the number of users
In Linux, to list the number of users, we can count lines inside the /etc/passwd file with the help of the wc command which is as follows:
The above command will provide us the regular and system users.
Get sudo users
Linux systems include a utility known as sudo that permits us to run commands as when we were another user usually who is a root user.
It must be managed with care inside the professional environment.
It is also very essential for knowing which user can execute the sudo command. It is sufficient for listing the users that are related to the sudo group.
Users inside this group can run the commands as the super users.
List the users with SSH access
SSH permits users for accessing remote computers on the network. This method is secure and was developed as a substitute for Telnet.
Every regular user can by default login and access SSH. If we wish to restrict it, we can apply the configuration file of SSH (/etc/ssh/ssh_config) and include the directive below:
We can also permit groups rather than permitting users only applying the AllowGroups directive:
These types of directives specify who can use the service. We should not forget to reboot the SSH service.
List the users who have access to a directory or file
We can provide multiple user permissions to modify or access the directories and files in two methods.
In the first method, we can add the users to a group of the directory or file.
We can get the group members with the help of member utility.
But, what when we wish this user for having access to this particular file only (not each group permission)?
For this, we can use the ACL for this particular file with the help of the setfacl command like below:
Here, we provide the user known as the newuser the authentication for the file known as test. It act as a authentications of reading, write, and execution.
Now, the file could be modified or accessed by the owner. The user is known as a newuser. Hence, how to list them?
With the help of the getfacl command, we can get them:
We can get every user using the above command who has authentications for the file along with their related permissions.
List the disabled (locked) users
We could lock any user as the security measures in Linux operating system. It is a precaution when it is uncertain that the user is implementing things wrong, and we do not wish to remove the user completely and only lock them for investigation.
We can apply the following command to lock the user:
After applying the above command, the user named user1' will no longer be capable to use or login into the system.
List the remote users (LDAP)
We can list remote and local users using a single command known as getent.
The above command will list both NIS or NDAP users and local system users or other network users.
We can pipe the outcomes of this command to the above-mentioned commands in a similar way.
The getent command can also list the accounts of a group like below:
We can inspect the command's man page to understand the other databases that the command could find in.
Techniques to List the Users
Technique 1: The "cat" command
We can list each user on the command line to show each user's password and account details in the file /etc/passwd of Linux using the cat command.
To apply the cat command for listing each user in Linux operating system, The below steps must be implemented in sequence:
Running the command will show the usernames and some other information as well. We can scroll this list for viewing each user of the Linux operating system.
Technique 2: The "awk" command
This command is useful if we want to show only user names which may be helpful if we don't require any technical information to be returned using the cat command
To apply this command for listing every user in Linux, the below steps must be implemented in sequence.
When we execute this command in our terminal, we will receive user names only. Also, this list contains every user of our Linux system.
Technique 3: The "compgen" command
The compgen command is applied to show user names only and it is avoiding every other information.
o apply the compgen command for listing every user of Linux, the below steps must be implemented in sequence:
We will get every user name connected with our Linux operating system using this command.
Technique 4: The "getent" command
The result of the cat command is very similar to that of the getent command because it shows several details with the usernames.
To apply the getent command for listing every user inside Linux, the below steps must be implemented in sequence:
The above command will list every user of our Linux system and some other information as well.