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>        localhost.localdomain    localhost laptop     desktop.localdomain      desktop     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"

4 Answers 4


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.

  • I 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.
    – user732
    Commented Dec 26, 2010 at 19:08
  • That is another way to do it, by distributing fixed IPs over dhcp. That means that the clients would not need to be configured individually.
    – sybreon
    Commented Dec 28, 2010 at 12:14
  • I do not have admin powers over the router(s).
    – Stefan
    Commented Dec 31, 2010 at 8:35

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...

  • Even beyond broadcasting the existence of sshd, avahi will very easily provide you with up-to-date hostname resolution.
    – Steven D
    Commented Dec 31, 2010 at 20:07

I have often wondered about this and I tried creating a script like this in /etc/sysconfig/network-scripts/update-hosts:-


set -e

if [ "$IFACE" = lo ]; then
    exit 0


# Remove current line with hostname at the end of line
sed -i '/'$SHORT_HOST'$/ d' /etc/hosts

ipaddr=$(ifconfig  | grep 'inet addr:'| grep -v '' | 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 :-

# chkconfig: 2345 11 89
# description: automatically update /etc/hosts

if [ ! -x /etc/sysconfig/network-scripts/update-hosts ]
    echo "Update hosts: can't update hosts file"

case "$1" in
        # Update hosts:
        cp /etc/hosts /etc/hosts.001
                echo "/etc/hosts updated"
        # Restore hosts:
                cp /etc/hosts.001 /etc/hosts
                echo "/etc/hosts restored"

enabled using chkconfig --add updatehosts

Can anyone recommend a better way or improvements to this method?


You can use avahi daemon to bind your IP address to some resolvable host name each time when your IP is changed by DHCP.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .