I am connected to Internet through an ADSL modem, my ISP gives me an IP address, does Ubuntu log this IP address somewhere?
How can I configure Ubuntu to log the IP address?
I am connected to Internet through an ADSL modem, my ISP gives me an IP address, does Ubuntu log this IP address somewhere?
How can I configure Ubuntu to log the IP address?
If your modem is purely a modem and your PC connects to your ISP directly, then the IP address that it obtains will be written to the system logs in /var/log/syslog. If you want this information to be easier to extract, you can log to a separate file.
If your ISP provides a PPP connection, then the scripts in /etc/ppp/ip-up.d are executed each time you connect (or /etc/ppp/ipv6-up.d if you have IPv6 connectivity). Add a script called /etc/ppp/ip-up.d/zzz_log_ip_address:
#!/bin/sh
echo "$PPP_LOCAL $(TZ=UTC date '+%Y-%m-%d %H-%M-%S')" >>/var/log/ip-addresses
If your ISP provides an Ethernet connection, then the scripts in /etc/network/if-up.d are executed each time you connect. Add a script called /etc/network/if-up.d/zzz_log_ip_address:
#!/bin/sh
if [ "$IFACE" = "lo" ]; then exit; fi # skip loopback interface
address=$(ip addr show eth1 | awk '$1 == "inet" {sub("/.*", "", $2); print $2}')
echo "$address $(TZ=UTC date '+%Y-%m-%d %H-%M-%S')" >>/var/log/ip-addresses
The address of your PC on the local network may not be the address that you reach the Internet with, due to NAT. NAT allows many machines on a network to make outgoing connections from a single IP address. Many ADSL modems are in fact routers and NAT appliances in addition to being a modem. If your modem offers wifi, it's almost certainly a NAT appliance. If your modem allows multiple computers to connect (without one of the computers being set up for connection sharing), it is a NAT appliance.
If your PC is behind a NAT, then the only way it can know what its Internet address is is to ask a remote machine. There are many publicly-available websites that provide this information. You can retrieve this information at any given time, but you will not know when it changes. Most ISPs will change your IP address from time to time because this facilitates load balancing in their equipment. This typically happens once a day or once every few days.
If you want to log your IP address at regular intervals, you can make a cron job. Note that if your address changes multiple times between runs of the job, you have no way to know. In your crontab:
0 * * * * $HOME/bin/log-ip-address
The script ~/bin/log-ip-address:
#!/bin/sh
log_file=~/.ip-addresses.log
address=$(wget -q -O - http://ipecho.net/plain)
previous=$(awk 'END {print $1}' <"$log_file") 2>/dev/null
if [ "$address" != "$previous" ]; then
echo "$address $(TZ=UTC date '+%Y-%m-%d %H-%M-%S')" >>"$log_file"
fi
Depending on your router model, it may be logging the IP addresses that it obtains, and there may be a way to make it send this information to your PC. This is very dependent on the router model, I can't give any general advice.
Or you can do it yourself, using curl to access a number of online services that respond with your public IP address.
Some of the services:
Example:
curl icanhazip.com will output to terminal
curl icanhazip.com > my_public_ip.txt will create or overide a txt file named my_public_ip.txt with the IP.
curl icanhazip.com >> my_public_ip.txt will append the public IP every time the command runs.
You could create a small script to run the check and even add it as cron job to run say every time the machine reboots or at some intervals.
The simplest version could be:
#!/usr/bin/env bash
curl icanhazip.com >> my_public_ip.txt