Random static ip address in kickstart Rocky Linux 8.5
Reasoning
DHCP is great if you really don’t care about the ip addresses you get for a given host - but if you want to modify the host to use a static ip address afterwards and you always do that after a kickstart installation, then its easier to just assign a “random” static ip address up front.
The reason for this is that the network configuration when you run with DHCP is much simpler and when you want to switch to a static configuration you have to add a whole lot of options to the network configuration.
When the system is already configured with a static ip address and you want to change that - it is just one simple variable you have to change.
As you can see from the following example network config
TYPE="Ethernet"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
NAME="ens192"
UUID="bb5e5b01-946d-493e-b595-1891bb0bab19"
DEVICE="ens192"
ONBOOT="yes"
ETHTOOL_OPTS="autoneg on"
IPADDR="192.168.0.4"
PREFIX="16"
GATEWAY="192.168.0.1"
DNS1="192.168.0.2"
DNS2="192.168.0.3"
DOMAIN="root.dom"
You only have to change the IPADDR variable - a single line.
Had the system been configured with DHCP you would need to change a configuration like the one below:
TYPE=Ethernet
DEVICE=ens192
UUID=bb5e5b01-946d-493e-b595-1891bb0bab19
ONBOOT=yes
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME="ens192"
To look like the one above for the static configuration - which would mean you would need to change the variables:
BOOTPROTO -> none
Add the following variables
IPADDR="192.168.0.4"
PREFIX="16"
GATEWAY="192.168.0.1"
DNS1="192.168.0.2"
DNS2="192.168.0.3"
DOMAIN="root.dom"
So enter my little %pre kickstart script that I use.
In your kick start file you need to add or modify an existing %pre section. In this section you add a script that generates a random number and check that the ip address is not currently in use - it assign it to the first network adapter in the system.
Script
It could look like the one below.
%pre
#generate a number between 1 & 250
number=$(($RANDOM%250+1))
echo "trying to use 192.168.4.$number" as host ip
exist=$(ping -c1 -W1 -q 192.168.4.$number &>/dev/null;echo $?)
while [ "$exists" == "0" ]
do
number=$(($RANDOM%250+1))
echo "checking 192.168.4.$number"
exist=$(ping -c1 -W1 -q 192.168.4.$number &>/dev/null;echo $?)
done
ifname=$(ls /sys/class/net/ |grep ens|head -n 1)
echo "using kube$number.root.dom/192.168.4.$number as host name/ip"
echo "network --hostname=kube$number.room.dom" >> /tmp/pre-hostname
echo "network --bootproto=static --device=$ifname --ethtool=\"autoneg on\" --ipv6=dhcp --ip=192.168.4.$number --netmask=255.255.0.0 --gateway=192.168.0.1 --nameserver=192.168.0.2 --nameserver=192.168.0.3 --activate" >> /tmp/$
%end
Then in the other part of the kick start file where you normally would have your network configuration you remove those and instead just add
%include /tmp/pre-network
%include /tmp/pre-hostname
When the system is installed with kickstart - your system has a static “randomly” generated ip address that currently is not in use - and you can either decide to just keep using that ip address - or simply change that single variable required so you get whatever ip address you require.
Which is done inside the file
/etc/sysconfig/network-scripts/ifcfg-ens192