Linux has a reputation for being more popular with the tech-savvy among us. This is mainly because the operating system is popular with both the tech research and development communities. This means that often times, the same Linux-based system is used by multiple people (users), as both of these communities largely feature team-based projects. Managing these users, or groups is a basic part of system administration.
Luckily, Linux makes it very easy to access system-wide user information. In this tutorial, we will see how to list all users on any given Linux-based system. We will also learn to view all the groups in the system, as well as finding out how to view which users are currently logged into the system.
Do note that users in Linux are not only the accounts we can make for people – they can also correspond to various system functions. While this is not pertinent to this tutorial, it is something worth keeping in mind.
The only thing you need to follow this guide is any distribution of Linux. For the record, we are working with a Ubuntu VPS.
Step 1 – Viewing All Users on Linux
The first portion of this tutorial is related to simply viewing all users registered on the system. Linux stores all the information related to user registration in a file called passwd located at /etc/passwd. To view the content of the file, simply:
- Open your terminal.
- Type the following command and execute:
After running the above command, you should be entertained with an output along the lines of:
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 . . .
In the output above, each line corresponds to a user. Thus, depending on the users on your system, you will be able to see additional lines displaying all registered users.
As you can see, each line is broken up by the use of colons (:). These are known as fields, and hold specific information about that particular user. A total of 7 fields should be present in any given line of the passwd file. Let’s take a look at the first line:
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, which should be unique for each user registered in the system. By default, root has a User ID of 0.
- Group ID, which should be unique for each registered group in the system. Example: 0
- Comment field. This is meant to describe the purpose of the user and is typically filled with a short description. Example: root.
- Home directory. This is 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 which will start running when this user signs into the system. Example: /bin/bash
To view only the name of the users, you can run this special command in the shell:
cut -d : -f 1 /etc/passwd
This will truncate the result to only mention the first field of the passwd file, which corresponds to the user names.
Step 2 – Viewing Groups on Linux
In Linux, in addition to creating users, you have the option to create groups. Groups bundle users together and grant them joint privileges or access. It is useful if multiple teams are using the same system, as users from each team could be put in a group and granted special privileges.
Just as all the user information is stored in a single file – passwd, the group information is similarly stored in a file named group located at /etc/group.
To view the contents of this file, simply open up the terminal, and type the following command:
The execution of the above line should yield the following output:
root:x:0: daemon:x:1: bin:x:2: sys:x:3: . . .
Note how many of the groups listed are named exactly the same as some of our users. This is a result of “user private groups” – a scheme employed by Linux for more fine-grained control. For instance, you can configure shared directories as such that the files inside those directories have the same group owner as the parent directory. We won’t go too much into it in this guide, but it is, nonetheless, a highly useful feature.
Of course, depending on your particular system, you would be able to see other groups if any have been created in the system.
Just like in the case of users, you can view only group names by executing the following line:
cut -d : -f 1 /etc/group
This will only yield the first field of each line of the /etc/group file, which is actually the group names.
Step 3 – Viewing Logged in Users
Now that we know how to list all the users and groups on our system, let’s take a look at a more frequent, and useful command related to system administration.
You can view which users are currently logged into the system by opening the terminal and simply typing in the following letter:
It also spits out some additional information, like their sign in time. Executing ‘w’ will yield something similar to:
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, only one user is currently logged into the system (root), with information regarding when the user logged in and etc.
There is another command which performs a similar function to ‘w’:
This produces an output like:
root pts/0 2017-04-02 10:02
In this basic system administration tutorial for Linux users, we learned how to perform rudimentary functions. After completing this tutorial, readers should now know how to:
- View all users on their Linux systems
- List all groups
- View and monitor every logged-in user
Linux gives incredible ease of access to system administrators with its many tools and a multitude of simple-to-run commands. Users can achieve basic tasks by running simple instructions, just like we saw in this guide.