Creating alias to domain name with /etc/hosts

76,896

Solution 1

The file /etc/hosts contains IP addresses and hostnames 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.com", 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.

Solution 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 domain.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:

Share:
76,896

Related videos on Youtube

Oliver Joseph Ash
Author by

Oliver Joseph Ash

Updated on September 18, 2022

Comments

  • Oliver Joseph Ash
    Oliver Joseph Ash over 1 year

    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:

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

    • Nils
      Nils over 11 years
      Why does your external IP change frequently? Is this your provider internet-IP that gets assigned to you from a pool?
    • Oliver Joseph Ash
      Oliver Joseph Ash over 11 years
      Yes, it is. I don't know why, my ISP just seem to change it sometimes!
  • Oliver Joseph Ash
    Oliver Joseph Ash over 11 years
    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!)
  • Nils
    Nils over 11 years
    @OliverJosephAsh so you can use the name you registered on no-ip! I do not see your reason to do this via /etc/hosts.
  • Oliver Joseph Ash
    Oliver Joseph Ash over 11 years
    Quite simply it is because I’m lazy! Typing home is quite a few keystrokes less than oliverjash.no-ip.org
  • text
    text over 11 years
    have you thought of aliases within you shell, or setting your domain search list in /etc/resolv.conf?
  • Nils
    Nils over 11 years
    @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
    Nils over 11 years
    @OliverJosephAsh I just checked the man-page of dhcpcd - there is probably a very simple solution there (did not test it).