CentOS 7 Linux Server Cookbook - Second Edition

4.5 (6 reviews total)
By Oliver Pelz , Jonathan Hobson
    Advance your knowledge in tech with a Packt subscription

  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Installing CentOS

About this book

This book will provide you with a comprehensive series of starting points that will give you direct access to the inner workings of the latest CentOS version 7 and help you trim the learning curve to master your server.

You will begin with the installation and basic configuration of CentOS 7, followed by learning how to manage your system, services and software packages. You will then gain an understanding of how to administer the file system, secure access to your server and configure various resource sharing services such as file, printer and DHCP servers across your network. Further on, we cover advanced topics such as FTP services, building your own DNS server, running database servers, and providing mail and web services. Finally, you will get a deep understanding of SELinux and you will learn how to work with Docker operating-system virtualization and how to monitor your IT infrastructure with Nagios.

By the end of this book, you will have a fair understanding of all the aspects of configuring, implementing and administering CentOS 7 Linux server and how to put it in control.

Publication date:
January 2016


Chapter 1. Installing CentOS

In this chapter, we will cover:

  • Downloading CentOS and confirming the checksum on Windows or OS X

  • Creating USB installation media on Windows or OS X

  • Performing an installation of CentOS using the graphical installer

  • Running a netinstall over HTTP

  • Installing CentOS using a kickstart file

  • Re-installing the boot loader

  • Troubleshooting the system in rescue mode

  • Getting started and customizing the boot loader

  • Updating the installation and enhancing the minimal install with additional administration and development tools



This chapter is a collection of recipes that covers the basic practice of installing the CentOS 7 operating system. The purpose of this chapter is to show you how quickly you can get CentOS up and running whilst enabling you to customize your installation with a few 'tricks of the trade' thrown in for good measure.


Downloading CentOS and confirming the checksum on Windows or OS X

In this recipe, we will learn how to download and confirm the checksum of one or more CentOS 7 disk images using a typical Windows or OS X desktop computer. CentOS is made available in various formats by HTTP, FTP, or the rsync protocol from a series of mirror sites located across the world or via the BitTorrent network. For downloading very important files from the Internet, such as operating system images, it is considered best practices to validate those files' checksum, in order to ensure that any resulting media would function and perform as expected when installing. This also makes certain that the files are genuine and come from the original source.

Getting ready

To complete this recipe, it is assumed that you are using a typical Windows-based (Windows 7, Windows Vista, or similar) or OS X computer with full administration rights. You will need an Internet connection to download the required installation files and also need access to a standard DVD/CD disk burner with the appropriate software, in order to create the relevant installation disks from the image files. For the purpose of this recipe, it is assumed that all the downloads will be stored on Windows in your personal C:\Users\<username>\Downloads folder, or if using an OS X system, in the /Users/<username>/Downloads folder.

How to do it...

