7

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"

5

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

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 Dec 31 '10 at 20:07
0

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?

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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