SystemD Commands Cheat Sheet - Essential Linux Administration Guide

SystemD Commands Cheat Sheet - Essential Linux Administration Guide

Picture this: You’re managing a bustling restaurant during dinner rush. You’ve got chefs, servers, dishwashers, and hosts all needing to coordinate perfectly. One person starts too early, another finishes too late, and suddenly your kitchen is chaos. Now imagine having a brilliant manager who knows exactly when each person should start, stop, and what they depend on to do their job properly.

That’s exactly what SystemD does for your Linux system! It’s the ultimate team manager that keeps all your services, processes, and system components running in perfect harmony. No more wondering why your web server started before your database, or why that critical service didn’t boot up with your system.

Why Should You Care About SystemD?

Here’s what SystemD solves for you:

  • No more service startup chaos — Everything starts in the right order, every time
  • Dependency management made easy — Services wait for what they need before starting
  • Faster boot times — Parallel startup instead of one-by-one waiting
  • Better system control — Start, stop, restart, and monitor services with simple commands
  • Automatic service recovery — Failed services can restart themselves
  • Centralized logging — All your service logs in one place with timestamps

Understanding SystemD Units: Your System’s Different Job Roles

Think of SystemD units like different job roles in your restaurant team. Each has a specific function:

Service Units: The Workhorses (.service)

These are your main applications and background processes — like your chefs and servers. They do the actual work.

Every service unit has three main sections:

[Unit] Section — “Who do I work with?”

[Unit]
Description=Start a custom script when connected to network
After=network.target # Wait for network before starting
Requires=database.service # Need database running first

[Service] Section — “How do I do my job?”

[Service]
Type=simple # I'm a straightforward background process
User=admin # Run as this user (not root for security)
ExecStart=/usr/mayhemcode/code/script.sh # Command to start me
ExecStop=/bin/kill $MAINPID # How to stop me gracefully

[Install] Section — “When should I automatically start?”

[Install]
WantedBy=multi-user.target # Start when system reaches multi-user mode

Quick Service Management:

# Start a service right now
sudo systemctl start myapp

# Make it start automatically at boot
sudo systemctl enable myapp

# Check what's happening
sudo systemctl status myapp

Target Units: The System States

Targets are like “shift changes” in your restaurant — they define different operating modes by grouping services together.

# See current system mode
systemctl get-default

# Change to text-only mode (no GUI)
sudo systemctl set-default multi-user.target

# Switch to graphical mode
sudo systemctl set-default graphical.target

Mount Units: The Storage Managers (.mount)

These handle your file systems — like assigning storage areas in your kitchen.

# Example mount unit
[Mount]
What=/dev/sdb1
Where=/home/data
Type=ext4
Options=defaults

Timer Units: The Schedulers (.timer)

Think of these as your system’s appointment calendar — they replace traditional cron jobs with better integration.

# Run backup every day at 2 AM
[Timer]
OnCalendar=daily
OnCalendar=*-*-* 02:00:00
# Run 5 minutes after boot
OnBootSec=5min

Essential SystemD Management Tools

SystemCtl: Your Primary Control Panel

This is your main remote control for everything:

# Service lifecycle management
sudo systemctl start nginx # Start now
sudo systemctl stop nginx # Stop now
sudo systemctl restart nginx # Restart (stop + start)
sudo systemctl reload nginx # Reload config without stopping
sudo systemctl enable nginx # Auto-start at boot
sudo systemctl disable nginx # Don't auto-start at boot

# Service investigation
systemctl status nginx # Current status + recent logs
systemctl is-active nginx # Just the status
systemctl is-enabled nginx # Will it start at boot?

System Configuration Tools

Hostname Management:

# Check current hostname
hostnamectl

# Change hostname
sudo hostnamectl set-hostname myserver

Kernel Parameter Tuning:

# View all kernel parameters
sysctl -a

# Change a parameter temporarily
sudo sysctl -w vm.swappiness=10

Network and Time Management

DNS Resolution:

# Query DNS (replaces nslookup)
resolvectl query google.com

# Check DNS status
resolvectl status

Time Management:

# Check current time settings
timedatectl

# Set timezone
sudo timedatectl set-timezone Asia/Kolkata

# Enable automatic time sync
sudo timedatectl set-ntp true

Performance Analysis: Finding the Bottlenecks

Boot Time Analysis

# See total boot time breakdown
systemd-analyze

# Find slowest services
systemd-analyze blame


# Create visual boot chart
systemd-analyze plot > boot.svg

Sample Output:

Startup finished in 2.1s (firmware) + 3.2s (loader) + 4.5s (kernel) + 8.7s (userspace) = 18.5s

Advanced Service Management

Creating Service Overrides

Instead of editing original service files, create safe overrides:

# Open override editor
# This creates: /etc/systemd/system/nginx.service.d/override.conf
sudo systemctl edit nginx

Always reload after changes:

sudo systemctl daemon-reload  # Tell SystemD to re-read configs
sudo systemctl restart nginx # Apply changes

Service Control Strategies

Enable vs Start vs Mask:

  • Enable = “Start automatically at boot” (creates symlinks)
  • Start = “Start right now” (one-time action)
  • Mask = “Never allow this to start” (symlinks to /dev/null)
# Permanently block a service
sudo systemctl mask bluetooth
sudo systemctl unmask bluetooth # Remove the block

TLDR Cheat Sheet

Daily Commands:

# Service basics
sudo systemctl start/stop/restart/status SERVICE
sudo systemctl enable/disable SERVICE # Boot behavior

# System investigation
systemctl list-units --failed # What's broken?
systemd-analyze blame # Boot slowdowns
journalctl -u SERVICE # Service logs

# System state
systemctl get-default # Current boot target
hostnamectl # Hostname info
timedatectl # Time settings

Remember: Always run sudo systemctl daemon-reload after editing any SystemD files!

SystemD might seem complex at first, but once you understand it’s just a really good manager keeping your system organized, it becomes an incredibly powerful tool. Start with basic service management, then gradually explore the advanced features as you need them.

Post a Comment

Previous Post Next Post