There are plenty of reasons why you’d want to find out when your Linux computer shut down, restarted, or how long it’s been running. Most of the time this information is invaluable in debugging a problem that may have happened when no one was looking. Thankfully, Linux meticulously logs system events automatically on most distributions. Accessing that logged information from the command line is a breeze as well.
First, if you want to check when your computer last booted up, you can use the
who command with the
-b flag to get an exact date and time in your terminal. You don’t need root privileges, so go ahead and check.
last command you can list every time your system has rebooted. These aren’t necessarily times when the system has used the
reboot command or that you’ve rebooted from your desktop. Instead, it logs every time your system booted.
If you’d prefer a more concise version, only showing the last time your computer booted, you can pipe the output to
head and supply it with
-1, telling it to only output one line. If you’d prefer the boot prior to your current one, use
-2 to get both lines.
last command works similarly with shutdowns. These are times that your computer shut down completely. They provide a time range when the computer was off. You can line these up with the reboots to know which shutdown corresponds.
Like with the reboots before, you can pipe the output to
head to get the last shutdown only. Also like before, you can supply a different number, like
-3 to get the last three shutdowns.
Finally, when you want to know how long your computer has been running, you can use the
uptime command to find out. Combine it with the
-p flag to get much more easily readable output. You’ll get the amount of time in days, hours, and minutes that your computer has been on since the last boot.
Hopefully, with the above commands, you’ll be able to figure out a pattern, or even the cause, behind your computer’s reboots and shutdowns. If other programs are involved, you can always check for specific log files in “/var/log.”