How To Prompt For Hostname During Kickstart Installation (CentOS/RHEL 6 and 7)

This post describes the steps to prompt for server hostname during kickstart install.

The hostname parameter can be passed interactively to the anaconda file during the kickstart install using pre and post scripts, as below:

For CentOS/RHEL 6 - Method 1

Pre script


%pre
echo -n "Enter Hostname: " > /dev/tty1
read HOSTN
echo "network --device eth0 --bootproto dhcp --hostname ${HOSTN}" > /tmp/network.txt
%end

Here the line “echo -n “Enter Hostname: " will ask the user the hostname for the system being installed. The same variable is then read and used to configure the hostname in the configuration files.

Post script

%post --nochroot
# bring in hostname collected from %pre, then source it
cp -Rvf network /mnt/sysimage/etc/sysconfig/network
cp ifcfg-eth0 /mnt/sysimage/etc/sysconfig/network-scripts/ifcfg-eth0
%end

Main script

Add to the main body of the anaconda file the file /tmp/network.txt generated with pre-script.

%include /tmp/network.txt

For CentOS/RHEL 6 - Method 2

In this method we can directly use the %pre to prompt for the hostname to the user:

%pre
chvt 3
exec  /dev/tty3
clear
## Query for hostname, then write it to 'network' file
read -p "
What is my hostname (FQDN). This will be set on eth0?
" NAME /dev/tty3 2>&1
echo "NETWORKING=yes" > network
echo "HOSTNAME=${NAME}" >> network
echo "DEVICE=eth0" > ifcfg-eth0
echo "BOOTPROTO=dhcp" >> ifcfg-eth0
echo "ONBOOT=yes" >> ifcfg-eth0
echo "DHCP_HOSTNAME=${NAME} " >> ifcfg-eth0
cat ifcfg-eth0
chvt 1
exec < /dev/tty1 > /dev/tty1
%end

For CentOS/RHEL 7

For the CentOS/RHEL 7, you can use the below sample script to ask user the requured system hostname:

%pre
iotty=`tty`
exec < $iotty > $iotty 2> $iotty

echo -n "Enter the server complete hostname : "
read NAME
echo $NAME > /tmp/hostname.tmp
sleep 1
echo "network --hostname=$NAME" >> /tmp/networkhost.txt

Similar to RHEL/CentOS6 we can then import this script to the main kickstart script.