Linux systemctl Command

Linux 命令大全Linux Command Reference


What is systemctl

systemctl is a command-line tool in Linux systems used to control the systemd system and service manager. As a core component of modern Linux distributions, it replaces the traditional init system and service commands.

Introduction to systemd

systemd is a system and service manager that:

  • Runs as PID 1 (the first process to start)
  • Responsible for starting, stopping, and managing all other processes
  • Provides parallel startup capability, significantly speeding up system boot time
  • Supports on-demand service startup
  • Provides log collection functionality (via journald)

systemctl Basic Syntax

systemctl [选项] [命令] [单元名称]

Common Options

Option Description
-t, --type Specify unit type (service, socket, device, etc.)
-a, --all Show all units, including inactive ones
--state Filter units by state
-l, --full Show complete unit information
-H, --host Operate on a remote host

Service Management Commands

Start/Stop/Restart Services

Example

# Start service
sudo systemctl start [service name]

# Stop service
sudo systemctl stop [service name]

# Restart service
sudo systemctl restart [service name]

# Reload configuration (without restarting service)
sudo systemctl reload [service name]

View Service Status

Example

# View single service status
systemctl status [service name]

# View all running services
systemctl list-units --type=service --state=running

# View failed services
systemctl --failed

Enable/Disable Services

Example

# Enable service (auto-start on boot)
sudo systemctl enable [service name]

# Disable service (cancel auto-start on boot)
sudo systemctl disable [service name]

# Check if service is enabled
systemctl is-enabled [service name]

Unit File Management

Unit File Locations

  • System units:/usr/lib/systemd/system/
  • Administrator custom units:/etc/systemd/system/

Common Unit File Operations

Example

# Reload all unit files (needed after modifying configuration)
sudo systemctl daemon-reload

# Display unit file contents
systemctl cat [unit name]

# Edit unit file (creates an override file)
sudo systemctl edit [unit name] --full

System State Management

System Power Management

Example

# Shut down
sudo systemctl poweroff

# Reboot
sudo systemctl reboot

# Suspend
sudo systemctl suspend

# Hibernate
sudo systemctl hibernate

System Runlevels

Example

# Get current target (runlevel)
systemctl get-default

# Set default target
sudo systemctl set-default [target name]

# Switch target (takes effect immediately)
sudo systemctl isolate [target name]

Common targets:

  • graphical.target - graphical interface mode
  • multi-user.target - multi-user text mode
  • rescue.target - rescue mode
  • emergency.target - emergency mode

Practical Examples

Example 1: Managing Nginx Service

Example

# Start Nginx
sudo systemctl start nginx

# Enable auto-start on boot
sudo systemctl enable nginx

# Check status
systemctl status nginx

# Test configuration then reload
sudo nginx -t  # Test configuration first
sudo systemctl reload nginx

Example 2: Creating a Custom Service

  1. Create service file/etc/systemd/system/myapp.service:

Example

[Unit]
Description=My Custom Application
After=network.target

[Service]
ExecStart=/usr/bin/python3 /opt/myapp/app.py
WorkingDirectory=/opt/myapp
User=myappuser
Group=myappgroup
Restart=always

[Install]
WantedBy=multi-user.target
  1. Enable and start the service:

Example

sudo systemctl daemon-reload
sudo systemctl enable myapp
sudo systemctl start myapp

Troubleshooting Common Issues

Service Failed to Start

  1. View detailed logs:

    journalctl -u [服务名] -xe
  2. Check dependencies:

    systemctl list-dependencies [服务名]
  3. Run in debug mode:

    systemctl status [服务名] -l --no-pager

Performance Analysis

Example

# Show system boot time
systemd-analyze

# Show startup time of each service
systemd-analyze blame

# Generate boot-up diagram (requires graphical interface)
systemd-analyze plot > boot.svg

Linux 命令大全Linux Command Reference

Other Extensions