Linux Server Administration Complete Guide Part 3: Package Management and Services
Mastering Software Installation and Service Operations
Linux Server Administration Complete Guide Series
Part 2: Users and Permissions | Part 3: Package Management and Services (Current) | Part 4: Network and Firewall
Introduction: The Importance of Software Management
Installing, updating, and removing software are daily tasks in server administration. Linux handles dependency resolution, version management, and security updates efficiently through package management systems. Additionally, systemd manages service startup, shutdown, and automatic startup.
1. Understanding Package Management Systems
1.1 What is a Package?
A package is a compressed archive containing software, configuration files, and dependency information. Package managers automate installation, updates, and removal.
1.2 Major Package Management Systems
| Distribution Family | Package Format | Low-level Tool | High-level Tool |
|---|---|---|---|
| Debian/Ubuntu | .deb | dpkg | apt, apt-get |
| RHEL/CentOS/Fedora | .rpm | rpm | yum, dnf |
| Arch Linux | .pkg.tar.zst | pacman | pacman |
2. APT (Debian/Ubuntu Family)
2.1 Updating Package Lists
# Update package lists (required before installation)
sudo apt update
# Upgrade installed packages
sudo apt upgrade
# Full system upgrade (includes dependency changes)
sudo apt full-upgrade
# Remove unnecessary packages
sudo apt autoremove
2.2 Searching and Getting Package Info
# Search for packages
apt search nginx
# View package information
apt show nginx
# List installed packages
apt list --installed
# Check if specific package is installed
apt list --installed | grep nginx
2.3 Installing/Removing Packages
# Install package
sudo apt install nginx
# Install multiple packages
sudo apt install nginx mysql-server php-fpm
# Install specific version
sudo apt install nginx=1.18.0-0ubuntu1
# Remove package (keep config files)
sudo apt remove nginx
# Completely remove package (including config files)
sudo apt purge nginx
# Install .deb file directly
sudo dpkg -i package.deb
sudo apt install -f # Resolve dependencies
2.4 Repository Management
# Repository list locations
/etc/apt/sources.list
/etc/apt/sources.list.d/
# Add PPA (Ubuntu)
sudo add-apt-repository ppa:ondrej/php
sudo apt update
# Remove PPA
sudo add-apt-repository --remove ppa:ondrej/php
3. YUM/DNF (RHEL/CentOS/Fedora Family)
3.1 Basic Commands
# Update package lists and upgrade
sudo yum update # CentOS 7
sudo dnf update # CentOS 8+, Fedora
# Search packages
yum search nginx
dnf search nginx
# Package information
yum info nginx
dnf info nginx
# Install package
sudo yum install nginx
sudo dnf install nginx
# Remove package
sudo yum remove nginx
sudo dnf remove nginx
3.2 Group Packages
# List groups
dnf group list
# Install Development Tools group
sudo dnf groupinstall "Development Tools"
# Remove group
sudo dnf groupremove "Development Tools"
3.3 Repository Management
# Repository list location
/etc/yum.repos.d/
# Add EPEL repository
sudo dnf install epel-release
# List repositories
dnf repolist
# Install from specific repository
sudo dnf install --enablerepo=epel package-name
4. Package Management Best Practices
4.1 Security Updates Only
# Ubuntu/Debian
sudo apt upgrade --only-upgrade
# RHEL/CentOS
sudo yum update --security
sudo dnf update --security
4.2 Holding Package Versions
# Ubuntu/Debian - Hold version
sudo apt-mark hold nginx
sudo apt-mark unhold nginx
apt-mark showhold
# RHEL/CentOS - Exclude packages
# In /etc/yum.conf or /etc/dnf/dnf.conf
exclude=nginx*
4.3 Cache Management
# APT cache cleanup
sudo apt clean # Delete downloaded package files
sudo apt autoclean # Delete only old versions
# YUM/DNF cache cleanup
sudo yum clean all
sudo dnf clean all
5. systemd and Service Management
5.1 What is systemd?
systemd is the Linux init system and system management daemon. It handles service start/stop, boot-time auto-start, logging, and dependency management.
5.2 Basic Service Control
# Check service status
sudo systemctl status nginx
# Start/stop/restart service
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
# Reload configuration (no downtime)
sudo systemctl reload nginx
# Restart or reload (reload if supported)
sudo systemctl reload-or-restart nginx
5.3 Boot-time Auto-start
# Enable auto-start
sudo systemctl enable nginx
# Disable auto-start
sudo systemctl disable nginx
# Start now + enable auto-start
sudo systemctl enable --now nginx
# Check if enabled
systemctl is-enabled nginx
5.4 Listing Services
# All services
systemctl list-units --type=service
# Running services only
systemctl list-units --type=service --state=running
# Failed services
systemctl list-units --type=service --state=failed
# Service dependencies
systemctl list-dependencies nginx
6. Creating Custom Services
6.1 Unit File Structure
# /etc/systemd/system/myapp.service
[Unit]
Description=My Custom Application
After=network.target
[Service]
Type=simple
User=appuser
Group=appgroup
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/bin/start.sh
ExecStop=/opt/myapp/bin/stop.sh
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
6.2 Key Sections Explained
- [Unit]: Service description, dependencies
- [Service]: Execution method, user, restart policy
- [Install]: Target to link when enabled
6.3 Registering and Managing Services
# After creating/modifying unit file
sudo systemctl daemon-reload
# Enable and start service
sudo systemctl enable --now myapp
# Check status
sudo systemctl status myapp
# View logs
sudo journalctl -u myapp -f
6.4 Various Service Types
# Type=simple (default)
# Main process starts directly
# Type=forking
# Traditional daemon style, forks then parent exits
Type=forking
PIDFile=/var/run/myapp.pid
# Type=oneshot
# One-time tasks (script execution)
Type=oneshot
RemainAfterExit=yes
# Type=notify
# Service notifies systemd when startup is complete
Type=notify
7. journalctl - Log Management
7.1 Basic Usage
# View all logs
sudo journalctl
# Specific service logs
sudo journalctl -u nginx
# Real-time log follow
sudo journalctl -u nginx -f
# Logs since boot
sudo journalctl -b
# Previous boot logs
sudo journalctl -b -1
7.2 Time and Priority Filters
# Time range
sudo journalctl --since "2026-01-21 10:00" --until "2026-01-21 12:00"
sudo journalctl --since "1 hour ago"
sudo journalctl --since today
# Priority filter
sudo journalctl -p err # error and above
sudo journalctl -p warning # warning and above
# Output formats
sudo journalctl -o json-pretty # JSON format
sudo journalctl -o short-iso # ISO time format
7.3 Log Maintenance
# Check disk usage
sudo journalctl --disk-usage
# Delete old logs
sudo journalctl --vacuum-time=7d # Delete older than 7 days
sudo journalctl --vacuum-size=500M # Keep under 500MB
# Config file: /etc/systemd/journald.conf
SystemMaxUse=500M
MaxRetentionSec=7day
8. Practical Scenarios
8.1 LAMP Stack Installation (Ubuntu)
# Update packages
sudo apt update
# Install Apache, MySQL, PHP
sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql
# Start and enable services
sudo systemctl enable --now apache2
sudo systemctl enable --now mysql
# Check status
systemctl status apache2 mysql
8.2 Registering a Node.js App as Service
# /etc/systemd/system/nodeapp.service
[Unit]
Description=Node.js Application
After=network.target
[Service]
Type=simple
User=nodeuser
WorkingDirectory=/opt/nodeapp
ExecStart=/usr/bin/node app.js
Restart=on-failure
Environment=NODE_ENV=production
Environment=PORT=3000
[Install]
WantedBy=multi-user.target
# Register and start
sudo systemctl daemon-reload
sudo systemctl enable --now nodeapp
8.3 Automated Updates
# Ubuntu - unattended-upgrades
sudo apt install unattended-upgrades
sudo dpkg-reconfigure unattended-upgrades
# Verify configuration
cat /etc/apt/apt.conf.d/20auto-upgrades
# RHEL/CentOS - dnf-automatic
sudo dnf install dnf-automatic
sudo systemctl enable --now dnf-automatic.timer
9. Troubleshooting
9.1 Package Problems
# APT lock issues
sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
# Fix broken packages
sudo apt --fix-broken install
sudo dpkg --configure -a
# Rebuild RPM database
sudo rpm --rebuilddb
9.2 Service Problems
# Check service failure cause
sudo systemctl status myservice
sudo journalctl -xe -u myservice
# Config file syntax check
sudo nginx -t
sudo apache2ctl configtest
# Reload systemd
sudo systemctl daemon-reexec
Conclusion
Package management and service operations are core to server administration. Use apt/yum/dnf to efficiently manage software and systemd to reliably run services.
In Part 4, we'll cover Network Configuration and Firewall.
Series Navigation
← Part 2: Users and Permissions | Part 3 (Current) | Part 4: Network and Firewall →