40

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?

4 Answers4

36

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.

4

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:

Nab Eng
  • 51
1

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

erwin
  • 1,973
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.

Nils
  • 18,492
  • 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!) – Oliver Joseph Ash Dec 19 '12 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 '12 at 20:58
  • 1
    Quite simply it is because I’m lazy! Typing home is quite a few keystrokes less than oliverjash.no-ip.org – Oliver Joseph Ash Dec 19 '12 at 22:48
  • have you thought of aliases within you shell, or setting your domain search list in /etc/resolv.conf? – mdpc Dec 20 '12 at 03:17
  • 1
    @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 '12 at 21:46
  • @OliverJosephAsh I just checked the man-page of dhcpcd - there is probably a very simple solution there (did not test it). – Nils Dec 20 '12 at 21:57
  • @Nils can you elaborate about this a bit more? – piotao Nov 30 '22 at 17:03