If you are running a server or a system that is being used by multiple users, you may want to find the users who are logged in currently.
And the easiest way to do that is by executing the users
command:
users
And as you can see, it gets the list of the currently logged-in users.
But there are other ways that give more detailed output than just listing the logged-in users.
Want to know what they are? Here you have it.
How to find logged-in users in Linux
There are multiple ways to find the logged-in users in Linux and I will be sharing the ones that provide more detailed output like IP address, time of logging in, etc.
So let's start with the first one.
Using the w command
This command gives you information like log-in time, IP, the terminal used to log in, and much more.
And execution is also quite simple. All you have to do is execute a single-character command:
w
Here,
USER
indicates the username of the logged-in user.TTY
indicates which terminal was used to log in. Here,tty7
indicates that the user has used the native terminal to log in andpts
indicates the user has logged in via SSH.FROM
is where you'll find the IP of the remotely logged-in used.LOGIN@
indicates the time of logging in.IDLE
shows the time the user is in an idle state (doing nothing).JCPU
is a time used by all the processes attached to the current tty.PCPU
is a time taken by the current process and mentioned in theWHAT
field.WHAT
indicates the current process.
Pretty detailed. Isn't it?
Using the who command
This is yet another way to list logged-in users and can be suitable for those who want to have less detailed output compared to what the w
command gave.
The command execution is quite simple:
who -H
The -H
option prints the heading which makes it easier to understand the output. The heading elements are:
NAME
show the username of the logged-in userLINE
indicates which terminal was used to log inTIME
column shows the time of logging inCOMMENT
is where you'll find the IP of remote logins
I know the column names are pretty different and do not make much sense but that's what the who command offers!
New Book: Efficient Linux at the Command Line
Pretty amazing Linux book with lots of practical tips. It fills in the gap, even for experienced Linux users. Must have in your collection.
Using the finger utility
If you want the most detailed output, the finger utility will do the job as it is a user information utility so if you added extra details while adding users in Linux, they will be reflected here!
But it does not come pre-installed and you'd have so here's how you install it.
For Ubuntu/Debian base:
sudo apt install finger
For Arch Linux:
yay -S netkit-bsd-finger
For Fedora/RHEL:
sudo dnf install finger
Once you are done with the installation, all you have to do is execute the following command:
finger
And as you can see, there are two extra rows for Office and Phone, so if you added extra details while creating a user, it should be reflected here!
Want to log out inactive users?
Once you know which users are inactive, you may want to log them out. So how do you do that? Here's a detailed guide:
I hope you will find this informative. And if you have any queries, feel free to ask in the comments.