Maintaining `/etc/hosts` for hosts on DHCP
Solution 1
It depends on what is doing the DHCP?
Most home routers use dnsmasq and you can use that as your local DNS server. You just need to set dnsmasq to return itself as the DNS server. Next, you need to make sure that your PCs broadcast a hostname during the DHCP request.
Then, voila, you should be able to resolve all your local machines through the DNS/DHCP server.
Solution 2
Depending on what you want to accomplish, you could set up avahi, which can broadcast the existence of a sshd service on your machine. Be careful in 'foreign' networks though...
Solution 3
I have often wondered about this and I tried creating a script like this in /etc/sysconfig/network-scripts/update-hosts:-
#!/bin/sh
set -e
if [ "$IFACE" = lo ]; then
exit 0
fi
SHORT_HOST=`hostname`
# Remove current line with hostname at the end of line
sed -i '/'$SHORT_HOST'$/ d' /etc/hosts
ipaddr=$(ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')
echo "$ipaddr $SHORT_HOST" >>/etc/hosts
I would then have it run via an init script /etc/init.d/updatehosts :-
#!/bin/sh
# chkconfig: 2345 11 89
# description: automatically update /etc/hosts
#
if [ ! -x /etc/sysconfig/network-scripts/update-hosts ]
then
echo "Update hosts: can't update hosts file"
exit
fi
case "$1" in
'start')
# Update hosts:
cp /etc/hosts /etc/hosts.001
/etc/sysconfig/network-scripts/update-hosts
echo "/etc/hosts updated"
;;
'stop')
# Restore hosts:
cp /etc/hosts.001 /etc/hosts
echo "/etc/hosts restored"
;;
esac
enabled using chkconfig --add updatehosts
Can anyone recommend a better way or improvements to this method?
Related videos on Youtube
Comments
-
Stefan almost 2 years
On my local DHCP network I have different PC's that I need to access remotely. Problem is that their IP's change. Sometimes I plug my laptop and netbook into other people's DHCP networks.
My current solution is to update the
/etc/hosts
file every time the target IP's change.My
/etc/hosts
file looks like this:# <ip-address> <hostname.domain.org> <hostname> 127.0.0.1 localhost.localdomain localhost laptop 192.168.1.14 desktop.localdomain desktop 192.168.1.12 netbook.localdomain netbook
Is there a way to bypass all that manual administration?
For example, could my computers broadcast their IPs on the LAN, or something like that? Windows does something like that, which allows you to reference a computer on the network with "\\COMPUTER_NAME"
-
Admin over 13 yearsI believe that getting the DHCP server to agree with the DNS server is the only way to do this without having the DHCP clients update DNS somehow. I have dhcpd configured to give out particular IP address to specific ethernet addresses. Those IP addresses are in the hosts file used by dnsmasp.
-
John Atwood over 13 yearsThat is another way to do it, by distributing fixed IPs over dhcp. That means that the clients would not need to be configured individually.
-
Stefan over 13 yearsI do not have admin powers over the router(s).
-
Steven D over 13 yearsEven beyond broadcasting the existence of sshd, avahi will very easily provide you with up-to-date hostname resolution.