To automatically update the local host's IP entry in
/etc/hosts
put this script under
/etc/network/if-up.d
:
#!/bin/bash
# Update /etc/host only if eth0 or eth1 changes
[ "$IFACE" != "eth0" ] || [ "$IFACE" != "eth1" ] || exit 0
HOSTNAME=$(< /etc/hostname)
IPADDRESS=$(ip -o -4 addr show dev $IFACE scope global primary \
| sed -n 's/.*inet\ \([^\/]*\)\/.*/\1/p' \
| sed -n 's/\([0-9]\{1,3\}\.\)\{1,3\}[0-9]\{1,3\}/\0/p')
if [ -n "$IPADDRESS" ]; then
sed -i 's/\(^[1-9]\+\S\+\)\(\s\+\(\S+\s+\)*'$HOSTNAME'\(\S+\s*\)*\)/'$IPADDRESS'\2/g' /etc/hosts
fi
Make sure that your
/etc/hosts
contains an entry like:
10.1.208.31 myhost myhost.mydomain.org
No comments:
Post a Comment