Linux systemctl Command
What is systemctl
systemctl is a command-line tool in Linux for controlling the systemd system and service manager. As a core component of modern Linux distributions, it replaces the traditional init system and the service command.
Introduction to systemd
systemd is a system and service manager that:
- Runs as PID 1 (the first process started)
- Starts, stops, and manages all other processes
- Provides parallel startup capability, significantly speeding up system boot
- Supports on-demand service startup
- Provides log collection (via journald)
systemctl Basic Syntax
systemctl [选项] [命令] [单元名称]
Common options
| Option | Description |
|---|---|
| -t, --type | Specify the unit type (service, socket, device, etc.) |
| -a, --all | Show all units, including inactive ones |
| --state | Filter units by state |
| -l, --full | Show full unit information |
| -H, --host | Operate on a remote host |
Service management commands
Start/stop/restart the service
Example
Start the service
sudo
systemctl start
[
Service name
]
Stop a service
sudo
systemctl stop
[
Service name
]
Restart the service
sudo
systemctl restart
[
Service name
]
Reload configuration (without restarting the service)
sudo
systemctl reload
[
Service name
]
Check service status
Example
Check the status of a single service
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 a service (start on boot)
sudo
systemctl
enable
[
Service name
]
Disable a service (remove from boot)
sudo
systemctl disable
[
Service name
]
Check whether a service is enabled
systemctl is-enabled
[
Service name
]
Unit File Management
Unit file locations
- System units:
/usr/lib/systemd/system/ - Administrator-defined units:
/etc/systemd/system/
Common unit file operations
Example
Reload all unit files (required after modifying configuration)
sudo
systemctl daemon-reload
Display unit file contents
systemctl
cat
[
Unit name
]
Edit a unit file (creates an override file)
sudo
systemctl edit
[
Unit name
]
--full
System State Management
System Power Management
Example
Shutdown
sudo
systemctl poweroff
Reboot
sudo
systemctl reboot
Suspend
sudo
systemctl
suspend
Hibernate
sudo
systemctl hibernate
System Runlevels
Example
Get the current target (runlevel)
systemctl get-default
Set the 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 the Nginx Service
Example
Start Nginx
sudo
systemctl start nginx
Enable startup on boot
sudo
systemctl
enable
nginx
to check the status
systemctl status nginx
Reload after testing the configuration
sudo
nginx
-t
Test the configuration first
sudo
systemctl reload nginx
Example 2: Creating a Custom Service
- Create a 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
- Enable and start the service:
Example
sudo
systemctl daemon-reload
sudo
systemctl
enable
myapp
sudo
systemctl start myapp
Troubleshooting common problems
Service fails to start
-
View detailed logs:
journalctl -u [service name] -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 the startup time of each service
systemd-analyze blame
Generate a boot flowchart (requires a graphical interface)
systemd-analyze plot
boot.svg
Linux Command Reference