Regardless of the type of installation files you download, the following techniques can be applied to all the image files supplied by the CentOS project:

  1. Let's begin by visiting http://www.centos.org in a web browser and navigate to the button link Get CentOS Now. Then click the link list of the current mirrors in the text.

  2. The mirror sites are categorized, so from the resulting list of links, choose a mirror that is geographically near your current location. For example, if you are in London (UK), you can choose a mirror from EU and United Kingdom. Now choose a mirror site by selecting either the HTTP or the FTP link.

  3. Having made your selection, you will now see a list of directories of all the available CentOS versions. To proceed, simply click the appropriate folder that reads 7. Next, you will see an additional list of directories, such as atomic, centosplus, cloud, and so on. We proceed by choosing the isos directory.

  4. CentOS 7 currently only supports the 64-bit architecture, so browse to the only directory available labeled x86_64, which is a container for the 64-bit version.

  5. You will now be presented with a series of files available for download. Begin by downloading a copy of the valid checksum result identified as md5sum.txt.

  6. If you are new to CentOS or are intending to follow the recipes found throughout this book, then the minimal installation is ideal. This contains the least amount of packages to have a functional system, so choose the following (XXXX is the month stamp of this release):

  7. On a Windows-based system only (on Mac, this tool is already available in the system), visit http://mirror.centos.org/centos/dostools/ in your browser and download the program md5sum.exe.

  8. Now on Windows, open the command prompt (typically found at Start | All Programs | Accessories | Command Prompt) and type the following commands into the window that will open (press the Enter key at the end of all the lines):

    cd downloads
  9. On OS X, open the program Finder | Applications | Utilities | Terminal, then type the following commands (press the Enter key at the end of all the lines):

    cd ~/Downloads
  10. You should now see all the files in your download folder (including all the downloaded CentOS installation image files, the md5sum.txt file and on Windows, the md5sum.exe program).

  11. Based on the file names shown, modify the following command in order to check the checksum of your downloaded ISO image file. On Windows, type the following command (change the XXXX month stamp accordingly):

    md5sum.exe CentOS-7-x86_64-Minimal-XXXX.iso
  12. On OS X, use instead:

    md5 CentOS-7-x86_64-Minimal-XXXX.iso
  13. Press the Return key to proceed and then wait for the command prompt to respond. The response is known as the MD5 sum and the result could look like the following:

    d07ab3e615c66a8b2e9a50f4852e6a77  CentOS-7-x86_64-Minimal-1503-01.iso
  14. Now look at the the sum and compare against the relevant listing for your particular image file in md5sum.txt (open in a text editor). If both the numbers match, then you can be confident that you have indeed downloaded a valid CentOS image file. If not, your downloaded file is probably corrupted, so please restart this procedure by downloading the image file again.

  15. When you have finished, simply burn your image file(s) to a blank CD-ROM or DVD-ROM using your preferred desktop software, or create a USB installation media from it, as we will show you in the next recipe in this chapter.

How it works…

So what have we learned from this experience?

The act of downloading a CentOS installation image is just the first step towards building the perfect server. Although this process is very simple, many do forget the need to confirm the checksum. In this book, we will work with the minimal installation image, but you should be aware that there are other installation options available to you, such as NetInstall, DVD, Everything, and various LiveCDs.


Creating USB installation media on Windows or OS X

In this recipe, we will learn how to create a USB installation media on Windows or OS X. Nowadays, more and more server systems, desktop PCs, and laptops get shipped without any optical drive. Installing a new operating system, such as CentOS Linux using USB devices gets essential for them as no other installation option is available, as there is no other way to boot the installation media. Also, installing CentOS using USB media can be considerably faster than using the CD/DVD approach.

Getting ready

Before we begin, it is assumed that you have followed the previous recipe in which you were shown how to download a minimal CentOS image and confirm the checksum of the relevant image files. It is also assumed that all the downloads (including the downloaded ISO file) are stored on Windows in your C:\Users\<username>\Downloads folder or if using a OS X system, in the /Users/<username>/Downloads folder. Next, you will need a free USB device which can be discovered by your operating system, with enough total space, and which is empty or with data on it that can be discarded. The total space of the USB device needed for preparing as an installation media for CentOS 7 for the minimal version must be roughly 700 megabyte. If you are working on a Windows computer, you will need a working Internet connection to download additional software. On OS X, you need an administrator user account.

How to do it...

