Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

3 Answers 3

up vote 4 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:-

#!/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?

share|improve this answer

Your Answer

 
discard

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.