41,99 €
Deploy and manage today's essential services on an enterprise-class, open operating system
This book is for Linux professionals with basic Unix/Linux functionality experience, perhaps even having set up a server before, who want to advance their knowledge in administering various services.
CentOS is derived from Red Hat Enterprise Linux (RHEL) sources and is widely used as a Linux server. This book will help you to better configure and manage Linux servers in varying scenarios and business requirements.
Starting with installing CentOS, this book will walk you through the networking aspects of CentOS. You will then learn how to manage users and their permissions, software installs, disks, filesystems, and so on. You'll then see how to secure connection to remotely access a desktop and work with databases. Toward the end, you will find out how to manage DNS, e-mails, web servers, and more. You will also learn to detect threats by monitoring network intrusion. Finally, the book will cover virtualization techniques that will help you make the most of CentOS.
This easy-to-read cookbook is filled with practical recipes. Hands-on, task-based exercises will present you with real-world solutions to deploy and manage CentOS in varying business scenarios.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 418
Veröffentlichungsjahr: 2016
Copyright © 2016 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: September 2016
Production reference: 1270916
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78328-888-5
www.packtpub.com
Author
Timothy Boronczyk
Copy Editor
Tom Jacob
Reviewer
Mitja Resman
Project Coordinator
Kinjal Bari
Commissioning Editor
Kartikey Pandey
Proofreader
Safis Editing
Acquisition Editor
Rahul Nair
Indexer
Pratik Shirodkar
Content Development Editor
Mehvash Fatima
Graphics
Kirk D'Penha
Technical Editors
Devesh Chugh
Siddhi Rane
Production Coordinator
Shantanu N. Zagade
Timothy Boronczyk is a native of Syracuse, New York, where he works as a lead developer at Optanix, Inc. (formerly ShoreGroup, Inc.). He's been involved with web technologies since 1998, has a degree in Software Application Programming, and is a Zend Certified Engineer. In what little spare time he has left, Timothy enjoys hanging out with friends, studying Esperanto, and sleeping with his feet off the end of the bed. He's easily distracted by shiny objects.
Mitja Resman comes from a small, beautiful country called Slovenia, located in southern Central Europe. Mitja is a fan of Linux and is an open source enthusiast. Mitja is a Red Hat Certified Engineer and Linux Professional Institute professional. Working as a system administrator, Mitja got years of professional experience with open source software and Linux system administration on local and international projects worldwide. The swiss army knife syndrome makes Mitja an expert in the field of VMware virtualization, Microsoft system administration, and lately, also Android system administration.
Mitja has a strong desire to learn, develop, and share knowledge with others. This is the reason he started a blog called GeekPeek.Net (https://geekpeek.net/). GeekPeek.Net provides CentOS Linux guides and How to articles covering all sorts of topics appropriate for beginners and advanced users. He wrote a book, CentOS High Availability by Packt Publishing, covering the topic of how to install, configure, and manage clusters on CentOS Linux.
Mitja is also a devoted father and husband. His two daughters and wife are the ones who take his mind off the geek stuff and make him appreciate life, looking forward to things to come.
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
For over a decade, the CentOS project has provided the community with a free, enterprise-grade operating system through the rebranding and recompilation of the Red Hat Enterprise Linux source. Since CentOS users rely almost exclusively on the community for their support needs, I was keen to write this book when Packt approached me about the project's latest release, CentOS 7. The recipes we chose cover a wide range of topics, from getting started to managing many common web services, and hopefully administrators of any skill level will find something of interest.
However, writing a book is a huge undertaking. Because of this, I want to thank the staff at Packt, my family, and my friends, for their support. The dog needs to be taken for a walk, family engagements need attending, and emergencies arise at the workplace. Without the understanding and encouragement of those around me and the editorial staff, you wouldn't be reading this book.
The recipes presented in this book aim to make even the most difficult configuration tasks easy by providing step-by-step instructions and discussion. Here's a quick rundown of what you can expect from each of the 12 chapters.
Chapter 1, Getting Started with CentOS, contains recipes for installing CentOS using graphical, text-based, and kick-start approaches. How to set up a CentOS platform for projects running Docker and on Amazon Web Services is also discussed.
Chapter 2, Networking, contains recipes to help you complete common networking tasks, such as how to set up a static IP address, assign multiple addresses to a single network interface, bond multiple interfaces with the same address, and configure the system's firewall using FirewallD and iptables. It also presents recipes for configuring network services such as DHCP, NFS, and Samba.
Chapter 3, User and Permission Management, shows you how to increase the security of your system by enforcing password restrictions, adjusting the default permissions given to newly created files and directories, and the use of sudo to avoid circulating the root password. How to work with SELinux is also discussed.
Chapter 4, Software Installation Management, provides recipes focused on working with software repositories and installing software. You'll learn how to register the EPEL and Remi repositories, prioritize the repositories packages are installed from, and update your software automatically. You'll also learn how to compile and install software from source code.
Chapter 5, Managing Filesystems and Storage, presents recipes that show you how to set up and work with RAID and with LVM. These services leverage your system's storage to maintain availability, increase reliability, and to keep your data safe against inevitable disk failures.
Chapter 6, Allowing Remote Access, aims to help you provide remote access to your CentOS system in a secure manner. Its recipes cover using SSH, configuring a chroot jail, and tunneling VNC connections through an encrypted SSH tunnel.
Chapter 7, Working with Databases, collects recipes that provide you with the necessary steps to get started with various database services such as MySQL, MongoDB, and OpenLDAP. You'll also learn how to provide backup and redundancy for these services.
Chapter 8, Managing Domains and DNS, takes us into the world of DNS. The recipes show you how to set up a resolving DNS server to decrease latency caused by domain lookups and how to manage your own domain with an authoritative DNS server.
Chapter 9, Managing E-mails, will help you set up your own mail server. The recipes discuss configuring Postfix to provide SMTP services, configuring Dovecot to provide IMAP and POP3 services, and securing these services with TLS. You'll also find instructions on how to set up SpamAssassin to help reduce unsolicited bulk e-mails.
Chapter 10, Managing Web Servers, contains recipes about configuring Apache to server web content. You'll learn how to set up name-based virtual hosting, server pages over HTTPS, and perform URL rewriting. How to set up NGINX as a load balancer is also discussed.
Chapter 11, Safeguarding Against Threats, contains recipes to help protect the investment you've made in your CentOS server. They cover logging, threat monitoring, virus and rootkits, and network backups.
Chapter 12, Virtualization, shows you how CentOS can function as a host operating system to one or more virtualized guests. This allows you to take better advantage of your hardware resources by running multiple operating systems on the same physical system.
To follow the recipes in this book, first and foremost you'll need a system capable of running CentOS 7. The minimum requirements (and maximum capabilities) are documented in the Red Hat Enterprise Linux knowledge base available online at https://access.redhat.com/articles/rhel-limits. In brief, you'll need a system that has the following:
Apart from a system to install CentOS on, you'll also need a copy of the CentOS installation media and a working network connection. You can download a copy directly from https://www.centos.org/download/ or using BitTorrent.
This book is for Linux professionals with basic Unix/Linux functionality experience, perhaps even having set up a server before, who want to advance their knowledge in administering various services.
In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).
To give clear instructions on how to complete a recipe, we use these sections as follows.
This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.
This section contains the steps required to follow the recipe.
This section usually consists of a detailed explanation of what happened in the previous section.
This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.
This section provides helpful links to other useful information for the recipe.
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The repositories' configuration files are found in the /etc/yum.repos.d directory."
A block of code is set as follows:
[sshd] enabled=true bantime=86400 maxretry=5Any command-line input or output is written as follows:
firewall-cmd --zone=public --permanent --add-service=dnsNew terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Select your desired language and click on Continue."
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at [email protected] with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.
This chapter contains the following recipes:
This chapter's recipes focus on getting up and running with CentOS using a variety of installation methods. You'll learn how to perform interactive graphical and text-based installations using Anaconda and perform an unattended installation using Kickstart. You'll also see how to run CentOS in the cloud with Amazon Web Services and in a Docker container image. Most of the recipes in this book take place at the command prompt, but some require a graphical desktop, so we'll finish up with a look at installing the GNOME and KDE Plasma desktops.
In this recipe, you'll learn how to install CentOS using the graphical installer Anaconda. This is the most common way that CentOS is installed, although there are other ways too (some of which are discussed in later recipes). This approach is also the easiest installation method, especially for setting up single-server deployments.
This recipe assumes that you have a copy of the CentOS 7 installation medium. If you don't, visit https://www.centos.org and download a minimal ISO image. You'll also need to make a physical disc from the image. Instructions for burning the ISO image to disc can be found at https://www.centos.org/docs/5/html/CD_burning_howto.html.
If your system doesn't have an optical drive and its BIOS supports booting from a USB device, you can also write the ISO image to a USB stick.
Follow these steps to install CentOS using the graphical installer Anaconda:
The installer is launched from the installation menu
If your system doesn't boot to the installation menu then the drive may not be configured as a boot device. The exact steps to verify and adjust the configuration vary between BIOS vendors, but in general you'll press Esc, F1, F2, or Delete while the system is booting to gain access to the BIOS settings. Then you'll find the list of boot devices and change the order in which each is searched for a boot record.
You can change the language used during the installation process
If your system doesn't have a mouse, you can navigate using Tab to cycle through the input fields, use the arrow keys to select the entry, and press Enter to select or activate an input.
The installation summary screen organizes the installation options into categories
The NETWORK & HOST NAME screen lets us configure the system's network interfaces
The DATE & TIME screen lets us configure the system's time zone
The SOFTWARE SELECTION screen lets us install purpose-based software
Software can easily be installed using yum, so don't worry if you need to install additional software after you already have CentOS up and running. The SOFTWARE SELECTION section is purely for convenience.
The INSTALLATION DESTINATION screen lets us set the disk where CentOS will be installed
You'll need to press the Done button twice to return to the configuration screen if you specify a password that's too weak. If you need help to create a strong password, visit http://www.howtogeek.com/195430/how-to-create-a-strong-password-and-remember-it/.
The ROOT PASSWORD screen lets us set the root account's password
The CREATE USER screen lets us create an unprivileged user account
After installing CentOS using Anaconda in graphical mode, you should now have a basic CentOS 7 system up and running. The process began when we booted the system from the installation disc and selected Install CentOS 7 from the installation menu. The installer's kernel loaded into memory and Anaconda launched in graphical mode.
The NETWORK & HOST NAME screen shows a list of the available network interfaces and basic information about them, for instance, the card's MAC address and transfer rate. By default, the interfaces are configured to use DHCP to obtain their IP address when they are enabled. (Configuring a static IP address is discussed in a later recipe.)
The system's time zone is set on the LOCALIZATION screen. The date and time fields are disabled when NTP is enabled because the values will be set by the NTP service. The system clock's time can drift for many reasons, especially if the system is running on a virtual machine, so allowing NTP to manage the system's time is a good idea to ensure it stays correct. If the date and time fields aren't set by NTP, make sure the Network Time toggle is set ON. You can specify an NTP server by clicking on the button with the gears icon.
The INSTALLATION DESTINATION screen lets us set the installation target for CentOS and specify how the system's drives are partitioned. You can choose to configure the partitions if you have special requirements, but in this recipe I let Anaconda partition the drives automatically.
While Anaconda is busy installing CentOS and any additional software packages you may have requested, it shows us the Configuration screen. This screen gives us the opportunity to set a password for the system's administrative account (root) and create an unprivileged user account. You should only sign in with root when necessary; for your normal day-to-day work you should use your unprivileged account. Anaconda finalizes the installation by configuring the system's boot record and creating the user account.
After the system reboots, the Grub boot loader prompt appears and the arrow keys can be used to select a boot configuration. There's also a timer, so pressing nothing will eventually boot the system using the default configuration.
For more information on installing CentOS 7, refer to the RHEL 7 Installation Guide (https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Installation_Guide).
Next, you'll learn how to install CentOS using Anaconda in text mode. It's recommended that you install CentOS graphically because graphics mode is easier to use and offers more functionality. However, it may not be available when the system lacks sufficient resources to run the installer in graphical mode, for example, if the display adaptor's capabilities are limited or if there is reduced RAM.
This recipe assumes that you have a copy of the CentOS 7 installation medium. If you don't, visit https://www.centos.org to download an ISO image and then burn the image to a disc.
Follow these steps to perform a text-based installation of CentOS:
Anaconda will launch in text mode automatically if your system has less than 768 MB of RAM.
The text-based installation menu categorizes the installation options
The Network settings menu lets us configure the system's network interfaces
The Install Destination menu let us set the installation target and the Autopartioning Options menu lets us specify how the disk will be used
You must confirm you really want to use your password if you provide a password that is too weak.
The Create User menu let us create an unprivileged user account
This recipe showed you how to install CentOS using Anaconda running in text mode. The process began when we booted the system from the installation disc, selected Install CentOS 7 from the installation menu, and added the text option to the boot parameters. The installer's kernel loaded into memory and Anaconda launched in text mode.
The text-based installation is similar to installing CentOS in graphical mode, answering prompts for time zone, software, and networking information. However, Anaconda presents the prompts in a different order when running in text mode and some functionality is missing. For example, we can't perform custom disk partitioning. Nevertheless, text mode enables us to quickly install a basic CentOS system.
For more information on installing CentOS 7, refer to the RHEL 7 Installation Guide (https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Installation_Guide).
If you're planning on installing CentOS on multiple servers, it's more convenient to automate as much of the process as possible. In this recipe, you'll learn how to use Anaconda's kickstart.cfg file to perform an unattended network-based installation.
This recipe requires at least two systems on your network: an existing system running an HTTP server to host the installation files and Kickstart configuration (the recipe Installing Apache HTTP Server and PHP in Chapter 10, Managing Web Servers, shows you how to install Apache) and the target system on which we'll install CentOS. You'll also need the installation media and administrative privileges.
Follow these steps to perform unattended network installations using the Kickstart method:
Anaconda writes the configuration values we provide when performing a graphical or text-based installation to kickstart.cfg. If you plan on installing CentOS on multiple servers, it's more convenient to use the file to provide the interface's answers. The remaining installations can be performed mostly unattended and the systems' configurations will be more consistent.
This recipe showed you how to make the kickstart.cfg file and the CentOS installation files available to other systems over the network, and update the boot command to tell Anaconda where to look for the installation files and prompt responses. Since the software packages are retrieved from the installation server instead of the disc, you can eject the disc as soon as the installation process is underway and use it to begin the next process on your next system.
Of course, kickstart.cfg can be used as a starting point, and you can edit the responses using a text editor to further customize the installations. If you like, you can create multiple kickstart files in the web root, each with a different configuration. Just specify the desired file when you set the installer's boot arguments.
Although you can edit your kickstart files with a basic text editor, dedicated programs exist for editing them as well. Check out Kickstart Configurator (http://landoflinux.com/linux_kickstart_configurator.html).
For more information on coordinating multiple installations of CentOS 7, refer to the following resources:
Amazon Web Services (AWS) is a suite of services hosted within Amazon's network infrastructure which allows companies and individuals take advantage of their computing/storage capacity and world wide data centers. Elastic Cloud Compute (EC2) is a virtualization platform that lets us set up virtual systems on demand, usually to host websites and web apps. This recipe will walk you through the process of setting up a new virtual server running CentOS on the AWS platform.
This recipe assumes that you have an AWS account. You can sign up for one at http://aws.amazon.com. You will need to provide a valid credit card, although you will have access to Amazon's free tier for 12 months.
To set up a new Amazon Machine Instance (AMI) on AWS's EC2 platform, follow these steps:
The EC2 Management Console presents an overview and quick access to resources
Review the list of available images carefully. Many are available, created using different versions of CentOS and with various configurations.
The image selection page presents a filterable list of machine images created by community users
Amazon guides you through selecting an AMI and configuring it in a wizard-like fashion, listing the steps at the top of the page. The Review and Launch buttons jump directly to the last step. You can use the links at the top of the page to go back to an earlier step and adjust the instance's configuration.
Review your instance's resources on the Review Instance Launch page
You're prompted to create a pair of encryption keys the first time you launch the image
This recipe walked you through the steps necessary to spin up a new CentOS AMI on AWS's EC2 platform. To log in to the system, a password or set of encryption keys is needed, and since the primary user account's password is likely to be unknown, we opted to generate a new pair of keys. The private key is downloaded and then used with your SSH client to authenticate your login.
Once you have logged in to your running system, it's worth viewing the contents of the /etc/system-release file to verify the running version of CentOS. Also, you should use the passwd command to change the root account's password if the account isn't already locked down. This is an important security precaution because you don't know who knows the default password. You'll find recipes for managing user permissions in Chapter 3, User and Permission Management, and recipes for managing remote access in Chapter 6, Allowing Remote Access:
After you log in, verify the system's version number and update the root password
Refer to the following resources for more information on working with AMIs on Amazon's EC2 platform:
This recipe shows you how to procure a CentOS base for your development needs using Docker, a virtualization strategy based on the concept of containers. Each container wraps the target software in its own filesystem so that it can run regardless of the operating system on which it's installed. Developers like Docker especially because it helps provide consistency between development and deployment environments.
The recipe assumes that you have a system with Docker installed. If you don't, you can obtain the Docker installer from http://www.docker.com.
Follow these steps to install a CentOS container image from the Docker Registry:
This recipe retrieves the official CentOS container from the Docker Registry using the docker pull command. By providing the version tag (:7), we can make sure we retrieved CentOS 7 as opposed to an earlier (or perhaps newer) version.
Alternatively, Kitematic is the graphical program which lets us search for and retrieve containers from the registry. Simply launch Kitematic and enter CentOS as the search term in the search box. Then, look for the official CentOS repository in the results list.
The default version retrieved by Kitematic is the latest. To specifically select CentOS 7 or a maintenance release, click on the entry's ellipsis button. Set the desired tag and then click on the Create button:
Kitematic displays the results of searching for CentOS
Refer to the following resources for more information about working with Docker:
This recipe shows you how to install the GNOME desktop environment, which provides a graphical user interface (GUI) for working with your CentOS system. Usually, such environments aren't installed on server systems, but it can be convenient sometimes to have one available. For example, an administrator might feel more comfortable updating a system's configuration using graphical programs.
GNOME isn't the only GUI environment available —other popular environments include KDE, XFCE, and Fluxbox. If GNOME isn't your cup of tea, the next recipe shows you how to install KDE.
This recipe requires a CentOS system with a working network connection. Administrative privileges are also required by logging in with the root account.
Follow these steps to install the GNOME desktop environment:
This recipe uses yum to install the GNOME desktop environment. All of the necessary components and dependencies are installed by the GNOME Desktop package group. Package groups saves us time and hassle because they let us install a collection of packages for a common task at the same time instead of individual packages one at a time.
yum groupinstall "GNOME Desktop"Unlike Windows, where the graphical desktop is part of its operating system, Linux systems delegate basic graphics and input handling to a graphics server. This approach is one reason why there are several desktop environments to choose from —it abstracts many of the specifics and provides a common platform on top of which any number of environments can run, both locally and across a network. CentOS's default graphics server is X Window System.
If GNOME is the only desktop environment installed, it'll be run by default when we launch X with startx. However, if more than one desktop is installed, we need to tell X which one we want to run. For GNOME, we provide the path to gnome-session:
startx /usr/bin/gnome-sessionThe GNOME desktop provides a graphical interface for working with the system
The systemd service manager is responsible for starting various servers and processes when the system boots. The systemctl command is our interface to the service manager and can be used to set the default boot target. The default target dictates whether the system boots to a terminal or GUI-based login screen:
systemctl set-default graphical.targetWhen set to graphical, systemd starts X and the GNOME Display Manager when the system boots, which presents us with a graphical login to provide our account details. Once we're authenticated, the desktop session is initiated and we find ourselves at the GNOME desktop.
If you no longer want to boot to the graphical environment, you can set the default target back to multiuser and the system will boot to the terminal-based login screen again:
systemctl set-default multi-user.targetYou can choose which desktop environment you want to use if more than one environment is installed by selecting it from the gear button on the login screen:
You can select your preferred desktop from the login screen
The following resources will provide you with more information about installing graphical desktop environments and using the GNOME desktop:
Separating the graphical interface from the operating system gives users the power to choose the graphical environment they like best. Don't worry if you're not a GNOME fan because there are still many other desktops you can explore! This recipe shows you how to install another popular desktop environment, KDE Plasma Workspaces.
This recipe requires a CentOS system with a working network connection. Administrative privileges are also required by logging in with the root account.
Follow these steps to install the KDE Plasma Workspaces desktop environment:
