Linux – How to check ec2 linux instance login history or time stamp

amazon ec2linux

We are two user accessing same linux ec2 instance.
How can we see entire login history with time stamp? including the command executed by each user if possible

We both access it remotely from different IPs

Best Answer

  • entire login history with timestamp

1) Use last command which searches through the /var/log/wtmp file by default and show a list of last logged in users. This file records all logins and logouts.

last
last -f /var/log/other_wtmp_file

Output example:

ec2-user pts/0        212.141.41.42    Mon Sep 23 09:21   still logged in
ec2-user pts/1        212.141.41.42    Mon Sep 23 08:33 - 01:46  (17:12)
  • including command executed by each user

1) This is usually not enabled by default. You can rely on standard shell history capability which is not so bullet proof but may be sufficient. Use history command and HISTTIMEFORMAT variable in case of bash shell to print the time stamp with each history entry. You may try to put this line to the system bash profile (e.g. /etc/profile file or create a new one in /etc/profile.d directory on RHEL systems):

HISTTIMEFORMAT="%d.%m.%y %T"
export HISTTIMEFORMAT

2) Or you can leverage Linux Auditing System and PAM module pam_tty_audit for auditing users. Simply put this line to /etc/pam.d/system-auth on RHEL systems (or similar configuration file on other distros):

session   required pam_tty_audit.so disable=* enable=root

Then, you can query audit daemon logs with ausearch tool for tty events:

ausearch -ts today -m tty -i
ausearch -ts yesterday -m tty -i
ausearch -ts recent -m tty -i

Output (after session logout):

type=TTY msg=audit(09/24/2013 02:29:56.394:31312) : tty pid=7032 uid=ec2-user 
auid=ec2-user ses=1239 major=136 minor=0 comm=bash data="ls",<ret>,"last",<ret>,<^D>

3) Finally, there is still an option to enable process accounting. Install psacct package on RHEL systems, enable the service psacct and use the lastcomm tool to print out information about previously executed commands.

lastcomm

Output:

man                     root     pts/1      0.00 secs Tue Sep 24 02:19
sh                      root     pts/1      0.00 secs Tue Sep 24 02:19
sh                 F    root     pts/1      0.00 secs Tue Sep 24 02:19
less                    root     pts/1      0.00 secs Tue Sep 24 02:19