To begin this recipe, start up your Windows or OS X operating system, then connect a free USB device with enough capacity, and wait until it gets discovered by File Manager under Windows or Finder under OS X.

  1. On a Windows based system, we need to download an additional software called dd. Visit http://www.chrysocome.net/dd in your favorite browser. Now download the latest dd-XX.zip file you can find there, with XX being the latest stable version number. For example, dd-0.5.zip.

  2. On Windows, navigate to your Downloads folder using File Manager. Here you will find the dd-05.zip file. Right-click on it and click on Extract All, and extract the dd.exe file without creating any subdirectory.

  3. On Windows, open the command prompt (typically found at Start | All Programs | Accessories | Command Prompt) and type the following commands:

    cd downloads
    dd.exe --list
  4. On OS X, open the program Finder | Applications | Utilities | Terminal, and then type the following commands:

    cd ~/Downloads
    diskutil list
  5. On Windows, to spot the name of the right USB device you want to use as installation media, look into the output of the command under the removable media section. Below that, you should find a line starting with Mounting on and then a drive letter, for example, \.\e:. This cryptic written drive letter is the most important part we need in the next step, so please write it down.

  6. On OS X, the device path can be found in the output of the former command and has the format of /dev/disk<number>, where number is a unique identifier of the disk. The disks are numbered, starting with zero (0). Disk 0 is likely to be the OS X recovery disk, and disk 1 is likely to be your main OS X installation. To identify your USB device, try to compare the NAME, TYPE, and SIZE columns to the specifications of your USB stick. If you have identified the device name, write it down, for example, /dev/disk3.

  7. On Windows, type the following command, assuming your USB device selected as a installation media has the Windows device name \\.\e: (change this as required and be careful what you type – this can create tremendous data loss). Also, substitute XXXX with the correct iso file version number in the next command:

    dd.exe if=CentOS-7-x86_64-Minimal-XXXX.iso of=\\.\e: bs=1M
  8. On OS X, you need two commands which will ask for the administrator password (replace XXXX and disk3 with the correct version number and the correct USB device path):

    sudo diskutil unmountDisk /dev/disk3
    sudo dd if=./CentOS-7-x86_64-Minimal-XXXX.iso of=/dev/disk3 bs=1m
  9. After the dd program finishes, there will be some output statistics on how long it took and how much data has been transferred during the copy process. On OS X, ignore any warning messages about the disk not being readable.

  10. Congratulations! You now have created your first CentOS 7 USB installation media. You now can safely remove the USB drive in Windows or OS X, and physically unplug the device and use it as a boot device for installing CentOS 7 on your target machine.

How it works...

So what have we learned from this experience?

The purpose of this recipe was to introduce you to the concept of creating an exact copy of a CentOS installation ISO file on a USB device, using the dd command-line program. The dd program is a Unix based tool which can be used to copy bits from a source to a destination file. This means that the source gets read bit by bit and written to a destination without considering the content or file allocation; it just involves reading and writing pure raw data. It expects two file name based arguments: input file (if) and output file (of). We will use the CentOS image file as our input filename to clone it exactly 1:1 to the USB device, which is accessible through its device file as our output file parameter. The bs parameter defines the block size, which is the amount of data to be copied at once. Be careful, it is an absolute expert tool and overwrites any existing data on your target while copying data on it without further confirmation or any safety checks. So at least double-check the device drive letters of your target USB device and never confuse them! For example, if you have a second hard disk installed at D: and your USB device at E: (on OS X, at /dev/disk2 and /dev/disk3 respectively) and you confuse the drive letter E: with D: (or /dev/disk3 with /dev/disk2), your second hard disk would be erased with little to no chances of recovering any lost data. So handle with care! If you're in doubt of the correct output file device, never start the dd program!

In conclusion, it is fair to say that there are other far more convenient solutions available for creating a USB installation media for CentOS 7 than the dd command, such as the Fedora Live USB Creator. But the purpose of this recipe was not only to create a ready-to-use CentOS USB installer but also to get you used to the dd command. It's a common Linux command that every CentOS system administrator should know how to use. It can be used for a broad variety of daily tasks. For example, for securely erasing hard disks, benchmarking network speed, or creating random binary files.


Performing an installation of CentOS using the graphical installer

In this recipe, we will learn how to perform a typical installation of CentOS using a new graphical installer interface introduced in CentOS 7. In many respects, this is considered to be the recommended approach to installing your system, as it not only provides you with the ability to create the desired hard disk partitions but also to customize your installation in lots of ways (for example, keyboard layout, package selection, installation type, and so on). Your installation will then form the basis of a server on which you can build, develop, and run any type of service you may want to provide in the future.

Getting ready

Before we begin, it is assumed that you have followed the previous recipe in which you were shown how to download a CentOS image, confirm the checksum of the relevant image files, and create the relevant installation optical disks or USB media. Your system must be a 64 bit (x64_86) architecture, must have at least 406 MB RAM to load the graphical installer 1 GB or more is recommended if installing a graphical window manager such as Gnome), and have at least 10 GB free hard disk space.

How to do it...

