1

From my host with hostname localhost, I would like to reach an external host with hostname exthost through the internet. The public IP address of exthost is dynamically assigned by the Internet Provider.

I would like to refer to exthost using always its name, and mapping this name to its actual IP address, according to the value assigned by the Provider, which may change.

To do so, I would like to use /etc/hosts, properly updating the IP address value of the line related to exthost.

For example, the line in /etc/hosts in Linux may look like:

<ip_address>    exthost.domain     exthost

I am using Ubuntu 18.04, but this surely applies also to other distros/Unix-like systems.

Is it possible to do this, avoiding to set up a DNS (and also a dynamic DNS, which sometimes is not a free service) just for this purpose?

For example, is it possible to use, instead of an explicit <ip_address>, a reference to another file containing only the desired IP as a string? So that this file can be accessed and modified by a user, according to the IP value, which may vary.


Note: this question seems not to be the same case, because it is about the local machine. I am instead referring to an external host.

  • Could you describe what you're trying to accomplish at a higher level as the hosts file predates DNS and as the question now stands it looks like you're trying to build your own DNS system using said host file... Maybe Dynamic DNS is a solution, but we need to have more info. – Fabby Nov 21 '18 at 10:33
  • @Fabby I tried to better explain what I'm trying to do: question updated. Thanks! Yes, maybe it is a handcrafted, rudimentary DNS service. If /etc/hosts file is intended for static IP only, this could be not a proper use of it: I don't know the purpose of /etc/hosts very well, so this is part of the question. – BowPark Nov 21 '18 at 10:49
  • Well, rAlen gave the answer I was going to give: there are free Dynymic DNS services out there (depending on your router brand, your router manufacturer might even provide you such a service for free) – Fabby Nov 21 '18 at 11:27
4

There is no such thing as a user defined hosts file on Linux, you can use HOSTALIASES which works with canonical names.

If I got your question correctly you can use a dynamic DNS service like DynDNS or No-IP to always have a correct public IP address your ISP assigned.

You can then use HOSTSALIASES to map exthost to FQDN name provided by a dynamic DNS service.

Export HOSTALIASES value with export HOSTALIASES=~/.hosts and then add a following line to ~/.hosts file to map exthost to FDQN name provided by no-ip for example.

exthost yourname.no-ip.org

HOSTALIASES works only with cannonical names, not IPs, which is why you should use some dynamic DNS service to have FQDN name, but you can just use that FQDN, and skip the HOSTALIASES completely.

I don't think you can do this without setting up some DNS service, or scripting something on remote site to always send you their public IP and then change that IP in local /etc/hosts file.

Another option would be to either give user permission to change /etc/hosts or set that user in chroot environment, and give him his own /etc/hosts file in his chroot environment.

  • Yup, exactly what I was going to say: +1 – Fabby Nov 21 '18 at 11:28
  • Nope... /etc/hosts @rAlen – Michael Prokopec Nov 21 '18 at 12:40
0

My /etc/hosts:

127.0.0.1   localhost
127.0.1.1   *some-host*

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

So say:

sudo nano /etc/hosts

Add a line:

IPOFEXTHOST   exthost

Where IPOFEXTHOST is the known ip of exthost.

From that point on you could write a script to update the file by replacing the line containing exthost. No matter what though, one of the computers is going to have to have a dyndns or no-ip updater. So that either you know the web address of exthost directly or exthost can know your other computer's address to reverse ssh in and update the file with it's curl'ed public IP.

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.