Use reference to file instead of IP in /etc/hosts

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












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.










share|improve this question























  • 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 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 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 at 11:27










  • @Fabby Thank you so much to both.
    – BowPark
    Nov 22 at 12:31














up vote
1
down vote

favorite












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.










share|improve this question























  • 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 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 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 at 11:27










  • @Fabby Thank you so much to both.
    – BowPark
    Nov 22 at 12:31












up vote
1
down vote

favorite









up vote
1
down vote

favorite











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.










share|improve this question















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.







ubuntu networking ip hosts






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 10:50

























asked Nov 21 at 9:51









BowPark

1,52982445




1,52982445











  • 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 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 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 at 11:27










  • @Fabby Thank you so much to both.
    – BowPark
    Nov 22 at 12:31
















  • 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 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 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 at 11:27










  • @Fabby Thank you so much to both.
    – BowPark
    Nov 22 at 12:31















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 at 10:33





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 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 at 10:49




@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 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 at 11:27




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 at 11:27












@Fabby Thank you so much to both.
– BowPark
Nov 22 at 12:31




@Fabby Thank you so much to both.
– BowPark
Nov 22 at 12:31










2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted










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.






share|improve this answer






















  • Yup, exactly what I was going to say: +1
    – Fabby
    Nov 21 at 11:28










  • Nope... /etc/hosts @rAlen
    – Michael Prokopec
    Nov 21 at 12:40

















up vote
0
down vote













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.






share|improve this answer








New contributor




Michael Prokopec is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f483167%2fuse-reference-to-file-instead-of-ip-in-etc-hosts%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote



    accepted










    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.






    share|improve this answer






















    • Yup, exactly what I was going to say: +1
      – Fabby
      Nov 21 at 11:28










    • Nope... /etc/hosts @rAlen
      – Michael Prokopec
      Nov 21 at 12:40














    up vote
    4
    down vote



    accepted










    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.






    share|improve this answer






















    • Yup, exactly what I was going to say: +1
      – Fabby
      Nov 21 at 11:28










    • Nope... /etc/hosts @rAlen
      – Michael Prokopec
      Nov 21 at 12:40












    up vote
    4
    down vote



    accepted







    up vote
    4
    down vote



    accepted






    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.






    share|improve this answer














    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 at 11:08

























    answered Nov 21 at 11:03









    rAlen

    675410




    675410











    • Yup, exactly what I was going to say: +1
      – Fabby
      Nov 21 at 11:28










    • Nope... /etc/hosts @rAlen
      – Michael Prokopec
      Nov 21 at 12:40
















    • Yup, exactly what I was going to say: +1
      – Fabby
      Nov 21 at 11:28










    • Nope... /etc/hosts @rAlen
      – Michael Prokopec
      Nov 21 at 12:40















    Yup, exactly what I was going to say: +1
    – Fabby
    Nov 21 at 11:28




    Yup, exactly what I was going to say: +1
    – Fabby
    Nov 21 at 11:28












    Nope... /etc/hosts @rAlen
    – Michael Prokopec
    Nov 21 at 12:40




    Nope... /etc/hosts @rAlen
    – Michael Prokopec
    Nov 21 at 12:40












    up vote
    0
    down vote













    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.






    share|improve this answer








    New contributor




    Michael Prokopec is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      0
      down vote













      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.






      share|improve this answer








      New contributor




      Michael Prokopec is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



















        up vote
        0
        down vote










        up vote
        0
        down vote









        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.






        share|improve this answer








        New contributor




        Michael Prokopec is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        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.







        share|improve this answer








        New contributor




        Michael Prokopec is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        Michael Prokopec is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered Nov 21 at 13:04









        Michael Prokopec

        59315




        59315




        New contributor




        Michael Prokopec is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        Michael Prokopec is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        Michael Prokopec is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f483167%2fuse-reference-to-file-instead-of-ip-in-etc-hosts%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)