Mapping a range of IP's to a domain name in hosts file












1














I work with Wi-Fi. Due to that my system's IP changes every time I connect to the network. For my work, I need to map my IP to a specific domain name. So, every day I need to make changes in my hosts file to update the IP. This is really annoying and I hope there must be a workaround.So, I need to add the range of IP's to that domain name in my hosts file. Is there a way to do it.










share|improve this question







New contributor




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

























    1














    I work with Wi-Fi. Due to that my system's IP changes every time I connect to the network. For my work, I need to map my IP to a specific domain name. So, every day I need to make changes in my hosts file to update the IP. This is really annoying and I hope there must be a workaround.So, I need to add the range of IP's to that domain name in my hosts file. Is there a way to do it.










    share|improve this question







    New contributor




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























      1












      1








      1


      1





      I work with Wi-Fi. Due to that my system's IP changes every time I connect to the network. For my work, I need to map my IP to a specific domain name. So, every day I need to make changes in my hosts file to update the IP. This is really annoying and I hope there must be a workaround.So, I need to add the range of IP's to that domain name in my hosts file. Is there a way to do it.










      share|improve this question







      New contributor




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











      I work with Wi-Fi. Due to that my system's IP changes every time I connect to the network. For my work, I need to map my IP to a specific domain name. So, every day I need to make changes in my hosts file to update the IP. This is really annoying and I hope there must be a workaround.So, I need to add the range of IP's to that domain name in my hosts file. Is there a way to do it.







      ubuntu networking osx dns hosts






      share|improve this question







      New contributor




      Khandekar Mohammad Saleh 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 question







      New contributor




      Khandekar Mohammad Saleh 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 question




      share|improve this question






      New contributor




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









      asked yesterday









      Khandekar Mohammad Saleh

      61




      61




      New contributor




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





      New contributor





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






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






















          3 Answers
          3






          active

          oldest

          votes


















          0














          I would either write a script that loops through a list of IP addresses, adding each on a single line in your host file - as ranges are not supported, or - which is better in my opinion - host your web application on a virtual machine with a NAT or host-only adapter so it has a fixed IP address.






          share|improve this answer





























            0














            Most likely, you don't actually need to put your DHCP-assigned IP address in the hosts file. E.g., you probably want to be able to point a browser at http://your-domain-here/ and have your local web server respond to that. (Same with any other client/server setup). In which case — just map the domain to a localhost IP, which is any IP starting with 127. (I'd avoid 127.0.0.1 as that might give weird answers for reverse lookups). Just pick a new one that isn't already in your hosts file.



            Another option is just to pick a static RFC1918 space IP address (192.168.x.x, 10.x.x.x, 172.16–31.x.x) and add it to a local interface, then just use that IP address.



            If you really do need have it mapped to your current IP address, there is typically somewhere you can put a shell script to run when your machine gets an IP address via DHCP. If you're using dhclient, you can put place a script in /etc/dhcp/dhclient-enter-hooks.d/. There is a dhclient-script(8) manual page that documents it, but something like this (very untested) would probably work (it uses "sponge" from the moreutils package):



            #!/bin/sh

            MY_DOMAIN=your-domain.com

            if [ "$reason" != "BOUND" ] && [ "$reason" != "REBIND" ]; then
            exit 0
            fi

            grep -v " $MY_DOMAIN$" /etc/hosts | sponge /etc/hosts # really should escape the dots in the domain, but probably doesn't matter.
            printf '%s %sn' "$new_ip_address" "$MY_DOMAIN" >> /etc/hosts


            That's very untested. Make sure to have a backup of /etc/hosts before trying that!






            share|improve this answer





























              0














              You write,




              I work with Wi-Fi. Due to that my system's IP changes every time I connect to the network.




              These two statements should not be connected. You IP address can be assigned manually to fixed value ("static" address) or it can be assigned dynamically by the DHCP server on the network ("dynamic" address). It doesn't matter whether your system connects with WiFi or with a cable; these are your two options for getting an IP address.



              To add interest, the DHCP server might be configured to give you the same IP address each time you connect to its network (informally sometimes called a "sticky" address). This is how I have my home network set up.



              You cannot (usually) use a static IP address if you are roaming across different networks, so a sticky address might be your best approach.



              On the other hand, if all you need to do is to get a host entry in your domain to point to your client system you might want to look up Dynamic DNS providers. One example is dyn.com but there are plenty of others to consider. You install a small piece of software on your client that captures any change in IP address, and notifies the central server. It updates your domain entry for you. The final piece of the puzzle is that you replace the A record in your real domain with a CNAME that points to the DDNS domain entry that points to your client system.






              share|improve this answer





















                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',
                autoActivateHeartbeat: false,
                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
                });


                }
                });






                Khandekar Mohammad Saleh is a new contributor. Be nice, and check out our Code of Conduct.










                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f490262%2fmapping-a-range-of-ips-to-a-domain-name-in-hosts-file%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                0














                I would either write a script that loops through a list of IP addresses, adding each on a single line in your host file - as ranges are not supported, or - which is better in my opinion - host your web application on a virtual machine with a NAT or host-only adapter so it has a fixed IP address.






                share|improve this answer


























                  0














                  I would either write a script that loops through a list of IP addresses, adding each on a single line in your host file - as ranges are not supported, or - which is better in my opinion - host your web application on a virtual machine with a NAT or host-only adapter so it has a fixed IP address.






                  share|improve this answer
























                    0












                    0








                    0






                    I would either write a script that loops through a list of IP addresses, adding each on a single line in your host file - as ranges are not supported, or - which is better in my opinion - host your web application on a virtual machine with a NAT or host-only adapter so it has a fixed IP address.






                    share|improve this answer












                    I would either write a script that loops through a list of IP addresses, adding each on a single line in your host file - as ranges are not supported, or - which is better in my opinion - host your web application on a virtual machine with a NAT or host-only adapter so it has a fixed IP address.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered yesterday









                    Tom

                    1217




                    1217

























                        0














                        Most likely, you don't actually need to put your DHCP-assigned IP address in the hosts file. E.g., you probably want to be able to point a browser at http://your-domain-here/ and have your local web server respond to that. (Same with any other client/server setup). In which case — just map the domain to a localhost IP, which is any IP starting with 127. (I'd avoid 127.0.0.1 as that might give weird answers for reverse lookups). Just pick a new one that isn't already in your hosts file.



                        Another option is just to pick a static RFC1918 space IP address (192.168.x.x, 10.x.x.x, 172.16–31.x.x) and add it to a local interface, then just use that IP address.



                        If you really do need have it mapped to your current IP address, there is typically somewhere you can put a shell script to run when your machine gets an IP address via DHCP. If you're using dhclient, you can put place a script in /etc/dhcp/dhclient-enter-hooks.d/. There is a dhclient-script(8) manual page that documents it, but something like this (very untested) would probably work (it uses "sponge" from the moreutils package):



                        #!/bin/sh

                        MY_DOMAIN=your-domain.com

                        if [ "$reason" != "BOUND" ] && [ "$reason" != "REBIND" ]; then
                        exit 0
                        fi

                        grep -v " $MY_DOMAIN$" /etc/hosts | sponge /etc/hosts # really should escape the dots in the domain, but probably doesn't matter.
                        printf '%s %sn' "$new_ip_address" "$MY_DOMAIN" >> /etc/hosts


                        That's very untested. Make sure to have a backup of /etc/hosts before trying that!






                        share|improve this answer


























                          0














                          Most likely, you don't actually need to put your DHCP-assigned IP address in the hosts file. E.g., you probably want to be able to point a browser at http://your-domain-here/ and have your local web server respond to that. (Same with any other client/server setup). In which case — just map the domain to a localhost IP, which is any IP starting with 127. (I'd avoid 127.0.0.1 as that might give weird answers for reverse lookups). Just pick a new one that isn't already in your hosts file.



                          Another option is just to pick a static RFC1918 space IP address (192.168.x.x, 10.x.x.x, 172.16–31.x.x) and add it to a local interface, then just use that IP address.



                          If you really do need have it mapped to your current IP address, there is typically somewhere you can put a shell script to run when your machine gets an IP address via DHCP. If you're using dhclient, you can put place a script in /etc/dhcp/dhclient-enter-hooks.d/. There is a dhclient-script(8) manual page that documents it, but something like this (very untested) would probably work (it uses "sponge" from the moreutils package):



                          #!/bin/sh

                          MY_DOMAIN=your-domain.com

                          if [ "$reason" != "BOUND" ] && [ "$reason" != "REBIND" ]; then
                          exit 0
                          fi

                          grep -v " $MY_DOMAIN$" /etc/hosts | sponge /etc/hosts # really should escape the dots in the domain, but probably doesn't matter.
                          printf '%s %sn' "$new_ip_address" "$MY_DOMAIN" >> /etc/hosts


                          That's very untested. Make sure to have a backup of /etc/hosts before trying that!






                          share|improve this answer
























                            0












                            0








                            0






                            Most likely, you don't actually need to put your DHCP-assigned IP address in the hosts file. E.g., you probably want to be able to point a browser at http://your-domain-here/ and have your local web server respond to that. (Same with any other client/server setup). In which case — just map the domain to a localhost IP, which is any IP starting with 127. (I'd avoid 127.0.0.1 as that might give weird answers for reverse lookups). Just pick a new one that isn't already in your hosts file.



                            Another option is just to pick a static RFC1918 space IP address (192.168.x.x, 10.x.x.x, 172.16–31.x.x) and add it to a local interface, then just use that IP address.



                            If you really do need have it mapped to your current IP address, there is typically somewhere you can put a shell script to run when your machine gets an IP address via DHCP. If you're using dhclient, you can put place a script in /etc/dhcp/dhclient-enter-hooks.d/. There is a dhclient-script(8) manual page that documents it, but something like this (very untested) would probably work (it uses "sponge" from the moreutils package):



                            #!/bin/sh

                            MY_DOMAIN=your-domain.com

                            if [ "$reason" != "BOUND" ] && [ "$reason" != "REBIND" ]; then
                            exit 0
                            fi

                            grep -v " $MY_DOMAIN$" /etc/hosts | sponge /etc/hosts # really should escape the dots in the domain, but probably doesn't matter.
                            printf '%s %sn' "$new_ip_address" "$MY_DOMAIN" >> /etc/hosts


                            That's very untested. Make sure to have a backup of /etc/hosts before trying that!






                            share|improve this answer












                            Most likely, you don't actually need to put your DHCP-assigned IP address in the hosts file. E.g., you probably want to be able to point a browser at http://your-domain-here/ and have your local web server respond to that. (Same with any other client/server setup). In which case — just map the domain to a localhost IP, which is any IP starting with 127. (I'd avoid 127.0.0.1 as that might give weird answers for reverse lookups). Just pick a new one that isn't already in your hosts file.



                            Another option is just to pick a static RFC1918 space IP address (192.168.x.x, 10.x.x.x, 172.16–31.x.x) and add it to a local interface, then just use that IP address.



                            If you really do need have it mapped to your current IP address, there is typically somewhere you can put a shell script to run when your machine gets an IP address via DHCP. If you're using dhclient, you can put place a script in /etc/dhcp/dhclient-enter-hooks.d/. There is a dhclient-script(8) manual page that documents it, but something like this (very untested) would probably work (it uses "sponge" from the moreutils package):



                            #!/bin/sh

                            MY_DOMAIN=your-domain.com

                            if [ "$reason" != "BOUND" ] && [ "$reason" != "REBIND" ]; then
                            exit 0
                            fi

                            grep -v " $MY_DOMAIN$" /etc/hosts | sponge /etc/hosts # really should escape the dots in the domain, but probably doesn't matter.
                            printf '%s %sn' "$new_ip_address" "$MY_DOMAIN" >> /etc/hosts


                            That's very untested. Make sure to have a backup of /etc/hosts before trying that!







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 22 hours ago









                            derobert

                            71.7k8152210




                            71.7k8152210























                                0














                                You write,




                                I work with Wi-Fi. Due to that my system's IP changes every time I connect to the network.




                                These two statements should not be connected. You IP address can be assigned manually to fixed value ("static" address) or it can be assigned dynamically by the DHCP server on the network ("dynamic" address). It doesn't matter whether your system connects with WiFi or with a cable; these are your two options for getting an IP address.



                                To add interest, the DHCP server might be configured to give you the same IP address each time you connect to its network (informally sometimes called a "sticky" address). This is how I have my home network set up.



                                You cannot (usually) use a static IP address if you are roaming across different networks, so a sticky address might be your best approach.



                                On the other hand, if all you need to do is to get a host entry in your domain to point to your client system you might want to look up Dynamic DNS providers. One example is dyn.com but there are plenty of others to consider. You install a small piece of software on your client that captures any change in IP address, and notifies the central server. It updates your domain entry for you. The final piece of the puzzle is that you replace the A record in your real domain with a CNAME that points to the DDNS domain entry that points to your client system.






                                share|improve this answer


























                                  0














                                  You write,




                                  I work with Wi-Fi. Due to that my system's IP changes every time I connect to the network.




                                  These two statements should not be connected. You IP address can be assigned manually to fixed value ("static" address) or it can be assigned dynamically by the DHCP server on the network ("dynamic" address). It doesn't matter whether your system connects with WiFi or with a cable; these are your two options for getting an IP address.



                                  To add interest, the DHCP server might be configured to give you the same IP address each time you connect to its network (informally sometimes called a "sticky" address). This is how I have my home network set up.



                                  You cannot (usually) use a static IP address if you are roaming across different networks, so a sticky address might be your best approach.



                                  On the other hand, if all you need to do is to get a host entry in your domain to point to your client system you might want to look up Dynamic DNS providers. One example is dyn.com but there are plenty of others to consider. You install a small piece of software on your client that captures any change in IP address, and notifies the central server. It updates your domain entry for you. The final piece of the puzzle is that you replace the A record in your real domain with a CNAME that points to the DDNS domain entry that points to your client system.






                                  share|improve this answer
























                                    0












                                    0








                                    0






                                    You write,




                                    I work with Wi-Fi. Due to that my system's IP changes every time I connect to the network.




                                    These two statements should not be connected. You IP address can be assigned manually to fixed value ("static" address) or it can be assigned dynamically by the DHCP server on the network ("dynamic" address). It doesn't matter whether your system connects with WiFi or with a cable; these are your two options for getting an IP address.



                                    To add interest, the DHCP server might be configured to give you the same IP address each time you connect to its network (informally sometimes called a "sticky" address). This is how I have my home network set up.



                                    You cannot (usually) use a static IP address if you are roaming across different networks, so a sticky address might be your best approach.



                                    On the other hand, if all you need to do is to get a host entry in your domain to point to your client system you might want to look up Dynamic DNS providers. One example is dyn.com but there are plenty of others to consider. You install a small piece of software on your client that captures any change in IP address, and notifies the central server. It updates your domain entry for you. The final piece of the puzzle is that you replace the A record in your real domain with a CNAME that points to the DDNS domain entry that points to your client system.






                                    share|improve this answer












                                    You write,




                                    I work with Wi-Fi. Due to that my system's IP changes every time I connect to the network.




                                    These two statements should not be connected. You IP address can be assigned manually to fixed value ("static" address) or it can be assigned dynamically by the DHCP server on the network ("dynamic" address). It doesn't matter whether your system connects with WiFi or with a cable; these are your two options for getting an IP address.



                                    To add interest, the DHCP server might be configured to give you the same IP address each time you connect to its network (informally sometimes called a "sticky" address). This is how I have my home network set up.



                                    You cannot (usually) use a static IP address if you are roaming across different networks, so a sticky address might be your best approach.



                                    On the other hand, if all you need to do is to get a host entry in your domain to point to your client system you might want to look up Dynamic DNS providers. One example is dyn.com but there are plenty of others to consider. You install a small piece of software on your client that captures any change in IP address, and notifies the central server. It updates your domain entry for you. The final piece of the puzzle is that you replace the A record in your real domain with a CNAME that points to the DDNS domain entry that points to your client system.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered 22 hours ago









                                    roaima

                                    42.7k551116




                                    42.7k551116






















                                        Khandekar Mohammad Saleh is a new contributor. Be nice, and check out our Code of Conduct.










                                        draft saved

                                        draft discarded


















                                        Khandekar Mohammad Saleh is a new contributor. Be nice, and check out our Code of Conduct.













                                        Khandekar Mohammad Saleh is a new contributor. Be nice, and check out our Code of Conduct.












                                        Khandekar Mohammad Saleh is a new contributor. Be nice, and check out our Code of Conduct.
















                                        Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f490262%2fmapping-a-range-of-ips-to-a-domain-name-in-hosts-file%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

                                        サソリ

                                        広島県道265号伴広島線

                                        Accessing regular linux commands in Huawei's Dopra Linux