Tag Archives: hostname

Automatically set the hostname during Kickstart Installation

When you want to install linux on a large number of servers kickstart approach is a very good one. But what about hostname?! You have many choices:

  • A kickstart file for each server, but come on... what kind the choice is this?!
  • A kickstart file for all servers and set hostname after installation (manually on every single server, or using a script)

Fortunately for you there is a third option: Automatically set the hostname during Kickstart Installation. I wish to take credit for this, but this will be so unfair for the guy which wrote an article about this.

I won't get to long with the story so... let's get started.

The trick is to pass the kernel a parameter and use it in our kickstart file. What if you were to pass a parameter that it doesn't recognize? In most cases, it will probably ignore it, but it will still in the kernel list. We can check kernel parameters by issuing the following command:

[codesyntax lang="bash"]

cat /proc/cmdline

[/codesyntax]

So what if we can pass a parameter with desired hostname to the kernel? With a very simple script we can parse the output of the above command and look for our parameter.

[codesyntax lang="bash"]

#!/bin/sh

echo "network --device eth0 --bootproto dhcp --hostname localhost.localdomain" > /tmp/network.ks

for x in `cat /proc/cmdline`; do
        case $x in SERVERNAME*)
            eval $x
        echo "network --device eth0 --bootproto dhcp --hostname ${SERVERNAME}" > /tmp/network.ks
                ;;
            esac;
done

[/codesyntax]

Here we are looking for SERVERNAME end evaluates that value into a variable. We will then echo the network setup with the variable (which we will use as part of the hostname setup) and redirect into the file under /tmp. Then we will include that file in our installation section.

You may ask yourself what is all about this line:

[codesyntax lang="bash"]

echo "network --device eth0 --bootproto dhcp --hostname localhost.localdomain" > /tmp/network.ks

[/codesyntax]

in the script above?! Well, if you don't pass the SERVERNAME to the kernel, then /tmp/network.ks will not be created and your installation will fail.

So this is my kickstart file for a minimal CentOS 6.3 installation:

install
firewall --disabled
url --url="ftp://ftp.ines.lug.ro/centos/6.3/os/i386"
network --bootproto=dhcp --device=eth0
rootpw --iscrypted YOUR_ENCRYPTED_PASSWORD
text

%include /tmp/network.ks

keyboard us
lang en_US
selinux --disabled
skipx
logging --level=info
reboot
timezone --utc Europe/Bucharest
bootloader --location=mbr --driveorder=sda,sdb --append="console=tty0 console=ttyS0,115200N1"
zerombr
clearpart --all --initlabel
part / --fstype="ext4" --size=10000
part swap --fstype="swap" --size=8000
part pv.01 --fstype="ext4" --grow --size=1
volgroup vg0 pv.01
logvol /data --vgname=vg0 --percent=90 --name=lv0 --fsoptions=noatime --fstype=ext4 --size=1 --grow

%packages
@core
sed
perl
less
dmidecode
bzip2
iproute
iputils
sysfsutils
rsync
nano
mdadm
setserial
man-pages.noarch
findutils
tar
net-tools
tmpwatch
lsof
python
screen
lvm2
curl
ypbind
yp-tools
smartmontools
openssh-clients
acpid
irqbalance
which
bind-utils
ntsysv
ntp
man
mysql
postfix
chkconfig
gzip
%end

%pre
#!/bin/sh

echo "network --device eth0 --bootproto dhcp --hostname localhost.localdomain" > /tmp/network.ks

for x in `cat /proc/cmdline`; do
        case $x in SERVERNAME*)
               eval $x
        echo "network --device eth0 --bootproto dhcp --hostname ${SERVERNAME}" > /tmp/network.ks
                ;;
            esac;
    done
%end

%post

cat > /etc/cron.d/ntpdate < /dev/null 2>&1
EOF

chkconfig ntpd on
chkconfig sshd on
chkconfig ypbind on
chkconfig iptables off
chkconfig ip6tables off
chkconfig yum-updatesd off
chkconfig haldaemon off
chkconfig mcstrans off
chkconfig sysstat off

cat > /etc/motd <> /etc/motd

echo >> /etc/motd
%end