📄 Page
1
systemd for Linux SysAdmins All You Need to Know About the systemd Suite for Linux Users — David Both
📄 Page
2
systemd for Linux SysAdmins All You Need to Know About the systemd Suite for Linux Users David Both
📄 Page
3
systemd for Linux SysAdmins: All You Need to Know About the systemd Suite for Linux Users ISBN-13 (pbk): 979-8-8688-1327-6 ISBN-13 (electronic): 979-8-8688-1328-3 https://doi.org/10.1007/979-8-8688-1328-3 Copyright © 2025 by David Both This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: James Robinson-Prior Editorial Project Manager: Jacob Shmulewitz Cover image designed by David Both Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 New York Plaza, New York, NY 10004. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a Delaware LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail booktranslations@springernature.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub. For more detailed information, please visit https://www.apress. com/gp/services/source-code. If disposing of this product, please recycle the paper David Both Raleigh, NC, USA
📄 Page
5
v Table of Contents About the Author ��������������������������������������������������������������������������������xv About the Technical Reviewer ����������������������������������������������������������xvii Acknowledgments �����������������������������������������������������������������������������xix Introduction ���������������������������������������������������������������������������������������xxi Chapter 1: Learning to Love systemd ���������������������������������������������������1 Objectives �������������������������������������������������������������������������������������������������������������1 Overview ���������������������������������������������������������������������������������������������������������������2 Linux Boot �������������������������������������������������������������������������������������������������������������2 systemd Controversy ��������������������������������������������������������������������������������������������3 Why I Prefer SystemV ��������������������������������������������������������������������������������������4 Why I Prefer systemd ��������������������������������������������������������������������������������������5 The Real Issue �������������������������������������������������������������������������������������������������5 Replacing SystemV ������������������������������������������������������������������������������������������6 systemd Tasks �������������������������������������������������������������������������������������������������������7 More Data for the Admin �������������������������������������������������������������������������������������11 systemd Standardizes Configuration ������������������������������������������������������������������13 Architecture ��������������������������������������������������������������������������������������������������������13 systemd As PID 1 ������������������������������������������������������������������������������������������������14 Preparation ���������������������������������������������������������������������������������������������������������20 Summary�������������������������������������������������������������������������������������������������������������21 Exercises �������������������������������������������������������������������������������������������������������������22
📄 Page
6
vi Chapter 2: Linux Boot and Startup �����������������������������������������������������23 Objectives �����������������������������������������������������������������������������������������������������������23 Overview �������������������������������������������������������������������������������������������������������������24 Hardware Boot ����������������������������������������������������������������������������������������������������25 The Boot Sector ���������������������������������������������������������������������������������������������26 Linux Boot �����������������������������������������������������������������������������������������������������������28 GRUB �������������������������������������������������������������������������������������������������������������28 The GUID Partition Table ��������������������������������������������������������������������������������29 The Kernel �����������������������������������������������������������������������������������������������������33 Linux Startup �������������������������������������������������������������������������������������������������������34 systemd ���������������������������������������������������������������������������������������������������������36 Graphical Login ���������������������������������������������������������������������������������������������������46 Display Manager ��������������������������������������������������������������������������������������������47 Window Manager �������������������������������������������������������������������������������������������48 How Do I Deal with All These Choices? ���������������������������������������������������������50 Console Login ������������������������������������������������������������������������������������������������������57 Virtual Consoles ���������������������������������������������������������������������������������������������59 How Logins Work ������������������������������������������������������������������������������������������������61 CLI Login Screen ��������������������������������������������������������������������������������������������63 GUI Login Screen �������������������������������������������������������������������������������������������64 Summary�������������������������������������������������������������������������������������������������������������65 Exercises �������������������������������������������������������������������������������������������������������������66 Chapter 3: Understanding Linux Startup with systemd ����������������������67 Objectives �����������������������������������������������������������������������������������������������������������67 Overview �������������������������������������������������������������������������������������������������������������68 Exploring Linux Startup with systemd ����������������������������������������������������������������68 Targets ����������������������������������������������������������������������������������������������������������������73 Table of ConTenTs
📄 Page
7
vii Exploring the Current Target ��������������������������������������������������������������������������76 Switching to a Different Target ����������������������������������������������������������������������78 Changing the Default Target ��������������������������������������������������������������������������79 Summary�������������������������������������������������������������������������������������������������������������85 Exercises �������������������������������������������������������������������������������������������������������������85 Chapter 4: How to Manage Startup Using systemd ����������������������������87 Objectives �����������������������������������������������������������������������������������������������������������87 Overview �������������������������������������������������������������������������������������������������������������88 Preparation ���������������������������������������������������������������������������������������������������������88 The Program �������������������������������������������������������������������������������������������������������89 The Service Unit ��������������������������������������������������������������������������������������������������90 Creating the Service Unit �������������������������������������������������������������������������������93 Start the Service ��������������������������������������������������������������������������������������������96 Reboot—Finally �������������������������������������������������������������������������������������������104 Changing the Sequence�������������������������������������������������������������������������������106 Ensure a Service Starts After the Network Is Running ��������������������������������111 Summary�����������������������������������������������������������������������������������������������������������114 Exercises �����������������������������������������������������������������������������������������������������������115 Chapter 5: Manage systemd Units with systemctl ���������������������������117 Objectives ���������������������������������������������������������������������������������������������������������117 Overview �����������������������������������������������������������������������������������������������������������117 Preparation �������������������������������������������������������������������������������������������������������118 systemd Suite ���������������������������������������������������������������������������������������������������118 Practical Structure ���������������������������������������������������������������������������������������119 systemctl �����������������������������������������������������������������������������������������������������121 Service Units �����������������������������������������������������������������������������������������������128 Table of ConTenTs
📄 Page
8
viii Mounts the Old Way �������������������������������������������������������������������������������������133 Creating a Mount Unit ����������������������������������������������������������������������������������139 Summary�����������������������������������������������������������������������������������������������������������142 Exercises �����������������������������������������������������������������������������������������������������������142 Chapter 6: Control Your Computer Time and Date with systemd ������145 Objectives ���������������������������������������������������������������������������������������������������������145 Overview �����������������������������������������������������������������������������������������������������������146 Why Time Is Important to Computers ����������������������������������������������������������������147 Multiple Times ���������������������������������������������������������������������������������������������������147 NTP��������������������������������������������������������������������������������������������������������������������148 The NTP Server Hierarchy ���������������������������������������������������������������������������149 NTP Implementation Options �����������������������������������������������������������������������149 NTP Client Configuration������������������������������������������������������������������������������151 NTP Server Pools �����������������������������������������������������������������������������������������153 Chrony ���������������������������������������������������������������������������������������������������������������154 Using chronyc from the Command Line ������������������������������������������������������155 Chronyc As an Interactive Tool ���������������������������������������������������������������������162 systemd-timesync ��������������������������������������������������������������������������������������������164 Configure systemd-timesyncd ���������������������������������������������������������������������169 Start timesyncd �������������������������������������������������������������������������������������������171 Set the Hardware Clock �������������������������������������������������������������������������������172 Do You Really Need RTC? ����������������������������������������������������������������������������175 Summary�����������������������������������������������������������������������������������������������������������176 Exercises �����������������������������������������������������������������������������������������������������������177 Table of ConTenTs
📄 Page
9
ix Chapter 7: Analyzing systemd Calendar and Time Spans ����������������179 Objectives ���������������������������������������������������������������������������������������������������������179 Overview �����������������������������������������������������������������������������������������������������������180 Definitions ���������������������������������������������������������������������������������������������������������180 Absolute Timestamp ������������������������������������������������������������������������������������180 Accuracy ������������������������������������������������������������������������������������������������������181 Calendar Event ��������������������������������������������������������������������������������������������181 Time Span ����������������������������������������������������������������������������������������������������182 Calendar Event Expressions ������������������������������������������������������������������������182 Exploring systemd Time Syntax �������������������������������������������������������������������183 Summary�����������������������������������������������������������������������������������������������������������193 Exercises �����������������������������������������������������������������������������������������������������������193 Chapter 8: Using systemd Timers �����������������������������������������������������195 Objectives ���������������������������������������������������������������������������������������������������������195 Overview �����������������������������������������������������������������������������������������������������������195 System Maintenance Timers �����������������������������������������������������������������������������196 Creating a Timer ������������������������������������������������������������������������������������������������202 Timer Accuracy �������������������������������������������������������������������������������������������������207 Timer Types �������������������������������������������������������������������������������������������������������210 OnCalendar Event Expressions �������������������������������������������������������������������������212 Superfluous Timers �������������������������������������������������������������������������������������������214 Summary�����������������������������������������������������������������������������������������������������������215 Exercises �����������������������������������������������������������������������������������������������������������216 Table of ConTenTs
📄 Page
10
x Chapter 9: Using systemd Journals ��������������������������������������������������219 Objectives ���������������������������������������������������������������������������������������������������������219 Overview �����������������������������������������������������������������������������������������������������������219 The Journal �������������������������������������������������������������������������������������������������������221 The systemd Journal Service ����������������������������������������������������������������������������224 Configuration�����������������������������������������������������������������������������������������������������225 About that Binary Data Format… ���������������������������������������������������������������������226 The journalctl Command �����������������������������������������������������������������������������������229 Commonly Used Options �����������������������������������������������������������������������������238 Other Interesting Options�����������������������������������������������������������������������������240 Journal Files ������������������������������������������������������������������������������������������������242 Adding Your Own Journal Entries ����������������������������������������������������������������243 Journal Storage Usage ��������������������������������������������������������������������������������������244 Journal File Rotation �����������������������������������������������������������������������������������246 Summary�����������������������������������������������������������������������������������������������������������253 Exercises �����������������������������������������������������������������������������������������������������������253 Chapter 10: Managing the Firewall with firewalld ���������������������������255 Objectives ���������������������������������������������������������������������������������������������������������255 Introduction �������������������������������������������������������������������������������������������������������256 Ports ������������������������������������������������������������������������������������������������������������256 Firewall Rules ���������������������������������������������������������������������������������������������������259 Firewall Tools ����������������������������������������������������������������������������������������������������261 Block (Almost) Everything ���������������������������������������������������������������������������������262 Crunchy on the Outside �������������������������������������������������������������������������������������263 firewalld ������������������������������������������������������������������������������������������������������������263 firewalld Zones ��������������������������������������������������������������������������������������������264 Using --reload ����������������������������������������������������������������������������������������������288 Table of ConTenTs
📄 Page
11
xi Zone Files ����������������������������������������������������������������������������������������������������289 Panic Mode ��������������������������������������������������������������������������������������������������293 firewall-config GUI ���������������������������������������������������������������������������������������295 nftables �������������������������������������������������������������������������������������������������������������295 Outbound Blocking ��������������������������������������������������������������������������������������������297 Fail2Ban ������������������������������������������������������������������������������������������������������������298 Cleanup �������������������������������������������������������������������������������������������������������������302 Summary�����������������������������������������������������������������������������������������������������������302 Exercises �����������������������������������������������������������������������������������������������������������303 Chapter 11: Resource Management with cgroups ���������������������������305 Objectives ���������������������������������������������������������������������������������������������������������305 Introduction �������������������������������������������������������������������������������������������������������305 Using cgroups for Process Management ����������������������������������������������������������306 Exploring the Cgroup Hierarchy ������������������������������������������������������������������������311 Managing cgroups with systemd ����������������������������������������������������������������������316 Summary�����������������������������������������������������������������������������������������������������������317 Exercises �����������������������������������������������������������������������������������������������������������318 Chapter 12: Using systemd- resolved Name Service �������������������������319 Objectives ���������������������������������������������������������������������������������������������������������319 Introduction �������������������������������������������������������������������������������������������������������320 How a Name Search Works �������������������������������������������������������������������������������320 resolv�conf ���������������������������������������������������������������������������������������������������������322 Historical Usage�������������������������������������������������������������������������������������������323 Current Usage ����������������������������������������������������������������������������������������������324 Name Service Strategies ����������������������������������������������������������������������������������326 The /etc/hosts File ���������������������������������������������������������������������������������������326 Table of ConTenTs
📄 Page
12
xii mDNS ����������������������������������������������������������������������������������������������������������329 nss-DNS �������������������������������������������������������������������������������������������������������337 systemd-resolved�service ���������������������������������������������������������������������������343 Fedora Name Resolution Fails When Using systemd-resolved �������������������������345 Determining the Problem ����������������������������������������������������������������������������346 Resolving the Problem ���������������������������������������������������������������������������������349 Concluding Thoughts About nsswitch ����������������������������������������������������������355 Summary�����������������������������������������������������������������������������������������������������������356 Exercises �����������������������������������������������������������������������������������������������������������356 Chapter 13: Replacing rc�local in systemd ���������������������������������������357 Objectives ���������������������������������������������������������������������������������������������������������357 Introduction �������������������������������������������������������������������������������������������������������357 Boot vs� Startup �������������������������������������������������������������������������������������������358 Local Startup �����������������������������������������������������������������������������������������������������359 Create the Executable File ���������������������������������������������������������������������������359 Create the systemd Service ������������������������������������������������������������������������361 Enable the New Service ������������������������������������������������������������������������������363 Revise mystartup�sh ������������������������������������������������������������������������������������364 Final Test �����������������������������������������������������������������������������������������������������365 A Temporary Option �������������������������������������������������������������������������������������������367 Cleanup �������������������������������������������������������������������������������������������������������������368 Summary�����������������������������������������������������������������������������������������������������������368 Exercises �����������������������������������������������������������������������������������������������������������368 Chapter 14: Getting More Out of the Journal ������������������������������������371 Objectives ���������������������������������������������������������������������������������������������������������371 Introduction �������������������������������������������������������������������������������������������������������371 Options to Narrow Search Results ��������������������������������������������������������������������372 Table of ConTenTs
📄 Page
13
xiii A Troubleshooting Example �������������������������������������������������������������������������377 Determining the Problem ����������������������������������������������������������������������������377 First Solution �����������������������������������������������������������������������������������������������380 The Better Solution ��������������������������������������������������������������������������������������383 Summary�����������������������������������������������������������������������������������������������������������385 Exercises �����������������������������������������������������������������������������������������������������������386 Chapter 15: Analyzing systemd Startup and Configuration �������������387 Objectives ���������������������������������������������������������������������������������������������������������387 Overview �����������������������������������������������������������������������������������������������������������388 Linux Startup �����������������������������������������������������������������������������������������������������388 Basic Analysis ����������������������������������������������������������������������������������������������389 The Blame Game �����������������������������������������������������������������������������������������390 Critical Chain �����������������������������������������������������������������������������������������������391 System State �����������������������������������������������������������������������������������������������396 Analytic Graphs �������������������������������������������������������������������������������������������399 Conditionals �������������������������������������������������������������������������������������������������401 Listing Configuration Files ��������������������������������������������������������������������������������402 Unit File Verification ������������������������������������������������������������������������������������������405 Security �������������������������������������������������������������������������������������������������������������405 Summary�����������������������������������������������������������������������������������������������������������407 Exercises �����������������������������������������������������������������������������������������������������������408 Chapter 16: Why I Support the systemd Plan to Take Over the World ������������������������������������������������������������������������������������������409 Introduction �������������������������������������������������������������������������������������������������������409 More Data for the Admin �����������������������������������������������������������������������������������411 systemd Standardizes Configuration ����������������������������������������������������������������416 Sometimes, the Pain �����������������������������������������������������������������������������������������416 Table of ConTenTs
📄 Page
14
xiv Five Reasons SysAdmins Love systemd �����������������������������������������������������������417 Boot Management ���������������������������������������������������������������������������������������418 Log Reviews ������������������������������������������������������������������������������������������������419 Service Management�����������������������������������������������������������������������������������420 Timers ����������������������������������������������������������������������������������������������������������422 Targets ���������������������������������������������������������������������������������������������������������423 Taking Control with systemd �����������������������������������������������������������������������423 Final Exercise ����������������������������������������������������������������������������������������������������424 Appendix A: systemd Resources ������������������������������������������������������425 Index �������������������������������������������������������������������������������������������������429 Table of ConTenTs
📄 Page
15
xv David Both, SCSA, RHCT, RHCE, is an open source software and Linux advocate, trainer, writer, and speaker. He has been working with Linux and open source software since 1996 and has been working with computers for over 50 years. He is a strong proponent of and evangelist for the “Linux Philosophy for System Administrators.” He has taught RHCE classes for Red Hat and has worked at MCI Worldcom, Cisco, and the State of North Carolina. He has taught classes on Linux ranging from Lunch'n'Learns to full five-day courses. David has written articles for magazines including Linux Magazine, Linux Journal, and OS/2 Magazine back when there was such a thing. David wrote for Opensource.com (OSDC) before it was closed by Red Hat. He now writes for Both.org while working with a core group of OSDC alumni to recreate the community that made OSDC so successful. David has published eight previous books with Apress, including The Linux Philosophy for SysAdmins, August 2018; a three-volume self- study training course, Using and Administering Linux – Zero to SysAdmin, released in December 2019; and Linux for Small Business Owners, released in late 2022. The second edition of Using and Administering Linux – Zero to SysAdmin was released in 2023. He has found some interesting and unusual ways of problem-solving, including sitting on one computer on which he was working. About the Author
📄 Page
16
xvii About the Technical Reviewer Seth Kenlon is a UNIX and Linux geek, open source enthusiast, and tabletop gamer. Between gigs in the film industry and the tech industry (not necessarily exclusive of one another), he designs games and hacks on Java and Lua. Visit gopher://ada.info- underground.net:70/1/klaatu or http://seth.kenlon.com for more information.
📄 Page
17
xix First, I need to say a special thanks to my awesome wife, Alice, who has been my head cheerleader. I could not have done this without you, my best friend, my sweetie. I’d like to thank my editor, James Robinson-Prior, for seeing the need for this book. I’d also like to thank Shobana Srinivasan, Gryffin Winkler, and Jacob Shmulewitz for their efforts in making this book a reality. I’d also like to thank all the people in production—the ones who take files full of words and produce the finished book in multiple formats. You always make my words look good. Acknowledgments
📄 Page
18
xxi Introduction Explore the world of systemd, the modern but controversial replacement for init and SystemV init scripts. systemd can evoke a wide range of reactions from SysAdmins and others responsible for keeping Linux systems up and running. The fact that systemd is taking over so many system management tasks in modern Linux systems has engendered pushback and discord among some groups of developers and SysAdmins. Yet that wide reach of systemd is the very reason it’s so popular with others. This book will help you to understand systemd’s strengths and weaknesses and why there’s no truth in the myth that systemd is a monolithic monstrosity. systemd is the mother of all processes and is also responsible for bringing the Linux host up to a state in which productive work can be done. You’ll learn about the functions assumed by systemd, which is far more extensive than the old init program, and how it manages many aspects of a running Linux host, including • Mounting filesystems • Managing hardware • Creating new systemd services and understanding existing ones • Creating timers that trigger system maintenance events • Starting and managing the system services that are required to have a productive Linux host
📄 Page
19
xxii • Using the systemd journal to access critical performance and problem-solving information • Why the systemd plan to take over the world is actually a good thing This book introduces you to systemd with an overview and exploration of the controversy surrounding it. We’ll cover systemd’s major components and how they can provide insight into Linux startup, as well as how to manage the tools and services required to operate and maintain a running Linux computer. This book will help demystify systemd. You’ll learn what it is, what it does, and how to use it to keep Linux systems up and running. You’ll explore the major functional components of systemd with real-world examples to illustrate their typical usage. You’ll also learn pragmatic work- arounds, hints, and tricks to minimize the issues that systemd does have and ensure you have maximized your system’s functionality and security. You’ll learn to manage each of the major functional components of systemd and learn from real-world examples to illustrate their typical usage by SysAdmins. This book is intended for Linux system administrators (SysAdmins) who need to or are already in the process of switching from SystemV to systemd. It’s also intended for SysAdmins with more systemd experience but who want to improve their knowledge and skills with systemd. This book is not for anyone who is not a SysAdmin. If you’re only interested in getting your work done and have no interest in what’s happening under the covers, if you call upon others to fix your computer when something goes awry, this is not the book for you. InTroduCTIon
📄 Page
20
1© David Both 2025 D. Both, systemd for Linux SysAdmins, https://doi.org/10.1007/979-8-8688-1328-3_1 CHAPTER 1 Learning to Love systemd Objectives In this chapter, you will learn • To differentiate the stages of the Linux boot process • About the controversy surrounding SystemV vs. systemd • To list the functions of systemd • Why systemd is an improvement over SystemV startup and init services • How the SystemV runlevels compare to systemd targets • The functions of systemd during boot • The functions of systemd while the system is up and running • To list the dependencies of systemd targets • To prepare for the experiments that start in Chapter 2