24

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

In case this is not possible, a way to check the last time an Ubuntu machine has been connected to a network would be good enough.

28

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

  • Where do I find the file? :) Also does this checks the last time the machine has been connected to the internet or the last time the machine has been connected to a network? Lastly, does this work afterwards? – kos Jun 21 '15 at 14:08
  • @kos apparently that still goes into the syslog. I'm gonna research a little more, and if I find something, I'll update my answer. – Sergiy Kolodyazhnyy Jun 21 '15 at 14:14
  • Ok I checked man NetworkManager.conf, the file is /etc/NetworkManager/NetworkManager.conf; this is going to work only after having enabled logging in the daemon, but unfortunately I need to check this afterwards supposing that the machine has not been configured for this. Anyway +1 for the kern.log solution and for the first solution which might be helpful in other cases – kos Jun 21 '15 at 14:28
  • 2
    @kos So that file contains hex-string=epoch-timestamp. hex strings are access points. epoch-timestamps are latest time of connection. Try my awk version there, I've edited my answer – Sergiy Kolodyazhnyy Jun 21 '15 at 14:55
  • 2
    @DeadChex I was off by one year :) Corrected already – Sergiy Kolodyazhnyy Jun 21 '15 at 16:08
6

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 
  • 1
    +1, still hoping for a way to check the last time it has been connected to the internet. If nothing comes up within today I'll accept this answer. However I'd like to suggest you a little more user-friendly solution such as < /var/apt/syslog grep DHCPREQUEST or variations. – kos Jun 21 '15 at 14:05
  • 1
    DHCPREQUEST is not reliable. I'am online since afternoon and since I had a lot of DHCP requests. – A.B. Jun 21 '15 at 14:40
  • @Kos you seem to have mistakenly put apt in place of log in your comment. – Sri Jun 26 '15 at 16:59
  • @Sri Indeed, that was a slip. Luckily OP didn't fall for that! – kos Jun 26 '15 at 18:20
5

Check CONNECTED_GLOBAL after we have found link connected in /var/log/syslog

/link connected/,/CONNECTED_GLOBAL/

% awk '/link connected/,/CONNECTED_GLOBAL/ {line=$0} END{print line}' /var/log/syslog
Jun 21 11:12:54 sturm NetworkManager[736]: <info> NetworkManager state is now CONNECTED_GLOBAL Jun 21 11:12:54

% awk '/link connected/,/CONNECTED_GLOBAL/ {month=$1;day=$2;time=$3} END{print month,day,time}' /var/log/syslog
Jun 21 11:12:54
  • There's no need for sudo in the first one, however none of these is working, I think it's because of the POSIXly [[:space:]]. Which version of awk are you running? – kos Jun 21 '15 at 14:34
  • @kos Ups, sudo removed. GNU Awk 4.1.1, API: 1.1 (GNU MPFR 3.1.2-p11, GNU MP 6.0.0) – A.B. Jun 21 '15 at 14:36
  • Ok now it's working :). +1 – kos Jun 21 '15 at 14:39
  • @kos I need my % ;) If there is a output in the line below – A.B. Jun 25 '15 at 20:16
  • 1
    I don't like %. All but this is perfect! – Helio Jun 26 '15 at 18:19
2

If you don't find the "right" way to do this, you can always manufacture your own!

The following bash function will tell you if you're online (to the Internet) or not.

You would just have to write a script which calls it (in a loop and then sleeps) and records the last date and time to a file (overwriting, so it just has just the latest value).

You would have to add code to the loop so it checks as soon as it is initially called and logs (so your initial state is set correctly).

After that, you would only log again when the state first goes offline and when it first comes online after being offline. It's easier to code than to explain. ;)

This would be limited in accuracy by how long a delay (sleep) you use in the loop (to keep it from being a very tight loop that might consume too many system resources.) It would also be unaware of whatever happens before it is started or when it is not running.

The script itself could be started (as a background job (& at the end of the invocation), possibly with a nohup to keep it running if its parent process terminates) when your user logs in by running it from your $HOME/.profile file, starting it with your desktop autostart utility (KDE or Gnome), from a cron job that periodically checks to see if it's not already running, or even from whatever startup system you have (init/systemd/etc., if you know enough to do that.)

When you can't find something like this that does exactly what you want, it's often not too hard to write your own script to do it and run it like a daemon in the background.

It is best to run it from a regular user account if possible - unless you code it in a robust language like C or Python - because shell scripts running with root privileges often pose security risks.

This approach has another issue. Sometimes a ping will timeout giving you a false offline status.

I have a full script, offline_alert, which pretty much does the opposite of what you want - alerts me when I go offline - but the code may prove useful. It has a better offline function which attempts to reduce false positives from ping timeouts.

function offline {
  ## Test if offline
  ## Returns 0 if online, 2 if offline
  local RC
  ping -c 1 google.com > /dev/null 2>&1
  RC=$?
  ##echo "offline returning [${RC}]"
  return $RC
}
2
+100

One way to check any service/daemon log that relay on Internet to work. For example NTP (Network Time Protocol) updates.

See dpkg -L ntpdate for network state hooks

