32

I have a domain setup to point to my LAN's external IP using dynamic DNS, because my external IP address changes frequently. However, I want to create an alias to this host, so I can access it with home. So I appended the following to my /etc/hosts:

example.com home

However, it doesn’t seem to like the domain name. If I change it to an IP:

0.0.0.0 home

then it works, but of course this defeats the purpose of dynamic DNS!

Is this possible?

2
  • Why does your external IP change frequently? Is this your provider internet-IP that gets assigned to you from a pool?
    – Nils
    Dec 19, 2012 at 20:39
  • Yes, it is. I don't know why, my ISP just seem to change it sometimes! Dec 19, 2012 at 20:43

4 Answers 4

33

The file /etc/hosts contains IP addresses and host names only. You cannot alias the string "home" in the way that you want by this method.

If you were running your own DNS server you'd be able to add a CNAME record to make home.example.com an alias for domain.example, but otherwise you're out of luck.

The best thing you could do is use the same DNS client to update a fully-qualified name.

0
2

As stated, /etc/hosts was not intended to provide alias (CNAME) ability for name to IP lookups. While setting up a proper DNS server and configuring the CNAME there would definitely be an always-accurate way to resolve home to the IP of example.com, it is a bit more involving than a simple alias.

The change itself can be automated quite easily to a single line within a script:

sudo sed -i "s/.* home/$(dig +short domain.com|tail -1) home/" /etc/hosts

If you already have a means of detecting an IP change, this could easily be appended to that process. Otherwise, a brute force method of just scheduling an overwrite every x period could be set up with a simple cron job.

References:

0

This looks to me as if you should register to DYNDNS or something like that.

If you really want to use /etc/hosts you have to monitor your IP. As soon as it changes:

  • Change the entry in /etc/hosts
  • Reload the hosts-cache
  • Keep monitoring

Update If you are using dhcpcd you can use the "-c" option for a script that changes your /etc/hosts-entry an then reloads the host-cache.

This depends on the capabilities of your dhcp-client.

6
  • I’ve registered to no-ip, which provides a similar service to DYNDNS I believe (I couldn't see to sign up on the DYNDNS homepage!) Dec 19, 2012 at 20:47
  • 1
    @OliverJosephAsh so you can use the name you registered on no-ip! I do not see your reason to do this via /etc/hosts.
    – Nils
    Dec 19, 2012 at 20:58
  • Quite simply it is because I’m lazy! Typing home is quite a few keystrokes less than oliverjash.no-ip.org Dec 19, 2012 at 22:48
  • have you thought of aliases within you shell, or setting your domain search list in /etc/resolv.conf?
    – mdpc
    Dec 20, 2012 at 3:17
  • @mdpc I did not ask the question. But an alias within the shell will propably not work within the browser. He could use a local proxy with a rewrite-rule, though...
    – Nils
    Dec 20, 2012 at 21:46
0

The easiest way to handle the situation you describe is to set your DNS Search Domain to domain.com.

Also, create a DNS Record so that home.domain.com points to domain.com.

Your home router should allow you to set the DHCP Search Domain. Most mobile devices also allow for this.

Then you'll be able to directly:

ping home

And it should ping home.domain.com.

The exact way that you set the Search Domain will very slightly by device, but almost every device will allow you to set the search domain...

Then you don't have to limit yourself to home... You'll also be pinging printer, phone and maybe even fridge without bothering with fully qualified domain names...

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.