Inspect login records in /var/run/utmp
In this step, you will learn about the /var/run/utmp
file and how commands like who
and w
get their information from it. This file is a crucial part of how Linux tracks who is logged into the system.
The /var/run/utmp
file is a binary file that contains information about the users currently logged into the system. It's constantly updated as users log in and out. Commands like who
, w
, and users
read this file to display current login information.
Because /var/run/utmp
is a binary file, you cannot simply view its contents with commands like cat
or less
as you would with a text file. Doing so would result in unreadable characters.
Let's try to view it with cat
to see what happens (and understand why we don't do this):
cat /var/run/utmp
You will see a lot of garbled characters, which is expected for a binary file.
<binary_output>
To read the contents of /var/run/utmp
in a human-readable format, you typically use commands designed to parse this file, such as who
or w
, which you used in the previous steps.
Another command that specifically reads and displays the contents of /var/run/utmp
is utmpdump
. This command is useful for debugging or examining the raw data in the file.
Let's use utmpdump
to view the contents of /var/run/utmp
.
utmpdump /var/run/utmp
You should see output that is structured and readable, showing records for each login session. The exact output will depend on the current state of the system, but it will look something like this:
Utmp dump of /var/run/utmp
[<type>] [<pid>] [<line>] [<id>] [<user>] [<host>] [<exit>] [<session>] [<tv_sec>] [<tv_usec>] [<addr_v6>]
...
[7] [ <pid> ] [ :0 ] [ :0 ] [ labex ] [ :0 ] [ 0, 0 ] [ <session_id> ] [ <timestamp> ] [ <usec> ] [ <ipv6_address> ]
...
The output shows various fields for each entry in the utmp
file, including the type of entry (like user login), process ID, terminal line, username, host, login time, and more.
Understanding /var/run/utmp
helps you appreciate how Linux keeps track of user sessions and how commands like who
and w
work under the hood.
Click Continue to complete this lab.