How can I reliably check which is the last time an Ubuntu machine has been connected to the internet?

Method 1

Even though NetworkManager.conf allows logging, that apparently still goes into syslog. However, kern.log also has it.

grep 'associated' /var/log/kern.log | tail -n1                               
Jun 21 17:08:25 anaconda kernel: [ 4910.819781] wlan1: associated

Method 2

I've found that NetworkManager does store last connection time, and it is sorted in /var/lib/NetworkManager/timestamps file, in the unix epoch time format (seconds since 1970). Mine looks like this for instance:

$ cat /var/lib/NetworkManager/timestamps                                       
[timestamps]
c562ac2d-8911-4273-b165-ed1495b28c9a=1432777079
46cfcdd9-d095-418f-acd6-0a7ca282bb9a=0
d81fb3d0-1717-42c0-903d-4622c2381597=1434895707
b0bdefe6-df88-49bb-83d8-154dd21d77d9=1433093286

To show the latest entry

date --date=@"$( awk -F'=' 'BEGIN {var=0}{if(var<$2) var=$2;} END{print var}' /var/lib/NetworkManager/timestamps )"

Awk will search for largest epoch time (in other words latest), and date will convert it to human readable form.

I suspect also that this file (/var/lib/NetworkManager/timestamps) is used by Edit Connections graphical menu to display last time of connection

enter image description here

The problem is that if you're still connected to an access point, the GUI way keeps showing now not, the time when connection was established last


You can check the file /var/log/syslog it will display the last time you connected to a network.


Example

Jun 21 08:00:00 Ubuntu dhclient: DHCPREQUEST of 192.0.0.0 on wlan0 to 192.0.0.0 port 67 (xid=0xec7c6e7)

You can run a grep command to pull only what you need from the log

< /var/log/syslog grep DHCPREQUEST