To begin this recipe, insert your installation media (CD/DVD or USB device), restart the computer, and press the correct key for selecting the boot device during startup. Then choose the inserted device from the list (for many computers, this can be reached using F11 or F12 but can be different on your system. Please refer to your motherboard's manual).

  1. On the welcome splash screen, the option Test this media & install CentOS 7 is preselected and we will use this option. When you are ready, press the Return key to proceed.

  2. After loading some initial files, the installer then starts to test the installation media. A single test should take between 30 seconds to five minutes and will report if there are any errors on your installation media. When this process is complete, the system will finally load the graphical installer.

  3. The CentOS installer will now present the graphical installation welcome screen. From this point onwards, you can use your keyboard and mouse (the latter is highly recommended), but remember to enable the number lock on your keyboard if you intend to use the keypad.

  4. On the left side you see the main language category and on the right side, the sub-languages for the installer. You can also search for a language using the textbox on the left bottom. All changes to your language settings will take effect immediately, so when you are ready, choose the Continue button to proceed.

  5. Now we reach the main installation menu, which is called Installation summary.

  6. Most options shown here already have some predefined values and can be used without changing, others which do not have any default value and which need your attention are labeled with a red exclamation mark like the Installation Destination under System category. So let's click on it using the mouse.

  7. After clicking the Installation Destination button, you will see a graphical list of all the hard disk devices currently connected to your computer, which you can use for installing the operating system on. You can select your target hard disk by clicking on the correct hard disk symbol. It will then put a check mark on it. If you are unsure about the right hard disk, try to identify it by comparing its brand and total size shown in the menu. Before the installation can proceed, you must select a hard disk. Be careful and choose your target hard disk wisely as it will erase any existing data on it during the installation. When you are ready, click the Done button.

  8. If your selected hard disk already contained data, then when clicking on Done, you may see what could be described as a warning/error message. The message may read: You don't have enough space available to install CentOS. Don't worry! This is to be expected and the message is simply asking you to re-initialize your hard disk because CentOS can only be installed on an empty disk. In most cases, especially if you have more than one partition on the hard disk, simply click on Reclaim space which will show a new window with a detailed list of all the partitions on this drive. Here just click on Delete All and then again on Reclaim space to discard any data on this disk, which will complete the task of disk initialization and enable you to proceed to the next step. When finished, click the Done button.

  9. Back at the Installation Summary screen, the exclamation mark on the Installation Destination item should be gone now.

  10. Optionally, we can click on Network & Hostname under System category. On the following page, on the left side, you can choose the primary network adapter you would like to connect to the Internet and select it by clicking on it. For the selected device, click on the switch on the right side to enable and connect it automatically using the On position of the switch. Finally, before closing this submenu, change the hostname in its text field to something appropriate. Click Done.

  11. Now back at the Installation Summary screen, all the important settings have been made or have got predefined values, and all the exclamation marks are gone. If you are happy with these settings, click on the Start installation button or change the settings appropriately.

  12. On the next screen, you will be required to create and confirm a root password for the root user while the new system gets installed in the background. Choose a secure password with not less than six characters.

  13. Here on this screen you can also create a standard user account which is highly recommended. If you create a new user do not check Make this user administrator. When you are ready, click Done (if you entered a weak password, you have to confirm this by clicking twice)

  14. CentOS will now partitionate and format your hard disk in the background and resolve any dependencies, and the installer will begin writing to the hard disk. This may take some time, but a progress bar will indicate the status of your installation. When finished, the installer will inform you that the entire process is complete and that the installation was successful. So when you are ready, click on the Reboot button. Now release your installation media from the drive.

  15. Congratulations! You have now installed CentOS 7 on your computer.

How it works…

In this recipe, you have discovered how to install the CentOS 7 operating system. Having covered the typical approach to the graphical installation process, you are now in a position to develop the server with additional configuration changes and packages that will suit the role you intend the server to fulfill. This graphical installer has been built with the aim to be very intuitive and flexible, and makes installation very easy as it will guide the user through some mandatory tasks that he has to fulfill before the installation of the main system can be started.


Running a netinstall over HTTP

In this recipe, we will learn how to initiate the process of running a netinstall over HTTP (using the URL method) in order to install CentOS 7. It is a process in which a small image file is used to boot the computer and let the user select and install only the software packages and services he wants and nothing more over a network connection thus providing great flexibility.

Getting ready

Before we begin, it is assumed that you already know how to download and checksum a CentOS 7 installation image and how to create the relevant installation media from it. For this recipe here, we will need to download and create installation media for the netinstall image (download the latest CentOS-7-x86_64-NetInstall-XXXX.iso file) instead of the minimal ISO shown in another recipe in this chapter. Also, it is assumed that you have at least gone through the graphical installation procedure once to exactly know how to boot from your installation media and work with the installer program.

How to do it...

To begin this recipe, insert your prepared netinstall media, boot your computer from it, and wait for the welcome screen to appear:

  1. On the welcome splash screen, the option Test this media & install CentOS 7 is preselected and we will use this option. When you are ready, press the Return key to proceed.

  2. After the tests finish, the graphical installer will load and present the typical graphical installation summary screen.


    Here the installer should be configured exactly as in the normal graphical installation recipe, besides the following mandatory changes to the Network & Host name and Installation source menu items (which is shown by the red exclamation marks).

  3. Before we can install CentOS over the network, we have to make sure that we have a working network connection. Therefore, you should first click on the Network & Host name menu entry and activate one of your network adapters to the connected state. Refer to the normal installation recipe for more details.

  4. Next, click on Installation source to enter the settings. As we will be installing over HTTP (also referred to as the URL method), you should leave the default On the network selected in the Which installation source would you like to use? section.

  5. Now type in the following URL in the standard http:// textfield, which we will use to download all the required installation packages at http://mirror.centos.org/centos/7/os/x86_64/.

  6. Alternatively, you can also use a personal repository which you would have to create in advance (see Chapter 4, Managing Packages with YUM)

  7. When you are ready, click on Done to start the initialization process.

  8. On success, the installer will begin to retrieve the appropriate install.img file. This may take several minutes to complete, but once resolved, a progress bar will indicate all the download activity. When this process finishes successfully, the exclamation mark at the installation source will go away but another one will pop up which will tell the user that it is missing the software selection. Click on it and choose whatever fits your need. As for the purpose of this recipe, just select Minimal install under Base environment and then click on Done.

  9. If the Which installation source would you like to use stays greyed out and cannot be changed, then there are connection problems with your network adapter. If this is the case, go back to configure Network & Hostname and change the network settings until the connected state can be reached.

  10. CentOS 7 will now install the operating system the usual way and will congratulate you when this process finishes. It may be slower than installing from a physical installation media since all the packages have to be retrieved from the Internet.

How it works...

The purpose of this recipe was to introduce you to the concept of the CentOS network installation process, in order to show you just how simple this approach can be. By completing this recipe you have not only saved time by limiting your initial download to those files that are required by the installation process, but you have also been able to take advantage of the full graphical installation method without the need for a complete DVD suite.


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.

  1. 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
  2. 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
  3. 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, where X is an alphabetical character, such as b, c, d, e, … and so on. If you cannot find the right device name for your USB media using the fdisk command, try the following trick: run fdisk -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 !

  4. 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
  5. 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
  6. 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
  7. 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
  8. 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>
  9. 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
  10. 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):

  11. 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.

  12. Next, install the following CentOS package:

    yum install system-config-kickstart
  13. 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
  14. If the config file is error-free, unmount the USB stick now by using the following commands:

    umount /mnt/kickstart-usb
  15. 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.

  16. 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.

  17. 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).

  18. Next, start the server and press the correct key during the initial bootup screen, associated with booting the CentOS installation media you just connected.

  19. 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.

  20. Next, press the Esc key on your keyboard once to switch to the boot: prompt.

  21. 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


    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.

  22. 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.

  23. 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.


