I asked this question on how to see the Linux DNS cache. It looks like it's possible but it doesn't show TTL, unlike those for Firefox, Chrome, and Windows.
On windows, this looks like
PS C:\> ipconfig /displaydns
Windows IP Configuration
chrome.cloudflare-dns.com
----------------------------------------
Record Name . . . . . : chrome.cloudflare-dns.com
Record Type . . . . . : 1
Time To Live . . . . : 54
Data Length . . . . . : 4
Section . . . . . . . : Answer
A (Host) Record . . . : 104.18.27.211
vortex.data.microsoft.com
----------------------------------------
Record Name . . . . . : vortex.data.microsoft.com
Record Type . . . . . : 5
Time To Live . . . . : 6
Data Length . . . . . : 8
Section . . . . . . . : Answer
CNAME Record . . . . : asimov.vortex.data.trafficmanager.net
...
On linux, it's possible to dump the cache and look at it with journalctl:
rj@vps:~$ time=$(date "+%F %T")
rj@vps:~$ systemctl kill -s USR1 systemd-resolved
rj@vps:~$ journalctl -b -0 --since "$time" -u systemd-resolved | grep " IN "
Oct 10 22:28:38 myserver systemd-resolved[3255524]: cloudflare.com IN A 104.16.133.229
Oct 10 22:28:38 myserver systemd-resolved[3255524]: cloudflare.com IN A 104.16.132.229
Question
How do you get the TTL of a record in the DNS cache on Linux using systemd?
Edit:
Based on user1686's answer, this script will return TTL and 0 if it's at 0 or not in cache:
get-ttl () {
site="$1"
time=$(date "+%F %T")
systemctl kill -s USR1 systemd-resolved
dns_cache=$(journalctl -b -0 --since "$time" -u systemd-resolved \
| grep " IN ")
site_cache="$(echo $dns_cache | grep $site)"
if [ "$site_cache" ]
then dig +noall +answer $site A | awk '{ print $2 }'
else echo 0
fi
}
You can then use this to find TTL in cache and out of cache:
$ get-ttl motel6.com
3349
$ get-ttl motels.com
0