Well as I have checked its log for Ubuntu desktop , its update run whenever network up and keep trying after short delay if last update failed. It connects ntp.ubuntu.com.

(Note, I added first column in the output for my comments)

$ grep -r ntpdate /var/log/syslog*

1st_trial   Jul 14 00:35:56 user-VirtualBox ntpdate[774]: Can't find host ntp.ubuntu.com: Name or service not known (-2)
            Jul 14 00:35:56 user-VirtualBox ntpdate[774]: no servers can be used, exiting
2nd_trial   Jul 14 03:38:57 user-VirtualBox ntpdate[2571]: Can't find host ntp.ubuntu.com: Name or service not known (-2)
            Jul 14 03:38:57 user-VirtualBox ntpdate[2571]: no servers can be used, exiting
3rd_trial   Jul 14 03:46:09 user-VirtualBox ntpdate[2782]: Can't find host ntp.ubuntu.com: Name or service not known (-2)
            Jul 14 03:46:09 user-VirtualBox ntpdate[2782]: no servers can be used, exiting
4th_trial   Jul 14 03:47:48 user-VirtualBox ntpdate[2917]: step time server 91.189.89.199 offset 3.458355 sec
1st_trial   Jul 14 10:23:07 user-VirtualBox ntpdate[728]: Can't find host ntp.ubuntu.com: Name or service not known (-2)
            Jul 14 10:23:07 user-VirtualBox ntpdate[728]: no servers can be used, exiting
2nd_trial   Jul 14 10:37:22 user-VirtualBox ntpdate[2099]: step time server 91.189.89.199 offset 2.021103 sec
dis-/re-con Jul 14 15:27:09 user-VirtualBox ntpdate[20174]: step time server 91.189.89.199 offset 1.677465 sec
1st_trial   Jul 14 23:55:00 user-VirtualBox ntpdate[807]: Can't find host ntp.ubuntu.com: Name or service not known (-2)
            Jul 14 23:55:00 user-VirtualBox ntpdate[807]: no servers can be used, exiting
2nd_trial   Jul 15 00:00:24 user-VirtualBox ntpdate[2041]: step time server 91.189.94.4 offset 1.619839 sec
1st_trial   Jul 15 05:39:08 user-VirtualBox ntpdate[767]: Can't find host ntp.ubuntu.com: Name or service not known (-2)
            Jul 15 05:39:08 user-VirtualBox ntpdate[767]: no servers can be used, exiting
2nd_trial   Jul 15 05:39:21 user-VirtualBox ntpdate[1088]: step time server 91.189.94.4 offset 2.897077 sec
1st_trial   Jul 15 05:49:40 user-VirtualBox ntpdate[787]: Can't find host ntp.ubuntu.com: Name or service not known (-2)
            Jul 15 05:49:40 user-VirtualBox ntpdate[787]: no servers can be used, exiting
2nd_trial   Jul 15 05:49:50 user-VirtualBox ntpdate[1101]: adjust time server 91.189.94.4 offset 0.090520 sec
reconnect   Jul 15 05:55:50 user-VirtualBox ntpdate[2251]: adjust time server 91.189.94.4 offset 0.261432 sec
reconnect   Jul 15 06:04:53 user-VirtualBox ntpdate[2702]: step time server 91.189.94.4 offset 0.525658 sec

I confirm it is installed by default for Ubuntu 14.04 LTS 64Bit desktop & server editions.

I gave it a try in VM, it shows only time it connects or reconnects (with Internet available). Not what you want (last time was connected)

Note, syslog is a rotate log, if you want search all logs even old compressed ones, use zgrep

zgrep -ih ntpdate /var/log/syslog* | sort -bn -k2
  • This looks good, however can you confirm this is enabled by default at least on Ubuntu Desktop? I can't test it personally right now, but I'll indeed do that tomorrow – kos Jul 13 '15 at 22:17
  • 1
    Don't delete it, it's useful and it's good for my scope, if I understood this will report the last time a (more or less) persistent connection has been made, so if I leave the machine connected for say 2 days it will report the date and the time of when the last connection was made, (i.e. 2 days before) right? Just the last thing, what happens when someone disconnects from the network and reconnects, say, 5 minutes later? Does the NTP check runs again? Or is there some kind of threshold after the last NTP check before a new NTP check is run? – kos Jul 15 '15 at 4:34
  • 1
    @kos, don't get confused, ntpdate is installed by default but notntpd/openntpd daemon/service which comes in separate packages. To check that change you time&date to wrong one, disconnect then re-connect. – user.dz Jul 15 '15 at 6:24
  • 1
    @kos, ntpdate could be used with cron to update time periodically, it will have same results as running a service (ntpd/openntpd). But you are looking for default configuration and no additional tunning, right? – user.dz Jul 15 '15 at 6:46
  • 1
    Sorry, I misread. Yes I was looking for something to use on a non configured machine. Ok, this works for me, I'll leave the question open anyway, maybe someone comes up with a killer answer. Thanks! It's a great step forward. Anyway mind that those logs rotate tough, maybe you want to add that to your answer. – kos Jul 15 '15 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.