Have you ever been curious to know who is connected to the remote machine or server you have been working on? I mean, who doesn’t want to do that?
Look, listing all the user accounts in your Linux system is quite an easy task, but that doesn’t mean that all those users are connected to the target machine.
However, you can read this article to learn four simple ways to list all the logged-in users on the target machine. Once you find them, you can kick them out of the target machine or say “hi” to them using the write command.
|Description||List All the logged-In Users in Linux|
|Root or Sudo Privileges||No|
|Host System and Architecture||Ubuntu 22.10 (x64)|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
|Prerequisites||w, who, users, finger|
4 Ways to Check Logged-in Users in Linux
To achieve this task, there are various built-in and external command-line tools, but most of the commands share the same output, so once you understand one, you can understand them all.
Using the w command
The w command is a simple single-character command-line utility that can tell you all of the logged-in users on the target machine, as well as their activity.
From the above output, it’s clear that in total, three users (“
david“, and “
jake“) are connected to the target machine.
The output from the preceding command will be identical to the rest of the upcoming commands, so let’s get a good grasp on each field in the output.
USER: The user who is currently logged-in.
TTY: How user are connected to the target machine: “
tty*” means the user is physically connected to the target machine from the command-line, and “
pts” means the user is connected remotely via SSH.
FROM: The user’s hostname or IP address.
LOGIN@: The time when the user logged-in, in 24-hour format.
IDLE: The amount of time the user has been idle.
WHAT: The user’s current process with options and arguments.
Once you understand this field, the next three commands become self explanatory. So, let’s move on to the next command.
Using the who command
The who command is another tool to find all the users logged-in to the target machine.
As you can see, the output is self explanatory.
Using the users command
The previous two commands provide far more information than is necessary while writing a shell script, as you might only be interested in knowing the names of the logged-in users.
The users command is a perfect fit for this type of situation, where the names of the logged-in users matter above all.
Using the finger command
The finger command is an external utility that can give you a list of all connected users to the target machine. Although, this tool is not shipped out of the box, it is available in major Linux repositories.
Execute any one of the following commands to install it on your system.
$ sudo apt install finger #On Debian and Ubuntu $ sudo dnf install finger #On Red Hat and Fedora
After the installation is complete, run the following command without any options to get a list of all logged-in users on the target machine.
How to Find Past Logged-In Users Information
All of the above mentioned methods will only give you the list of users while they are connected to the target machine; once they quit, their entries will be removed from the output.
However, you can use the last command to list all the previous users who are no longer logged-in in order to determine when they logged in and out.
Here comes the end of this article.
All the tools mentioned in the article are great, but if you know any tool that is missing and should be included, let us know in the comment section.
Till then, peace!