Getting started and customising the boot loader

When you turn on your computer, the boot loader is the first program that starts up and is responsible for loading and transferring control to an underlying operating system. Nowadays, almost any modern Linux distribution uses the GRand Unified Bootloader version 2 (GRUB2) for starting the system. It has a lot of flexibility in configuration and supports a lot of different operating systems. In this recipe, we will show how to customize the GRUB2 boot loader by disabling the waiting time of the menu display and therefore improving the time it takes for booting the system.

Getting ready

To complete this recipe, you will require access to an already installed CentOS 7 operating system (minimal or any other CentOS 7 installation type will work) with root privileges. Also, you need to have some basic experiences with a text based editor, such as nano, for changing the configuration files.

How to do it...

We begin this recipe by opening the main GRUB2 configuration file with our text editor of choice and modifying it.

  1. First log in as root into your system and create a copy of the GRUB2 configuration file for backup and rollback, if needed. Press the Return key to finish:

    cp /etc/default/grub /etc/default/grub.BAK
  2. Open the main GRUB2 configuration file that we want to edit with the following command and press the Return key (here we will use the editor nano, if you have not installed it yet type yum install nano):

    nano /etc/default/grub
  3. Press the Return key in the first line where the cursor is at to insert a new line at the top, and then insert the following line:

  4. Add a # sign to the beginning of the following line, as shown:

  5. Now save the file in the nano using Ctrl+o (and Return to confirm the filename to save). Use Ctrl+x to exit the editor and then run the following command:

    dmesg | grep -Fq "EFI v"
  6. If the preceding command does not produce any output, run the following command:

    grub2-mkconfig -o /boot/grub2/grub.cfg
  7. Otherwise, if there is an output, run:

    grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg
  8. If grub2-mkconfig is successful, it will print Done. Now reboot your system using the following command:

  9. During the rebooting process, you will notice that the GRUB2 boot menu will not appear any more and the system will boot up faster.

