Linux systems maintain several log files that record package management activities. The most relevant for tracking apt-get update
executions are:
/var/log/apt/history.log
/var/log/apt/term.log
/var/log/dpkg.log
The most reliable way to check the last update timestamp is through the apt history log:
grep "update" /var/log/apt/history.log | tail -n 1
This will output something like:
Start-Date: 2023-08-15 09:25:42
Commandline: apt-get update
Requested-By: root (1000)
Another approach is to check when package lists were last refreshed:
ls -lt /var/lib/apt/lists/ | head -n 5
Example output showing the most recently modified files:
-rw-r--r-- 1 root root 14280 Aug 15 09:25 Packages
-rw-r--r-- 1 root root 2957 Aug 15 09:25 Release
-rw-r--r-- 1 root root 1045 Aug 15 09:25 Sources
For systems using systemd, you can query the journal:
journalctl -u apt-daily.service | grep "finished" | tail -n 1
Here's a bash script to check and notify about stale updates:
#!/bin/bash
LAST_UPDATE=$(stat -c %Y /var/lib/apt/periodic/update-success-stamp)
NOW=$(date +%s)
DAYS_SINCE_UPDATE=$(( (NOW - LAST_UPDATE) / 86400 ))
if [ "$DAYS_SINCE_UPDATE" -gt 7 ]; then
echo "Warning: Last apt update was $DAYS_SINCE_UPDATE days ago"
else
echo "System is up to date (last updated $DAYS_SINCE_UPDATE days ago)"
fi
For comprehensive tracking, combine multiple log sources:
{
echo "=== History Log ==="
grep "update" /var/log/apt/history.log | tail -n 3
echo -e "\n=== Package Lists ==="
ls -lt /var/lib/apt/lists/ | head -n 3
echo -e "\n=== Systemd Journal ==="
journalctl -u apt-daily.service --since "1 month ago" | grep -i "update" | tail -n 3
} | less
When managing Debian-based Linux servers, knowing when package repositories were last updated is crucial for security patches and version control. The apt-get update
command refreshes the local package index but doesn't store its execution timestamp in an obvious location.
The most reliable method examines system logs where apt operations are recorded:
grep "apt-get update" /var/log/apt/history.log
For more detailed timestamp information:
zgrep "apt-get update" /var/log/apt/history.log*
You can check when package lists were last modified:
stat -c %y /var/lib/apt/lists/
ls -lt /var/lib/apt/lists/ | head -n 5
Newer Ubuntu versions store operation history in JSON format:
jq -r '.history[] | select(.command == "update") | .start_date' /var/log/apt/history.log
For regular checks, consider this bash script:
#!/bin/bash
LAST_UPDATE=$(stat -c %Y /var/lib/apt/lists/)
CURRENT_TIME=$(date +%s)
DAYS_SINCE=$(( (CURRENT_TIME - LAST_UPDATE) / 86400 ))
if [ $DAYS_SINCE -gt 7 ]; then
echo "WARNING: apt-get update not run for $DAYS_SINCE days"
else
echo "Package lists updated $DAYS_SINCE day(s) ago"
fi
For systems using systemd, you can check when the apt-daily service last ran:
systemctl list-timers apt-daily.timer --all
journalctl -u apt-daily.service -n 10