IP address 0.0.0.0

The IP address 0.0.0.0 is often used to mean “all a machine’s IP addresses” or “all network interfaces”. If you configure a server to listen to 0.0.0.0 it listens on everything. If you specify 0.0.0.0 as some sort of IP restricting filter it usually applies to all addresses.

But what does 0.0.0.0 mean to a client? Surprisingly, connecting to http://0.0.0.0/ seems to work on both my Macs and Linux. On my Mac “route get 0.0.0.0” shows me it’s going to use my Ethernet interface, with a real IP address. On my Linux box “ip route get 0.0.0.0” shows it using the loopback on 127.0.0.1.

Looking at this because I need a shell script that prints out an IP address that is likely to work even if I’m remotely shelled in to something. Here’s a script I use to launch test web servers for webapp development:

function pws {
  PORT="$1"
  [ -z "$PORT" ] && PORT=$[RANDOM%1000 + 9000]
  IP=`ifconfig | awk '/inet / { if ($2 != "127.0.0.1") { print $2; exit 0; } }'`
  python -m CGIHTTPServer "$PORT" >> /tmp/pws-"$PORT".log 2>&1 &
  echo "http://$IP:$PORT/"
}