How it works...

Having completed this recipe, we now know how to customize the GRUB2 boot loader. In this very easy recipe, we only showed you very basic modifications to the boot loader but it can do much more! It supports a broad variety of filesystems and can boot almost any compatible operating system. This is also particularly useful if you plan to run multiple operating systems on the same machine. To learn more about GRUB2's configuration file syntax type the info grub2 | less command and go to the section 6.1 Simple configuration handling (read the recipe Navigating text files with less in Chapter 2, Configuring the System to learn how to browse this document).


Troubleshooting the system in rescue mode

We all make mistakes and this is especially true for novice Linux system administrators. Linux can have a steep learning curve and sooner or later there will be a point in your career where your CentOS installation does not start up due to broad number of reasons, including hardware problems or human mistakes such as configuration errors. If this has happened to you then you can use the CentOS rescue mode in order to boot an otherwise unbootable system and try to undo your mistakes or find out the root of the problems. In this recipe, we will show you three common use cases when to use this option:

  • Accessing the filesystem for recovering important data or undoing changes to configuration files if CentOS is not booting up

  • Changing the root password if you forgot it

  • Re-installing the boot loader which can be damaged when installing another operating system on the same harddisk where CentOS is installed

Getting ready

To complete this recipe, you will require a standard installation media (CD/DVD or USB device) of the CentOS 7 operating system. For recovering the data from the system, you will need to connect some sort of external storage device to the system, such as an external hard disk or a working network connection to another computer to copy all your precious data to a different location.

How to do it...

To begin this recipe, you should boot your server from the CentOS installation CD/DVD or the USB device and wait until the first welcome splash screen appears with the cursor waiting at the Test this media & install CentOS 7 menu option.

