How to set hostname during kickstart installation (CentOS/RHEL 5,6,7)

For CentOS/RHEL 5 and 6

Here, you can specify the hostname as a boot parameter in Anaconda, then set the hostname from this parameter in a %pre script. The steps are as below:

1. Provide the hostname on anaconda boot command line with kickstart file path as well.

boot: linux ks= ksdevice=eth0

2. Add the following %pre script in kickstart file.

%pre --interpreter /bin/bash
hostname > /tmp/hostname
echo "network --bootproto=dhcp --hostname=`cat /tmp/hostname`" > /tmp/networking

This will set the hostname and one can use the hostname set by running hostname command in %pre script or read the file /tmp/hostname.

3. Next, disable the default network line in the kickstart profile, by unchecking the checkbox next to the “network” field in the kickstart profile’s Advanced Options tab. On the same Advanced Options tab, add the following to the “Custom options” field:

%include /tmp/networking

For CentOS/RHEL 7

This can be achieved using %post script in kickstart using nochroot environment. Below %post can be used to achieve this:

%post --nochroot
hostnamectl set-hostname
hostnamectl --pretty set-hostname
cp /etc/hostname /mnt/sysimage/etc/hostname
cp /etc/machine-info /mnt/sysimage/etc/machine-info