details on "last" logins

Posted on 1998-04-08
Last Modified: 2010-04-21
the last command keeps track of the last logins into a system (user, from where, what time etc).  The info (as specified in the man page) is stored in /var/adm/wtmpx.  From what people have told me, that file is a sparse file.  My problem is that the info obtained from typing last is formatted to fit the screen.  In doing so, the location from which the user connected is truncated to ~16 characters.

Is there a way to get last to display that to the full length?  if not, is there somewhere where I can get a program to analyze the wtmpx file with?
Question by:cokeman_
  • 5
  • 4

Expert Comment

Comment Utility
you can write a 'C' program to access these details directly as
explained in the following code.
(Note: this code is for SunOS 5.x, but should be able to use on
other platforms too.)  
This code uses 'getutxent' call to get the utmp entries.  On
other machines this fucntion could be 'getutent'.

#include <stdio.h>
#include <utmpx.h>
        struct utmpx *wt;
        int i = 0;
        utmpxname(WTMPX_FILE);  /* Change database to /var/adm/wtmpx */
        while ( (wt = getutxent()) != NULL ) {
                printf("%s\t",  wt->ut_user);   /*User Name */
                printf("%s\n", wt->ut_host);


Please note these issues:
1. There are lot more fields in the 'utmpx' structure that are
   not processed here.
2. getutxent function gets a entry from the begining of the
   'utmpx/wtmpx' file.  Where as 'last' command displays info
   from the bottom of the file to top.
3. Your platform may support only 16 characters to store the
   remote host name.  In that case even writing a 'C' as above
   will not help.  Verify this by man utmp and check how the
   field ut_host is defined in the utmp structure.

Hope this helps.  BTW, what is your platform?  


Expert Comment

Comment Utility
One more issue:  You would want to skip a lot of entries
based on the 'ut_type' field - just as 'last' does.

Author Comment

Comment Utility
I'm using SunOS 5.5.1, and I've included some of the output from the man page of utmp below:

     #define  UTMP_FILE                  "/var/adm/utmp"
     #define  WTMP_FILE                  "/var/adm/wtmp"
     #define  ut_name                    ut_user

     struct utmp {
              char  ut_user[8];          /* user login name */
              char  ut_id[4];            /* /sbin/inittab id (created by */
                                         /* process that puts entry in utmp) */
              char  ut_line[12];         /* device name (console, lnxx) */
              short ut_pid;              /* process id */
              short ut_type;             /* type of entry */
              struct exit_status {
                   short e_termination;  /* process termination status */
                   short e_exit;         /* process exit status */
              } ut_exit;                 /* exit status of a process
                                         /* marked as DEAD_PROCESS */
              time_t ut_time;            /* time entry was made */
     /*  Definitions for ut_type  */
     #define  EMPTY          0
     #define  RUN_LVL        1
     #define  BOOT_TIME      2
     #define  OLD_TIME       3
     #define  NEW_TIME       4
     #define  INIT_PROCESS   5         /* process spawned by "init" */
     #define  LOGIN_PROCESS  6         /* a "getty" process waiting for login */
     #define  USER_PROCESS   7         /* a user process */
     #define  DEAD_PROCESS   8
     #define  ACCOUNTING     9
     #define  UTMAXTYPE      ACCOUNTING  /* max legal value of ut_type */
     /*  Below are special strings or formats used in the "ut_line"  */
     /*  field when  accounting for something other than a process.  */
     /*  No string for the ut_line field can be more than 11 chars + */
     /*  a null character in length.  */
     #define  RUNLVL_MSG     "run-level %c"
     #define  BOOT_MSG       "system boot"
     #define  OTIME_MSG      "old time"
     #define  NTIME_MSG      "new time"


Author Comment

Comment Utility
ooops, sorry.. I guess I should have looked up the man page for utmpx instead of utmp.. I've added that below:

     utmpx is an extended version of utmp(4).

     utmpx and wtmpx hold user  and  accounting  information  for
     commands  such  as  who, write, and login.  These files have
     the following structure as defined by <utmpx.h>:

     #define  UTMPX_FILE                    "/var/adm/utmpx"
     #define  WTMPX_FILE                    "/var/adm/wtmpx"
     #define  ut_name                       ut_user
     #define  ut_xtime                      ut_tv.tv_sec
     struct utmpx  {
        char    ut_user[32];                /* user login name */
        char    ut_id[4];                   /* inittab id */
        char    ut_line[32];                /* device name */
                                            /* (console, lnxx) */
        pid_t   ut_pid;                     /* process id */
        short   ut_type;                    /* type of entry */
        struct  exit_status ut_exit;        /* process termination/exit */
                                            /* status */
        struct  timeval ut_tv;              /* time entry was made */
        long    ut_session;                 /* session ID, used for */
                                            /* windowing */
        long    pad[5];                     /* reserved for future use */
        short   ut_syslen;                  /* significant length of */
                                            /* ut_host */
                                            /* including terminating null */
        char    ut_host[257];               /* remote host name */
     /* Definitions for ut_type */
     #define  EMPTY             0
     #define  RUN_LVL           1
     #define  BOOT_TIME         2
     #define  OLD_TIME          3
     #define  NEW_TIME          4
     #define  INIT_PROCESS      5           /* Process spawned by "init" */
     #define  LOGIN_PROCESS     6           /* A "getty" process waiting */
                                            /* for login */
     #define  USER_PROCESS      7           /* A user process */
     #define  DEAD_PROCESS      8
     #define  ACCOUNTING        9

     #define  UTMAXTYPE  ACCOUNTING         /* Largest legal value */
                                            /* of ut_type */

     /* Below are special strings or formats used in the "ut_line" */
     /* field when accounting for something other than a process. */
     /* No string for the ut_line field can be more than 11 chars + */
     /* a null character in length. */

     #define  RUNLVL_MSG                    "run-level %c"
     #define  BOOT_MSG                      "system boot"
     #define  OTIME_MSG                     "old time"
     #define  NTIME_MSG                     "new time"
     #define  MOD_WIN                       10

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.


Author Comment

Comment Utility
       char    ut_host[257];               /* remote host name */

it appears that this string is definitely long enough.. hmm.. out of curiousity, do you know why they used 257 instead of something like 255 or 256?  I've never seen 257 being used =)

I'll try this out when I get a chance tomorrow.  Thanks for your help.. it seems like it should give me what I need.

Expert Comment

Comment Utility
257?  No, neither have I.  But could it be 256 + 1 for the null?
Please note the ut_syslen field.  This must be used to find
the number of significant characters in 'ut_host'.  The maximum
for ut_syslen in my machine(Sun OS 5.5.1) was only 16.  But it
could be different in your machine - depending on the entries.

Expert Comment

Comment Utility
Did it work for you?

Author Comment

Comment Utility
it did.. it gave two of everything.. but looking at wt->ut_type, it was coded 7 and 8, i guess indicating login and logout.

hey, do you know how to deal with the timeval struct? ie, to get it into a date/time?


Accepted Solution

seedy earned 100 total points
Comment Utility
The field ut_tv is of type struct timeval.  This structure
is same as that is used in 'gettimeofday' function.
Read man pages for more info.  You can use ctime to convert
this to the character string.  So,
will  do the trick for you.
Ps:  Last actually does a lot more.  It looks at both the login
and logout records and prints the duration of the session.

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now