Automatic update of IP address for server

I find that from time to time (I’m guessing after a power cycle or rest button press) my router reassigns ip address to all of my stuff. This doesn’t seem to bother printers or the machines running plain F35 Workstation, but one of my machines is running a website on a F35 Workstation with httpd, mariadb, php, etc. installed. The change in ip address means the server can’t serve anymore. Currently I just look at the address in GCC → Network then get it into the config file by doing the following:

# nano /var/www/html/sitename/wp-config.php
   Add or edit the following lines...
       define( 'WP_HOME','http://newip/sitename' ) ;
       define( 'WP_SITEURL', 'http://newip/sitename' ) ;

I want to automate this so I can just have a script that runs after a reboot and it’s all tanken care of.

I can get the ip address by running:

$ myip=`ip address | grep '192.168.1.255' | cut -d "/" -f1 | cut -d "t" -f2`

and $ echo $myip shows the current ip address, but I haven’t been able to figure out how to get the new ip address into the config file via a script.

First, is there a better way to get the ip address? Second, how can the script get the ip address into the config file? Third, is my approach wrong and I should do something else?

2 Likes

Set up a static DHCP lease on the router, or use a static IP on the server.
Also you can replace IP with domain name in the server configs if you properly configure DNS.

4 Likes

\ - deleted by author -

Vladislav is correct, but perhaps you or others reading this for advice need more explanation, so here’s a long explanation at a lower level.

Most routers for the home receive a single IP address assignment from the ISP, and then share this with the devices on your home network via “NAT”, Network Address Translation. They create a subnet for your network and dynamically assign IP addresses to your devices as needed. Your devices usually ask for their assignment via the DHCP protocol.
You can configure the router to assign a specific IP address each time (“static IP”) by determining the MAC address the device is using, and assigning the address (within your subnet) the router is to provide that device via DHCP.
There are various other ways to do this, such as setting up your own DHCP server to replace the router’s (I use dnsmasq to have my own DHCP and DNS for my local network). Or use Dynamic DNS, if you are providing a service that extends outside your local network.
But once you’ve made the IP address static, if you have a very small scope for your web server (a small number of devices access it), then you can edit /etc/hosts and manually provide the mapping of that IP address to a hostname. (For something involving less work in the long run but more work now, configure a local DNS server as I did.) Now you don’t have to edit your config file in /var/www/html/... as the hostname and IP are static.

Hope that’s useful to you and others with similar problems.

5 Likes

My thanks to all of you. I will need a while to study and understand this. I will reply again if I have follow up questions or to let everyone know what I did. I will try to figure out which is easiest to implement and maintain.

I have a single ip address coming in and the local router provides ip address for the devices in the building and those addresses are the sort not routed on the internet in the 192.168.1 group. I have between 6 and 10 devices (cellphones and pads are the variables) accessing the local router by a mix of wired and wifi. They all have access to the server. So it is a very limited audience.

1 Like

I agree with @vgaetera
The easiest way to set a ‘permanent’ IP for a machine on a network using dhcp is to configure a reserved address for that MAC address on the dhcp server. In your case the router is the dhcp server so setting it there will insure that machine always gets the same address so it acts like a static IP even though dhcp served. I have 3 different machines on my home network with reserved addresses, one of which is my printer.

2 Likes

It sounds like you are accessing this website on your local network only, not from an external network. You may rely on multicast DNS ( mDNS ) then.

Determine the hostname of your webserver:

$ hostname
thewebserver

Test:

$ curl thewebserver.local
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
...

Edit /var/www/html/sitename/wp-config.php

define( 'WP_HOME','http://thewebserver.local/sitename' ) ;
define( 'WP_SITEURL', 'http://thewebserver.local/sitename' ) ;

Should work.

You can browse what devices are discoverable through mDNS:

avahi-browse -alr # all devices
avahi-browse -d local _http._tcp --resolve -t # Just webservers
avahi-browse -d local _ipp._tcp --resolve -t  # Just printers with IPP protocol
avahi-browse -d local _ipps._tcp --resolve -t # Just printers with IPPS protocol

Edit: If you are running Fedora Server, you’ll need to install the nss-mdns and avahi packages, and enable the avahi daemon:

sudo dnf install nss-mdns avahi
sudo systemctl enable --now avahi-daemon.service

There’s a 2018 Fedora Magazine article about mDNS with helpful information.

2 Likes