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.
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 %s\n' "$new_ip_address" "$MY_DOMAIN" >> /etc/hosts
That's very untested. Make sure to have a backup of
/etc/hosts before trying that!
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.
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.