I have a Server on my house to proves and when I'm on my laptop in my house I have to write the local IP to see the web server, and i want to know if it is possible write a script to automatically detect the network and add the line on hosts file, and when is in other network delete this line.


  • 1
    Does the server have a fixed IP address? Why don't you just add the line into /etc/hosts? Why do you want to have this line removed when you are on another network? – user205569 Oct 21 '13 at 10:54

Perhaps the trigger of the custom hosts file entry can be NetworkManager when it connects to your home access point.


A better solution than manually skwabbing a file would be to have an internal DNS server. You could add your internal records to this. There are two ways in which you could make this work:

  • For your entire network: Install pdnsd (a is a good simple caching DNS server) on your server, set it up with an external DNS provider to cache through, and then point your router at your internal server's IP for DNS. All devices connected to the network will have their DNS requests passed through your internal server.

    In my experience, this route isn't possible with Home/SME Netgear routers as they blow up if you try and set an internal IP as the DNS route. Stupid, unneccessary limitation on otherwise capable hardware.

  • Just your laptop: As above except you install and configure pdnsd on your laptop (it's not very heavy) and alter your home connection settings to use for DNS.


You actually might already be running a solution:


If your server is named 'my-server', try this command from the machine that isn't the server:

$ ping my-server.local

If you succeed, then you're running avahi on your machines and they're doing "zeroconf".

There may be other reasons why you want to run a local dns server, such as caching and so forth. A zeroconf approach doesn't address those needs.


You can even use the router DNS server and add an host entry for the server and point the primary DNS of your laptop to the router.

DNS is basically the replacement of keeping host records on individual computers.

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.