29

I'm running Ubuntu 10.04. Is there a way I can get a daily report of who has logged onto the box, what time, and even - this may be asking too much - a report of the commands they used? This is a low-usage box and so I think this would be a nice way to see what activity is happening on it.

Along these same lines, I heard it was not possible to track when things are done on the box via non-interactive shells, such as rsync or just remotely executing single commands via ssh. Is that true, or is there a way to log and track this as well?

6 Answers 6

24

The information of who logged in when is available in /var/log/auth.log (or other log files on other distributions). There are multiple log monitoring programs that can extract the information you configure as relevant. On any sane system, every user authentication is logged.

To log every command invocation (but not their arguments), use process accounting, provided by the acct package Install acct on Ubuntu. If the accounting subsystem is up and running, then lastcomm shows information about finished processes.

1
  • 3
    /var/log/secure.log is another common logfile Commented Dec 13, 2011 at 4:38
14

You can also use who or w to see who is currently logged in to the system, including SSH users.

4
  • 17
    last might be a better option for what the OP was looking for...
    – jasonwryan
    Commented Dec 13, 2011 at 6:11
  • 1
    indeed. "last" is the command you want.
    – Sirex
    Commented Dec 13, 2011 at 9:14
  • 4
    This is a MUCH better answer then the selected one.
    – PaulBGD
    Commented Jun 18, 2014 at 23:17
  • I could not find the appropriate log file, last was exactly what I needed.
    – Merlin
    Commented Feb 21, 2020 at 6:53
3

You can also try entering the command last into the console, which displays all recent log-ins, including the user name they logged in under (but does not record if they changed their user name after logging in), IP, date, and duration of time logged in.

This command was mentioned by @jasonwryan in a comment here.

0

You can also modify the bash shell to do some rsylog.

Effectively, you setup rsyslog on a remote host to accept specific connections. Then modify the host shell on which you want to monitor - by compiling your version, with one which has the following enabled:

vi config-top.h
#define SYSLOG_HISTORY
#if defined (SYSLOG_HISTORY)
#  define SYSLOG_FACILITY LOG_USER
#  define SYSLOG_LEVEL LOG_INFO
#endif

Once compiled with this enabled, you can replace bash with this version OR have users be logged into this one by redirecting their logins to it.

For more info:

https://www.pacificsimplicity.ca/blog/remote-logging-using-syslog-and-logging-shell-commands-remotely

1
  • Fine. I will update it. Commented Jul 7, 2016 at 17:53
0

These 2 commands give the same result, I used them a lot to check who have logged in.

Note: 22.22.22.22 logged in 4 times, and still in there.

last




root     pts/0        22.22.22.22    Fri Apr 23 08:36   still logged in
root     pts/0        22.22.22.22    Fri Apr 23 08:29 - 08:36  (00:06)
root     pts/0        22.22.22.22    Fri Apr 23 08:27 - 08:29  (00:02)
root     pts/0        22.22.22.22    Fri Apr 23 08:25 - 08:27  (00:01)
root     pts/0        22.22.22.22    Thu Apr 15 11:51 - 12:00  (00:09)
reboot   system boot  5.4.0-51-generic Thu Apr 15 11:50   still running

wtmp begins Thu Apr 15 11:50:06 2021

============

grep -E "Accepted publickey" /var/log/auth.log




Apr 23 12:25:50 server sshd[59026]: Accepted publickey for root from 22.22.22.22 port 62685 ssh2: RSA SHA256:12
Apr 23 12:27:41 server sshd[59165]: Accepted publickey for root from 22.22.22.22 port 63240 ssh2: RSA SHA256:12
Apr 23 12:29:52 server sshd[60083]: Accepted publickey for root from 22.22.22.22 port 63860 ssh2: RSA SHA256:12
Apr 23 08:36:27 server sshd[63873]: Accepted publickey for root from 22.22.22.22 port 49318 ssh2: RSA SHA256:12
1
  • Any recommendations to then identify who connected as root from 22.22.22.22? Theoretically, they wouldn't have to be root on that device, so not as simple as checking who logged into the device as root. Thank you. Commented Jul 18, 2022 at 16:39
-1

Usually when some one logs into a user system then in /var/log/messages it gets printed as:

sshd[18468]: Accepted keyboard-interactive/pam for root from 134.64.66.666 port 49867 ssh2

So just grep the messages as:

grep -E "Accepted keyboard-interactive/pam for" /var/log/messages
1
  • cat: /var/log/messages: No such file or directory
    – code-8
    Commented Apr 23, 2021 at 12:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .