Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 upate the /etc/hosts file everytime 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 admin? :)

For example, could my computers broadcast their IPs on the LAN? or something like that. Windows does something like that, which allows you to refference a computer on the network with "\\COMPUTER_NAME"

share|improve this question
up vote 5 down vote accepted

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.

share|improve this answer
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. – Bruce Ediger Dec 26 '10 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 Dec 28 '10 at 12:14
I do not have admin powers over the router(s). – Stefan Dec 31 '10 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...

share|improve this answer
Even beyond broadcasting the existence of sshd, avahi will very easily provide you with up-to-date hostname resolution. – Steven D Dec 31 '10 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?

share|improve this answer

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.