Linux Cookbook, 2nd Edition

Book description

This handy cookbook teaches new-to-intermediate Linux users the essential skills necessary to manage a Linux system, using both graphical and command-line tools. Whether you run Linux in embedded, desktop, server, or cloud or virtual environments, the fundamental skills are the same. This book aims to get you up and running quickly, with copy-paste examples.

Carla Schroder provides recipes that cover specific problems, with discussions that explain how each recipe works, as well as references for additional study.

You'll learn how to:

  • Use systemd, the new comprehensive service manager
  • Build simple or complex firewalls with firewalld
  • Set up secure network connections for Linux systems and mobile devices
  • Rescue nonbooting systems
  • Reset lost passwords on Linux and Windows
  • Use dnsmasq to simplify managing your LAN name services
  • Manage users and groups and control access to files
  • Probe your computer hardware and monitor hardware health
  • Manage the GRUB bootloader and multiboot Linux and Windows
  • Keep accurate time across your network with the newest tools
  • Build an internet router/firewall on Raspberry Pi
  • Manage filesystems and partitioning

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Who Should Read This Book
    2. Why I Wrote This Book
    3. Navigating This Book
    4. Conventions Used in This Book
    5. Using Code Examples
    6. O’Reilly Online Learning
    7. How to Contact Us
    8. Acknowledgments
  2. 1. Installing Linux
    1. Booting from Installation Media
    2. Where to Download Linux
    3. Best Linux for Newbies
    4. 1.1. Entering your System BIOS/UEFI Setup
    5. 1.2. Downloading a Linux Installation Image
    6. 1.3. Creating a Linux Installation USB Stick with UNetbootin
    7. 1.4. Creating a Linux Installation DVD with K3b
    8. 1.5. Using the wodim Command to Create a Bootable CD/DVD
    9. 1.6. Creating a Linux Installation USB Stick with the dd Command
    10. 1.7. Trying a Simple Ubuntu Installation
    11. 1.8. Customizing Partitioning
    12. 1.9. Preserving Existing Partitions
    13. 1.10. Customizing Package Selection
    14. 1.11. Multibooting Linux Distributions
    15. 1.12. Dual-boot with Microsoft Windows
    16. 1.13. Recovering an OEM Windows 8 or 10 Product Key
    17. 1.14. Mounting Your ISO Image on Linux
  3. 2. Managing the GRUB Bootloader
    1. 2.1. Rebuilding Your GRUB Configuration File
    2. 2.2. Unhiding a Hidden GRUB Menu
    3. 2.3. Booting to a Different Linux Kernel
    4. 2.4. Understanding GRUB Configuration Files
    5. 2.5. Writing a Minimal GRUB Configuration File
    6. 2.6. Setting a Custom Background for Your GRUB Menu
    7. 2.7. Changing Font Colors in the GRUB Menu
    8. 2.8. Applying a Theme to Your GRUB Menu
    9. 2.9. Rescuing a Nonbooting System from the grub> Prompt
    10. 2.10. Rescuing a Nonbooting System from the grub rescue> Prompt
    11. 2.11. Reinstalling Your GRUB Configuration
  4. 3. Starting, Stopping, Restarting, and Putting Linux into Sleep Modes
    1. 3.1. Shutting Down with systemctl
    2. 3.2. Shutting Down, Timed Shutdowns, and Rebooting with the shutdown Command
    3. 3.3. Shutting Down and Rebooting with halt, reboot, and poweroff
    4. 3.4. Sending Your System into Sleep Modes with systemctl
    5. 3.5. Rebooting Out of Trouble with Ctrl-Alt-Delete
    6. 3.6. Disabling, Enabling, and Configuring Ctrl-Alt-Delete in the Linux Console
    7. 3.7. Creating Scheduled Shutdowns with cron
    8. 3.8. Scheduling Automated Startups with UEFI Wake-Ups
    9. 3.9. Scheduling Automated Startups with RTC Wake-ups
    10. 3.10. Setting Up Remote Wake-Ups with Wake-on-LAN over Wired Ethernet
    11. 3.11. Setting Up Remote Wake-Ups over WiFi (WoWLAN)
  5. 4. Managing Services with systemd
    1. 4.1. Learning if Your Linux Uses systemd
    2. 4.2. Understanding PID 1, the Mother of All Processes
    3. 4.3. Listing Services and Their States with systemctl
    4. 4.4. Querying the Status of Selected Services
    5. 4.5. Starting and Stopping Services
    6. 4.6. Enabling and Disabling Services
    7. 4.7. Stopping Troublesome Processes
    8. 4.8. Managing Runlevels with systemd
    9. 4.9. Diagnosing Slow Startups
  6. 5. Managing Users and Groups
    1. 5.1. Finding a User’s UID and GID
    2. 5.2. Creating a Human User with useradd
    3. 5.3. Creating a System User with useradd
    4. 5.4. Changing the useradd Default Settings
    5. 5.5. Customizing the Documents, Music, Video, Pictures, and Downloads Directories
    6. 5.6. Creating User and System Groups with groupadd
    7. 5.7. Adding Users to Groups with usermod
    8. 5.8. Creating Users with adduser on Ubuntu
    9. 5.9. Creating a System User with adduser on Ubuntu
    10. 5.10. Creating User and System Groups with addgroup
    11. 5.11. Checking Password File Integrity
    12. 5.12. Disabling a User Account
    13. 5.13. Deleting a User with userdel
    14. 5.14. Deleting a User with deluser on Ubuntu
    15. 5.15. Removing a Group with delgroup on Ubuntu
    16. 5.16. Finding and Managing All Files for a User
    17. 5.17. Using su to Be Root
    18. 5.18. Granting Limited Root Powers with sudo
    19. 5.19. Extending the sudo Password Timeout
    20. 5.20. Creating Individual sudoers Configurations
    21. 5.21. Managing the Root User’s Password
    22. 5.22. Changing sudo to Not Ask for the Root Password
  7. 6. Managing Files and Directories
    1. 6.1. Creating Files and Directories
    2. 6.2. Quickly Creating a Batch of Files for Testing
    3. 6.3. Working with Relative and Absolute Filepaths
    4. 6.4. Deleting Files and Directories
    5. 6.5. Copying, Moving, and Renaming Files and Directories
    6. 6.6. Setting File Permissions with chmod’s Octal Notation
    7. 6.7. Setting Directory Permissions with chmod’s Octal Notation
    8. 6.8. Using the Special Modes for Special Use Cases
    9. 6.9. Removing the Special Modes in Octal Notation
    10. 6.10. Setting File Permissions with chmod’s Symbolic Notation
    11. 6.11. Setting the Special Modes with chmod’s Symbolic Notation
    12. 6.12. Setting Permissions in Batches with chmod
    13. 6.13. Setting File and Directory Ownership with chown
    14. 6.14. Changing Ownership on Batches of Files with chown
    15. 6.15. Setting Default Permissions with umask
    16. 6.16. Creating Shortcuts (Soft and Hard Links) to Files and Directories
    17. 6.17. Hiding Files and Directories
  8. 7. Backup and Recovery with rsync and cp
    1. 7.1. Selecting Which Files to Back Up
    2. 7.2. Selecting Files to Restore from Backups
    3. 7.3. Using the Simplest Local Backup Method
    4. 7.4. Automating Simple Local Backups
    5. 7.5. Using rsync for Local Backups
    6. 7.6. Making Secure Remote File Transfers with rsync over SSH
    7. 7.7. Automating rsync Transfers with cron and SSH
    8. 7.8. Excluding Files from Backup
    9. 7.9. Including Selected Files to Backup
    10. 7.10. Managing Includes with a Simple Include File
    11. 7.11. Managing Includes and Excludes with an Exclude File
    12. 7.12. Limiting rsync’s Bandwidth Use
    13. 7.13. Building an rsyncd Backup Server
    14. 7.14. Limiting Access to rsyncd Modules
    15. 7.15. Creating a Message of the Day for rsyncd
  9. 8. Managing Disk Partitioning with parted
    1. Overview
    2. 8.1. Unmounting Your Partitions Before Using parted
    3. 8.2. Choosing the Command Mode for parted
    4. 8.3. Viewing Your Existing Disks and Partitions
    5. 8.4. Creating GPT Partitions on a Nonbooting Disk
    6. 8.5. Creating Partitions for Installing Linux
    7. 8.6. Removing Partitions
    8. 8.7. Recovering a Deleted Partition
    9. 8.8. Increasing Partition Size
    10. 8.9. Shrinking a Partition
  10. 9. Managing Partitions and Filesystems with GParted
    1. 9.1. Viewing Partitions, Filesystems, and Free Space
    2. 9.2. Creating a New Partition Table
    3. 9.3. Deleting a Partition
    4. 9.4. Creating a New Partition
    5. 9.5. Deleting a Filesystem Without Deleting the Partition
    6. 9.6. Recovering a Deleted Partition
    7. 9.7. Resizing Partitions
    8. 9.8. Moving a Partition
    9. 9.9. Copying a Partition
    10. 9.10. Managing Filesystems with GParted
  11. 10. Getting Detailed Information About Your Computer Hardware
    1. 10.1. Collecting Hardware Information with lshw
    2. 10.2. Filtering lshw Output
    3. 10.3. Detecting Hardware, Including Displays and RAID Devices, with hwinfo
    4. 10.4. Detecting PCI Hardware with lspci
    5. 10.5. Understanding lspci Output
    6. 10.6. Filtering lspci Output
    7. 10.7. Using lspci to Identify Kernel Modules
    8. 10.8. Using lsusb to List USB Devices
    9. 10.9. Listing Partitions and Hard Disks with lsblk
    10. 10.10. Getting CPU Information
    11. 10.11. Identifying Your Hardware Architecture
  12. 11. Creating and Managing Filesystems
    1. Filesystem Overview
    2. 11.1. Listing Supported Filesystems
    3. 11.2. Identifying Your Existing Filesystems
    4. 11.3. Resizing Filesystems
    5. 11.4. Deleting Filesystems
    6. 11.5. Using a New Filesystem
    7. 11.6. Creating Automatic Filesystem Mounts
    8. 11.7. Creating Ext4 Filesystems
    9. 11.8. Configuring the Ext4 Journal Mode
    10. 11.9. Finding Which Journal Your Ext4 Filesystem Is Attached To
    11. 11.10. Improving Performance with an External Journal for Ext4
    12. 11.11. Freeing Space from Reserved Blocks on Ext4 Filesystems
    13. 11.12. Creating a New XFS Filesystem
    14. 11.13. Resizing an XFS Filesystem
    15. 11.14. Creating an exFAT Filesystem
    16. 11.15. Creating FAT16 and FAT32 Filesystems
    17. 11.16. Creating a Btrfs Filesystem
  13. 12. Secure Remote Access with OpenSSH
    1. 12.1. Installing OpenSSH Server
    2. 12.2. Generating New Host Keys
    3. 12.3. Configuring Your OpenSSH Server
    4. 12.4. Checking Configuration Syntax
    5. 12.5. Setting Up Password Authentication
    6. 12.6. Retrieving a Key Fingerprint
    7. 12.7. Using Public Key Authentication
    8. 12.8. Managing Multiple Public Keys
    9. 12.9. Changing a Passphrase
    10. 12.10. Automatic Passphrase Management with Keychain
    11. 12.11. Using Keychain to Make Passphrases Available to Cron
    12. 12.12. Tunneling an X Session Securely over SSH
    13. 12.13. Opening an SSH Session and Running a Command in One Line
    14. 12.14. Mounting Entire Remote Filesystems with sshfs
    15. 12.15. Customizing the Bash Prompt for SSH
    16. 12.16. Listing Supported Encryption Algorithms
  14. 13. Secure Remote Access with OpenVPN
    1. OpenVPN Overview
    2. 13.1. Installing OpenVPN, Server and Client
    3. 13.2. Setting Up a Simple Connection Test
    4. 13.3. Setting Up Easy Encryption with Static Keys
    5. 13.4. Installing EasyRSA to Manage Your PKI
    6. 13.5. Creating a PKI
    7. 13.6. Customizing EasyRSA Default Options
    8. 13.7. Creating and Testing Server and Client Configurations
    9. 13.8. Controlling OpenVPN with systemctl
    10. 13.9. Distributing Client Configurations More Easily with .ovpn Files
    11. 13.10. Hardening Your OpenVPN Server
    12. 13.11. Configuring Networking
  15. 14. Building a Linux Firewall with firewalld
    1. firewalld Overview
    2. 14.1. Querying Which Firewall Is Running
    3. 14.2. Installing firewalld
    4. 14.3. Finding Your firewalld Version
    5. 14.4. Configuring iptables or nftables as the firewalld Backend
    6. 14.5. Listing All Zones and All Services Managed by Each Zone
    7. 14.6. Listing and Querying Services
    8. 14.7. Selecting and Setting Zones
    9. 14.8. Changing the Default firewalld Zone
    10. 14.9. Customizing firewalld Zones
    11. 14.10. Creating a New Zone
    12. 14.11. Integrating NetworkManager and firewalld
    13. 14.12. Allowing or Blocking Specific Ports
    14. 14.13. Blocking IP Addresses with Rich Rules
    15. 14.14. Changing a Zone Default Target
  16. 15. Printing on Linux
    1. Overview
    2. 15.1. Using the CUPS Web Interface
    3. 15.2. Installing a Locally Attached Printer
    4. 15.3. Giving Printers Useful Names
    5. 15.4. Installing a Network Printer
    6. 15.5. Using Driverless Printing
    7. 15.6. Sharing Nonnetworked Printers
    8. 15.7. Correcting the “Forbidden” Error Message
    9. 15.8. Installing Printer Drivers
    10. 15.9. Modifying an Installed Printer
    11. 15.10. Saving Documents by Printing to a PDF File
    12. 15.11. Troubleshooting
  17. 16. Managing Local Name Services with Dnsmasq and the hosts File
    1. 16.1. Simple Name Resolution with /etc/hosts
    2. 16.2. Using /etc/hosts for Testing and Blocking Annoyances
    3. 16.3. Finding All DNS and DHCP Servers on Your Network
    4. 16.4. Installing Dnsmasq
    5. 16.5. Making systemd-resolved and NetworkManager Play Nice with Dnsmasq
    6. 16.6. Configuring Dnsmasq for LAN DNS
    7. 16.7. Configuring firewalld to Allow DNS and DHCP
    8. 16.8. Testing Your Dnsmasq Server from a Client Machine
    9. 16.9. Managing DHCP with Dnsmasq
    10. 16.10. Advertising Important Services over DHCP
    11. 16.11. Creating DHCP Zones for Subnets
    12. 16.12. Assigning Static IP Addresses from DHCP
    13. 16.13. Configuring DHCP Clients for Automatic DNS Entries
    14. 16.14. Managing Dnsmasq Logging
    15. 16.15. Configuring Wildcard Domains
  18. 17. Keeping Time with ntpd, chrony, and timesyncd
    1. 17.1. Finding Which NTP Client Is on Your Linux System
    2. 17.2. Using timesyncd for Simple Time Synchronization
    3. 17.3. Setting Time Manually with timedatectl
    4. 17.4. Using chrony for Your NTP Client
    5. 17.5. Using chrony as a LAN Time Server
    6. 17.6. Viewing chrony Statistics
    7. 17.7. Using ntpd for Your NTP Client
    8. 17.8. Using ntpd for Your NTP Server
    9. 17.9. Managing Time Zones with timedatectl
    10. 17.10. Managing Time Zones Without timedatectl
  19. 18. Building an Internet Firewall/Router on Raspberry Pi
    1. Overview
    2. 18.1. Starting and Shutting Down Raspberry Pi
    3. 18.2. Finding Hardware and How-Tos
    4. 18.3. Cooling the Raspberry Pi
    5. 18.4. Installing Raspberry Pi OS with Imager and dd
    6. 18.5. Installing Raspberry Pi with NOOBS
    7. 18.6. Connecting to a Video Display Without HDMI
    8. 18.7. Booting into Recovery Mode
    9. 18.8. Adding a Second Ethernet Interface
    10. 18.9. Setting Up an Internet Connection Sharing Firewall with firewalld
    11. 18.10. Running Your Raspberry Pi Headless
    12. 18.11. Building a DNS/DHCP Server with Raspberry Pi
  20. 19. System Rescue and Recovery with SystemRescue
    1. 19.1. Creating Your SystemRescue Bootable Device
    2. 19.2. Getting Started with SystemRescue
    3. 19.3. Understanding SystemRescue’s Two Boot Screens
    4. 19.4. Understanding SystemRescue’s Boot Options
    5. 19.5. Identifying Filesystems
    6. 19.6. Resetting a Linux Root Password
    7. 19.7. Enabling SSH in SystemRescue
    8. 19.8. Copying Files over the Network with scp and sshfs
    9. 19.9. Repairing GRUB from SystemRescue
    10. 19.10. Resetting a Windows Password
    11. 19.11. Rescuing a Failing Hard Disk with GNU ddrescue
    12. 19.12. Managing Partitions and Filesystems from SystemRescue
    13. 19.13. Creating a Data Partition on Your SystemRescue USB Drive
    14. 19.14. Preserving Changes in SystemRescue
  21. 20. Troubleshooting a Linux PC
    1. Overview
    2. 20.1. Finding Useful Information in Logfiles
    3. 20.2. Configuring journald
    4. 20.3. Building a Logging Server with systemd
    5. 20.4. Monitoring Temperatures, Fans, and Voltages with lm-sensors
    6. 20.5. Adding a Graphical Interface to lm-sensors
    7. 20.6. Monitoring Hard Disk Health with smartmontools
    8. 20.7. Configuring smartmontools to Send Email Reports
    9. 20.8. Diagnosing a Sluggish System with top
    10. 20.9. Viewing Selected Processes in top
    11. 20.10. Escaping from a Frozen Graphical Desktop
    12. 20.11. Troubleshooting Hardware
  22. 21. Troubleshooting Networks
    1. Diagnostic Hardware
    2. 21.1. Testing Connectivity with ping
    3. 21.2. Profiling Your Network with fping and nmap
    4. 21.3. Finding Duplicate IP Addresses with arping
    5. 21.4. Testing HTTP Throughput and Latency with httping
    6. 21.5. Using mtr to Find Troublesome Routers
  23. Software Management Cheatsheets
    1. Package Management Commands
    2. Managing Software on Ubuntu
    3. Managing Software on Fedora
    4. Managing Software on openSUSE
  24. Index

Product information

  • Title: Linux Cookbook, 2nd Edition
  • Author(s): Carla Schroder
  • Release date: August 2021
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492087168