There are several simple perl parsers for wtmp files, like wtmp.pl by "Brocade Blue"

http://brocadeblue.blogspot.com/2012/10/perl-script-to-parse-wtmp-logs.html

Full source of wtmp.pl with minor typos fixed:

#!/usr/bin/perl
@type = (
    "Empty", "Run Lvl", "Boot", "New Time", "Old Time", "Init",
    "Login", "Normal",  "Term", "Account"
);
$recs = "";
while (<>) { 
    $recs .= $_;
}
foreach ( split( /(.{384})/s, $recs ) ) {
    next if length(type, line, $inittab, $user, $host, t2, t4, _ =~ /(.{4})(.{4})(.{32})(.{4})(.{32})(.{256})(.{4})(.{4})(.{4})(.{4})(.{4})/s;
    if ( defined $line && $line =~ /\w/ ) {  ##FILTER
        $line =~ s/\x00+//g;
        $host =~ s/\x00+//g;
        $user =~ s/\x00+//g;
        printf(
            "%s %-8s %-12s %10s %-45s \n",
            scalar( gmtime( unpack( "I4", type[ unpack( "I4", $type ) ],
            $user,   $line,   $host
        );
    }
}
printf "\n" 

The script may not work on 64-bit machines. The "384" and long line with (.{4}) should be fixed for 64-bit environment.

PS: to see really all records, disable the expression in the if marked with "##FILTER".

Answer from osgx on serverfault.com
Top answer
1 of 4
18

All three of the files that you want to read are stored in binary format. They are not plain text files and cannot be read with a normal text editor, or by using the cat command. Doing so will result in garbled output as you have noted.

Below are the functions of each of the three files:

  • The file /var/log/btmp records failed login attempts.
  • The file /var/run/utmp allows one to discover information about who is currently using the system. This file will contain information on a user's logins: on which terminals, logouts, system events and the current status of the system, system boot time (used by uptime) etc.
  • The file /var/log/wtmp provides an historical record of utmp data.

You can use the last command to read each of the files.

For example:

sudo last /var/log/btmp` (note: this command needs to be run using sudo)

johndoe@computer:~$ last -f /var/run/utmp
 johndoe   tty7                          Fri Jul 26 17:58   still logged in   
 reboot   system boot  3.5.0-37-generic Fri Jul 26 17:57 - 20:10 (1+02:13)  

johndoe@computer::~$ last -f /var/log/wtmp
 reboot   system boot  3.5.0-37-generic Fri Jul 26 17:57 - 20:16 (1+02:19)   
 johndoe   pts/2        :0               Fri Jul 26 17:52 - 17:55  (00:03)    
 johndoe   pts/5        :0               Fri Jul 26 12:00 - 17:55  (05:55)    
 johndoe   pts/0        :0.0             Fri Jul 26 07:11 - 11:58  (04:46)
 <snip>...

For more information see: Linux Display Date And Time Of Login and the man pages for the command "last".

2 of 4
2

Using Perl 5

#!/usr/bin/env perl
#
# ripped from https://www.hcidata.info/wtmp.htm

use warnings;

@type=("Empty","Run Lvl","Boot","New Time","Old Time","Init","Login","Normal","Term","Account");
$recs = "";

while (<>) {
    $recs .= $_
};

foreach (split(/(.{384})/s, $recs)) {
    next if length(type, line, $inittab, $user, $host, t2, t4, _ =~/(.{4})(.{4})(.{32})(.{4})(.{32})(.{256})(.{4})(.{4})(.{4})(.{4})(.{4})/s;
    if (defined $line && $line =~ /\w/) {
        $line =~ s/\x00+//g;
        $host =~ s/\x00+//g;
        $user =~ s/\x00+//g;
        printf("%s %-8s %-12s %10s %-45s\n",
            scalar(gmtime(unpack("I4", type[unpack("I4", $type)],
            $user,
            $line,
            $host,
        )
    };
};

Output will look something like

Tue Dec 20 08:08:25 2022 Term                       pts/0
Mon Dec 26 02:19:58 2022 Normal   root              pts/0 131.191.30.152
Mon Dec 26 17:27:51 2022 Term                       pts/0
Mon Dec 26 18:23:54 2022 Normal   root              pts/0 131.191.30.152
Mon Dec 26 20:06:19 2022 Term                       pts/0
Wed Dec 28 07:07:29 2022 Normal   root              pts/0 131.191.30.152

last is the canonical way to read wtmp files. But this perl script hints at the wtmp file format.

Ripped from here.

🌐
Codeberg
codeberg.org › hjacobs › utmp
hjacobs/utmp: Pure-Python library to decode/read utmp and wtmp files - Codeberg.org
with open('/var/log/wtmp', 'rb') as fd: buf = fd.read() for entry in utmp.read(buf): print(entry.time, entry.type, entry)
🌐
LinuxQuestions.org
linuxquestions.org › questions › linux-security-4 › var-log-wtmp-72976
/var/log/wtmp
What gets logged to wtmp and how would you read it? When I try reading it with vi i get hex entries. And wtmp.1 gives gibberish. Is there a certain
🌐
Apple Community
discussions.apple.com › thread › 135337
reading wtmp - Apple Community
% file /var/log/wtmp /var/log/wtmp: data Note that the file type is identified as 'data', not ASCII Text. In other words, it's a binary data file, not meant to be read via tools such as pico, vi, cat, etc.
🌐
Medium
bromiley.medium.com › torvalds-tuesday-logon-history-in-the-tmp-files-83530b2acc28
Torvalds Tuesday: Logon History in the *tmp Files | by Matt B | Medium
December 14, 2016 - Linux users will also be familiar with the who command, which prints information about users currently logged into the system. While limited in data capture, who also parses /var/run/utmp to grab its output. You can also force who to parse /var/log/wtmp, obviously providing more details.
🌐
Linux Man Pages
linux.die.net › man › 5 › wtmp
wtmp(5): login records - Linux man page
telnetd(8) sets up a LOGIN_PROCESS entry and leaves the rest to login(1) as usual. After the telnet session ends, telnetd(8) cleans up utmp in the described way. The wtmp file records all logins and logouts. Its format is exactly like utmp except that a null username indicates a logout on the ...
Find elsewhere
🌐
Hewlett Packard Enterprise Community
community.hpe.com › t5 › operating-system-hp-ux › decode-contents-of-wtmp-file › td-p › 5569517
Solved: decode contents of wtmp file - Hewlett Packard Enterprise Community
March 9, 2012 - You can get login ID's, computere names or IP's (depends on your DNS settings I suppose), etc. Perhaps this can help you once your file(s) in question get fixed? ... Sure but your current file has useful info, if you want to spend time getting at it. I.e. make a copy first. Also, you can just clear it with: > /var/adm/wtmps
🌐
The Geek Diary
thegeekdiary.com › what-is-the-purpose-of-utmp-wtmp-and-btmp-files-in-linux
What is the purpose of utmp, wtmp and btmp files in Linux – The Geek Diary
# last -f /var/log/wtmp ### To open wtmp file and view its content use blow command. # last -f /var/run/utmp ### To see still logged in users view utmp file use last command.
🌐
Web Hosting Talk
webhostingtalk.com › showthread.php
How to decrypt wtmp file in linux | Web Hosting Talk
You can put the contents of wtmp logs file in some txt file using following command and it should show you in readable format: /var/log/wtmp >>wtmp.txt
🌐
TREND OCEANS
trendoceans.com › home › blog › topic › tools › what is utmp,wtmp,btmp, and how to read?
What is utmp,wtmp,btmp, and how to read? - TREND OCEANS
June 13, 2022 - The last command leverages the /var/log/wtmp file to display all the previous logged in and logged out data.
🌐
Tenable
tenable.com › audits › items › CIS_Debian_Linux_7_v1.0.0_L2.audit:9fc76c7ae09694838355d879decbeab4
8.1.9 Collect Session Initiation Information - /var/log/wtmp
Add the following lines to the /etc/audit/audit.rules file. -w /var/run/utmp -p wa -k session-w /var/log/wtmp -p wa -k session-w /var/log/btmp -p wa -k session # Execute the following command to restart auditd# pkill -HUP -P 1 auditd Note- Use the last command to read /var/log/wtmp (last with no parameters) and /var/run/utmp (last -f /var/run/utmp)
🌐
Linux TLDR
linuxtldr.com › home › purpose of utmp, wtmp, and btmp files in linux (with an example)
Purpose of utmp, wtmp, and btmp files in Linux (with an Example)
October 31, 2025 - The lastb command utilizes the “/var/log/btmp” file to show you the record of failed login attempts on the target machine, but note that this command requires root or sudo privilege. ... Note that all of the above commands use the “utmp“, “wtmp“, and “btmp” log files but also utilize some other files, and they also show you certain information from the files unless you specify an option.
🌐
Sandfly Security
sandflysecurity.com › blog › using-linux-utmpdump-for-forensics-and-detecting-log-file-tampering
Using Linux utmpdump for Forensics and Detecting Log File Tampering
To start, utmpdump is a utility to dump the system audit logs called utmp, wtmp, and btmp. These logs contain the following data: /var/run/utmp – Contains currently logged in users. /var/log/wtmp – Contains all current and past logins and additional information about system reboots, etc.