Introduction
Linux is known for being the one more common with the tech-savvy among us. The reason for this is because the OS is popular with both the tech research and development communities. Which means that some times, the same Linux-based system is used by several users, as both of those communities mostly feature team-based projects. Managing those users, or groups is a basic part of system administration.
In the following tutorial, we are going to teach you how to list every user on any given Linux-based system, this includes teaching you how to view all the groups in the system, and learning how to view what user is currently logged into the system.
Step 1 – Viewing All Users on Linux
The first part of this tutorial is related to just viewing all the users registered on the system.
Linux stores every bit of information related to user registration in a file named passwd located at /etc/passwd. To see the content of the file, follow the next steps:
- Open your terminal.
- Enter the command below and run it:
less /etc/passwd
Once you’ve ran the command above, you will be shown an output, similar or like the one below:
root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh sys:x:3:3:sys:/dev:/bin/sh . . .
As shown in the output above, you will learn that each line actually corresponds to a user. Which means that depending on the users on your system, you should be able to see additional lines displaying every registered user.
Like you can see, every line is broken up by the use of colons (:). Which is known as fields, it is used to hold particular information about that specific user. A total of 7 fields need to be present in any given line of the passwd file. This is the first line, let’s explain it:
root:x:0:0:root:/root:/bin/bash
The fields in this line describe:
- Username. Example: root
- Placeholder for the password(actual password is stored in a separate file). Example: x
- User ID, this is unique for each user registered in the system. By default, root has a User ID of 0.
- Group ID, this will be unique for each registered group in the system. Example: 0
- Comment field. This is supposed to describe the purpose of the user and is typically filled with a short description. Example: root.
- Home directory. This should be the main directory of the user, and for users other than root, it is ‘/home/username’. Example: /root
- User shell. This corresponds to the shell and it will start running once the user signs into the system. Example: /bin/bash
To see only the name of the users, execute the command below in the shell:
cut -d : -f 1 /etc/passwd
This should truncate the result to only mention the first field of the passwd file, and it corresponds to the user names.
Step 2 – Viewing Groups on Linux
In Linux, in addition to making users, you can make groups. Groups bundle users together and it gives them the option to joint privileges or access. It can be useful if several teams are using the same system, since users from every team could put it in a group and be granted special privileges.
Same as all the user information being stored in a single file – passwd, the group information is also stored in a single file which is called group, it is located at /etc/group.
To see the contents of this file, just open up the terminal, then execute the command below:
less /etc/group
After running the command above, you should receive the next output:
root:x:0: daemon:x:1: bin:x:2: sys:x:3: . . .
Keep in mind the number of groups listed are named exactly the same as some of our users. This is because of “user private groups”, it is a scheme employed by Linux for more fine-grained control. For example, you could configure shared directories such as the files inside these directories have the same group owner as the parent directory. We will not explain too much about it in this tutorial, though it can prove to be a really useful feature.
You may view only group names by running the next lines:
cut -d : -f 1 /etc/group
This should show the first field of every line of the /etc/group file, this is usually the group names.
Step 3 – Viewing Logged in Users
Since we know how to list every user and group on the system, we will take a glance at a more frequent, and more useful command related to system administration.
You could see what user is logged currently into the system by opening the terminal and just typing the following letter:
w
This also shows up some additional information, such as their sign in time. Running the ‘w’ command will show the following output, or similar:
10:13:10 up 4:32, 1 user USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 rrcs-72-43-115-1 10:02 12.00s 0.33s 0.33s –bash
As you can see, right now only one user is currently logged into the system (root), with information regarding when the user logged in and more.
There is a different command which does a similar thing to ‘w’:
who
This will reveal the next output:
root pts/0 2017-04-02 10:02
Conclusion
This will conclude this tutorial, you’ve now learned how you can view all users on their linux systems, list all groups and view and monitor every logged-in user.