Sometimes you may need to check user login activity in Linux. This is especially important when you have multiple users logging into your Linux system from different locations. You can easily do this using last command. In this article, we will look at how to view user login history in Linux.
How to View User Login History in Linux
You need to be logged into Linux as root or user with sudo privileges to be able to view login history of other users. Otherwise, last command will only show your own login history.
1. View login history of all users
Open terminal and run the following command to list login history of all users.
$ sudo last ubuntu pts/0 106.214.165.131 Wed Jun 30 03:49 still logged in ubuntu pts/0 106.214.165.131 Wed Jun 30 02:35 - 02:35 (00:00) ubuntu pts/0 110.227.202.203 Tue Jun 29 15:05 - 15:06 (00:00) ...
The above output is a list of all user sessions on your system. In the above output, you can see username in first column, its IP address in 3rd column, date as columns 4-6. Columns 7-8 show time of session. If the session is still continuing, then the 8th column shows “still logged in”.
2. View Login history of specific users
If you want to view login history of only specific user, mention that username after last command
$ sudo last <username>
Here is the command to list login history of user ubuntu.
$ sudo last ubuntu
The above command show similar information to using just last command. The only difference is that in this case, it will show information only for user ubuntu and not for all users.
As you can see it is a very useful command to easily monitor user activity on your Linux system. This command is available on almost all Linux systems.
Also read:
How to Send Email Using Python
How to Restrict SSH Access to Specific IP
How to List Currently Logged In Users in Linux
How to Check Uptime Service in Linux
How to Resize Partition in Ubuntu
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.