Installing CentOS 7 using a kickstart file
While installing CentOS 7 manually using the graphical installer utility is fine on a single server, doing so on a multiple number of systems can be tedious. Kickstart files can automate the installation process of a server system and here we will show how this can be done. They are simple text based configuration files which provide detailed and exact instructions on how the target system should be set up and installed (for example, which keyboard layout or additional software packages to install).
Getting ready
To successfully complete this recipe, you will need access to an already installed CentOS 7 system to retrieve the kickstart configuration file we want to work with and use for automated installation. On this pre-installed CentOS server, you also need a working Internet connection to download additional software.
Next, we will need to download and create installation media for the DVD or the Everything image (download the latest CentOS-7-x86_64-DVD-XXXX.iso
or CentOS-7-x86_64-Everything-XXXX.iso
file), instead of the minimal iso file shown in another recipe in this chapter. Then you need another USB device which must be read and writable on Linux systems (formatted as FAT16, FAT32, EXT2, EXT3, EXT4, or XFS filesystem).
How to do it...
For this recipe to work, we first need physical access to an existing kickstart file from another finished CentOS 7 installation, which we will use as a template for a new CentOS 7 installation.
- Log in as root on the existing CentOS 7 system and make sure the kickstart configuration file exists by typing the following command and pressing the Return key to execute (this will show you the details of the file):
ls -l /root/anaconda-ks.cfg
- Next, physically plug in a USB device and then type the following command, which will give you a list of all the hard disk devices currently connected to the computer:
fdisk -l
- Try to identify the device name by comparing its size, partitions, and identified filesystems with the specifications of your USB device. The device name will be of kind
/dev/sdX,
whereX
is an alphabetical character, such asb
,c
,d
,e
, … and so on. If you cannot find the right device name for your USB media using thefdisk
command, try the following trick: runfdisk -l
twice - first with plugged-out and then with plugged-in USB device and compare how the second output changed - it has one device name more than the first output: your device name of interest ! - If you have found the right device name in the list, create a directory to mount it to the current filesystem:
mkdir /mnt/kickstart-usb
- Next, actually mount the stick to this folder, assuming that your USB partition of choice is at
/dev/sdc1
(change this as required):mount /dev/sdc1 /mnt/kickstart-usb
- Now we will create our working copy of the kickstart file on the USB device for customizing:
cp /root/anaconda-ks.cfg /mnt/kickstart-usb
- Next, open the copied kickstart file on the USB device with your favorite text editor (here we will use the editor nano, if you have not installed it yet type
yum install nano
):nano /mnt/kickstart-usb/anaconda-ks.cfg
- We will now modify the file for installing CentOS on a new target system. In nano, use the up and down arrow keys to go to the line which starts with (
<your_hostname>
will be the name of the hostname you gave during installation e.g.minimal.home
):network --hostname=<your_hostname>
- Now edit the
<your_hostname>
string to give it a new unique hostname. For example, add a-2
to the end of any existing name, as shown next:network --hostname=minimal-2.home
- Next, move the cursor down using the up and down arrow keys until it stops at the line which says
%packages.
Append the following lines right below it (you can further customize this and provide additional packages that you want to install automatically):mariadb-server httpd rsync net-tools
- Now save and close the file, to do this in the nano editor use the key combination Ctrl+o (which means, hold down the Ctrl key on the keyboard and then the o key without releasing the Ctrl key) to write the changes. Then press Return to confirm the filename and Ctrl+x to exit the editor.
- Next, install the following CentOS package:
yum install system-config-kickstart
- Now we validate the syntax of our kickstart file using the
ksvalidator
program, which is included in the package we just installed:ksvalidator /mnt/kickstart-usb/anaconda-ks.cfg
- If the
config
file is error-free, unmount the USB stick now by using the following commands:cd umount /mnt/kickstart-usb
- When you get a new command prompt again, unplug the USB device with the kickstart file for using on the target machine physically from the system.
- Now you need physical access to the target machine you want to install CentOS on, using the kickstart file just created. Disconnect any other external file storage(s) that you do not need during the installation.
- Power on the computer and put in your prepared CentOS installation media (must be a CentOS DVD or Everything installation disk image prepared on a CD/DVD disc or a USB device installer). Also connect to the computer the USB stick containing the kickstart file you just created in the earlier steps (if you using a USB drive for installing CentOS then you will need two free USB ports in total to complete this recipe).
- Next, start the server and press the correct key during the initial bootup screen, associated with booting the CentOS installation media you just connected.
- After the CentOS installer starts loading, the common standard CentOS 7 installation welcome screen will show up and the option Test this media & install CentOS 7 will be pre-selected by the cursor.
- Next, press the Esc key on your keyboard once to switch to the
boot: prompt
. - Now we are ready to start the kickstart installation. To do this, you need to know the exact partition name on the USB device where the kickstart file is located. Type the following command, assuming that your partition is at
/dev/sdc1
(change this as required), and press the Return key to start the kickstart installation process:linux ks=hd:sdc1:/anaconda-ks.cfg
Note
If you cannot find out the right device and partition name of the USB stick, you have to start the target system in rescue mode (refer to the Troubleshooting the system in rescue mode recipe) to identify the right device name and partition number by comparing its size, partitions, and identified filesystems with the specifications of your stick.
- The new system now gets installed automatically using the instructions from the provided kickstart file. You can watch the installation output messages as it is showing the user detailed installation progress.
- If the system has finished installing, reboot the system and log in to your new machine to verify that the new system has been setup the way we described using the kickstart file.
How it works...
In this recipe, you have seen that every server running a CentOS 7 installation keeps the kickstart file in its root directory, which contains detailed information on how the system had been set up during the installation. The kickstart files can be used to automate the installations of multiple systems with the same configuration. This can save a lot of time doing repetitive work as no user interaction during installation is needed. Also, we can use this method if the target machines don't meet the minimum requirement in RAM for graphical based installations but when needed other features the text mode installer does not provide such as custom partitioning of the system. Kickstart configuration files are simple plain text files which can be created manually from scratch. Because there are quite a number of different commands available to construct your system using the kickstart syntax, we used an existing file as a template and customized it to fit our needs, instead of starting out completely new. We did not use the minimal installation image to drive our kickstart installation because we installed some extra packages not included on the minimal ISO file, such as the Apache webserver.