Reaching rescue mode

  1. From the main menu, use the down arrow key to select Troubleshooting and then press the Return key to proceed.

  2. On the Troubleshooting screen, use the down arrow key to highlight Rescue a CentOS system. When you are ready, press the Return key to proceed.

  3. After some loading time, we enter the rescue screen, which includes various confirmation sub-screens. To begin this section, use the left and right arrow keys to choose Continue and press the Return key to proceed.

  4. On the first sub-screen, choose OK and press the Return key to proceed.

  5. Again, in the following sub-screen, choose OK and press the Return key to proceed.

  6. On the next screen, choose the Start shell and by using the Tab key, highlight OK and press the Return key to proceed.

  7. By completing the preceding steps, you will launch a shell session. You will notice this at the bottom of your display. The current status of the shell session will read as follows:

  8. At the prompt, type the following instruction to change the root filesystem, before pressing the Return key to complete your request:

    chroot /mnt/sysimage
  9. Congratulations! You just reached the rescue mode. To exit it at any time, simply type the following command and then press the Return key to complete your request (don't do this right now as this will restart the system):

  10. After the basic rescue mode is reached, we have the following options, depending on the type of problem.

Accessing the filesystem

If you are now in the rescue mode and need to backup important files from the filesystem, you need a destination location for the data transfer. For transferring the data we want to recover from the server to another computer please physically connect an external USB device to it. You can also use network storages for the recovery. For example, you could import an NFS server share and copy data to it. Refer to the Working with NFS recipe in Chapter 7, Building a Network.

  1. On the rescue mode command line, type in the following command, which will show you all the current partitions connected to the system, and then press the Return key to complete your request:

    fdisk -l
  2. You now need to find out the right device name with the partition number of your connected device; comparing the total size or the filesystem output of the various devices with the specifications from your stick can help you in this process. You can also try the following trick: run the fdisk -l command twice, first with the plugged-in USB device and then again with the USB device unplugged, and compare the output of both the commands. It should be different by one device name which you are searching for!

  3. If you have found the right device name in the list, create a directory to mount the stick to the filesystem:

    mkdir /mnt/hdd-recovery
  4. Next, mount the disk partition to this folder. Here we assume that the USB device of interest has the device name sdd1 (please change if different on your system):

    mount /dev/sdd1 /mnt/hdd-recovery
  5. The original system's hard disk's root partition has been mounted under a specific folder by the rescue system automatically (under /mnt/sysimage), if you need to access it for example to change configuration files which caused startup problems or make a full or partial backup. For example, if you need to backup your Apache webserver configuration files, use:

    cp -r /mnt/sysimage/etc/http /mnt/hdd-recovery
  6. If you need to access the data that lives on partitions other than the currently mounted root partition, use fdisk -l to identify the partition of interest. Then create a directory and mount the partition to it and change to that directory to access your data similar you did when mounting the USB device.

  7. To finish backing up the files, type:


Accessing the filesystem

  1. If you are in the rescue mode for changing the root password, just use the following command and provide a new password:

  2. To complete changing the password, type:


Re-install the CentOS boot loader

  1. We will now use the fdisk command to find the name of all the current partitions. To do this, type the following instruction and then press the Return key to complete your request:

    fdisk –l
  2. Now run the following command:

    dmesg | grep -Fq "EFI v"
  3. If the preceding command does not produce any output look for the * symbol in the fdisk listing in the boot column to find the correct start partition, and assuming that your boot disk is on /dev/sda1 (change this as required), type the following:

    grub2-install /dev/sda
  4. Otherwise, if there is an output, run instead:

    yum reinstall grub2-efi shim
  5. If no error is reported, the console should respond as follows:

    # this device map was generated by anaconda
    (hd0) /dev/sda
  6. The console output from the last step has confirmed that GRUB has now been successfully restored.

  7. To reboot the computer, type:


How it works...

There are a broad variety of problems which can be resolved by the tools provided through the rescue mode environment. Often these problems refer to booting problems but can also be from different types, such as forgetting the root password. Rescue mode can be a life-saver and an understanding of it is a very important skill to learn. It was felt that such a recipe should thus remain close at hand.


Remember to always be careful when working with bootloader commands as improper use can make your operating system unbootable.


Updating the installation and enhancing the minimal install with additional administration and development tools

In this recipe, we will learn how to enhance the minimal install with additional tools that will give you a variety of administrative and development options, which in turn will prove vital during the lifetime of your server and which are essential for some recipes in this book. The minimal install is probably the most efficient way you can install a server, but having said that, a minimal install does require some additional features in order to make it a more compelling model.

Getting ready

To complete this recipe, you will require a minimal installation of the CentOS 7 operating system with root privileges and a connection to the Internet in order to facilitate the download of additional packages.

How to do it...

We will begin this recipe by updating the system.

  1. To update the system, log in as root and type:

    yum -y update
  2. CentOS will now search for the relevant updates and, if available, they will be installed. On completion and depending on what was updated (that is, kernel and new security features to name but a few), you can decide to reboot your computer. To do this, type:

  3. Your server will now reboot and return to the login screen. We will now complete this recipe and enhance our current installation with a series of package groups that will prove to be very useful in the future. To do this, log in as root and type:

    yum -y groupinstall "Base" "Development Libraries" "Development Tools"
    yum -y install policycoreutils-python

How it works...

The purpose of this recipe is to enhance the minimal installation of the CentOS 7 operating system and by doing this you have not only introduced yourself to the Yellowdog Updater Modified (YUM) package manager (something to which we will return to later on in this book), but you now have a system that is capable of running a vast amount of applications right out-of-the-box.

So what have we learned from this experience?

We started the recipe by updating the system in order to ensure that it is up to date. At this stage, it is often a good idea to reboot the system. It is not expected that we will do this very often but it is expected when updating for the first time after the installation of the operating system, as it is most likely that there are major changes available. The reason behind this is typically based on the desire to take advantage of a new kernel or revised security updates. In the next phase, the recipe showed you how to add a series of package groups that may prove to be more than useful in the future. To save time, we wrapped the instruction to install the three main package groups: Base, Development Libraries, and Development Tools. The preceding action alone installs over 200 individual packages, thereby giving your server the ability to compile the code and run a vast array of applications out-of -the-box, that you may need over the life time of your server. To see a list of all the packages within a group, for example, from Base, run the yum groupinfo Base command. Another package we installed was policycoreutils-python which provides tools and programs to manage the security enhanced access control to Linux, which we will use quite often throughout the chapters of this book.

About the Authors

  • Oliver Pelz

    Oliver Pelz has more than 10 years of experience as a software developer and system administrator. He graduated with a diploma degree in Bioinformatics and is currently working at the German Cancer Research center in Heidelberg where he authored and co-authored several scientific publications in the field of Bioinformatics. Next to developing web applications and biological databases for his department and scientists all over the world he is administrating a division-wide Linux-based data center and has setup two high-performance CentOS clusters for the analysis of high-troughput microscope and genome sequencing data. He loves writing code, riding his mountainbike in the Black Forest of Germany and is an absolute Linux and open-source enthusiast for many years. He has contributed to several open-source projects in the past and also worked as a reviewer on the book Centos 7 High Performance. He maintains an IT tech blog at www.oliverpelz.de **********************************Acknowledgement************************************** I would like to thank my family and especially my wonderful wife Beatrice and little son Jonah for their patience and understanding for all these long working hours while writing this book. Also I would like to thank the folks at Packt Publishing for all their support and the opportunity to to write this book, it was a great pleasure for me. Last but not least I would like to thank Jonathan Hobson for writing the first edition of this book, without him no second edition of this book would have been possible. I would also like to thank all of the mentors that I’ve had over the years, especially Tobias Dykerhoff who introduced me to the whole world of Linux a long time ago and infected me with his enthusiasm about open-source and the free software movement. **********************************Dedication*********************************** This work is dedicated to my son Marlin Pelz who was stillborn on 2.10.15 just a few days before his expected date of delivery while writing the last few chapters of this book. Marlin, words can not express how much I miss you!

    Browse publications by this author
  • Jonathan Hobson

    Jonathan is a web developer, systems engineer and applications programmer, who, for more than 20 years has been working behind the scenes to support companies, organizations and individuals around the world to realize their digital ambitions. With an honors degree in both English and History and as a respected practitioner of many computer languages Jonathan enjoys writing code, publishing articles, building computers, playing the video games and getting 'out and about' in the big outdoors. He has been using CentOS since its inception and over the years, it has not only earned his trust, but it has become his first choice server solution. CentOS is a first class community based enterprise class operating system, it is a pleasure to work with, and because of this Jonathan has written this book in order that his knowledge and experience can be passed on to others.

    Browse publications by this author

Latest Reviews

(6 reviews total)
everything went well 12345
I like this book and the book help me for work.
通读一遍,即使是资深人士也能有收获。 cookbook系列一直接的很赞!

Recommended For You

Book Title
Unlock this book and the full library for FREE
Start free trial