systemctl Command
The systemctl command is the primary interface for controlling the systemd system and service manager. It replaces older SysV init commands like service and chkconfig, providing unified management of system services, targets, and system state.
Syntax
systemctl [OPTIONS] COMMAND [UNIT...]
Core Service Management
| Command | Description | Old Equivalent |
|---|---|---|
systemctl start service | Start a service | service service start |
systemctl stop service | Stop a service | service service stop |
systemctl restart service | Restart a service | service service restart |
systemctl reload service | Reload configuration | service service reload |
systemctl status service | Show service status | service service status |
systemctl enable service | Enable at boot | chkconfig service on |
systemctl disable service | Disable at boot | chkconfig service off |
Service Management Commands
Basic Operations
Start a service:
systemctl start nginx.service
Stop a service:
systemctl stop nginx.service
Restart a service:
systemctl restart nginx.service
Reload service configuration:
systemctl reload nginx.service
Check service status:
systemctl status nginx.service
Check if service is active:
systemctl is-active nginx.service
Enable/Disable Services
Enable service to start at boot:
systemctl enable nginx.service
Enable and start immediately:
systemctl enable --now nginx.service
Disable service from starting at boot:
systemctl disable nginx.service
Disable and stop immediately:
systemctl disable --now nginx.service
Service Information
List all services:
systemctl list-units --type=service
List all services including inactive:
systemctl list-units --type=service --all
List enabled services:
systemctl list-unit-files --type=service --state=enabled
List failed services:
systemctl --failed --type=service
Show service configuration:
systemctl cat nginx.service
System State Management
Power Management
Reboot the system:
systemctl reboot
Shutdown the system:
systemctl poweroff
Halt the system:
systemctl halt
Suspend the system:
systemctl suspend
Hibernate the system:
systemctl hibernate
Target Management
List current targets:
systemctl list-units --type=target
Change to specific target:
systemctl isolate graphical.target
Set default target:
systemctl set-default graphical.target
Get default target:
systemctl get-default
Unit Management
Common Target Levels
| Target | Description | Equivalent |
|---|---|---|
poweroff.target | System shutdown | Runlevel 0 |
rescue.target | Rescue mode | Runlevel 1 |
multi-user.target | Multi-user, no GUI | Runlevel 3 |
graphical.target | Multi-user with GUI | Runlevel 5 |
reboot.target | System reboot | Runlevel 6 |
Unit File Management
Create a new service:
sudo systemctl edit --full --force myservice.service
Edit service file:
sudo systemctl edit nginx.service
Reload systemd configuration:
systemctl daemon-reload
Reset service to defaults:
sudo systemctl edit nginx.service --remove
Job Management
Control System Jobs
List active jobs:
systemctl list-jobs
Cancel a job:
systemctl cancel job-id
Check system boot performance:
systemd-analyze
Analyze boot time:
systemd-analyze blame
Analyze critical chain:
systemd-analyze critical-chain
Advanced Usage
Service Dependencies
Show dependencies:
systemctl list-dependencies nginx.service
Show reverse dependencies:
systemctl list-dependencies --reverse nginx.service
Show unit requirements:
systemctl show --property=Requires nginx.service
Service Properties
Show all service properties:
systemctl show nginx.service
Show specific properties:
systemctl show -p CPUQuota,MemoryLimit nginx.service
Get main PID:
systemctl show --property=MainPID --value nginx.service
Masking Services
Mask a service (prevent manual start):
systemctl mask nginx.service
Unmask a service:
systemctl unmask nginx.service
Check if masked:
systemctl is-enabled nginx.service
Real-World Examples
Web Server Management
# Enable and start Apache
systemctl enable --now httpd
# Check Apache status
systemctl status httpd
# Reload Apache configuration
systemctl reload httpd
# Restart Apache with verbose output
systemctl restart --verbose httpd
Database Service Management
# Start MariaDB
systemctl start mariadb
# Enable MariaDB at boot
systemctl enable mariadb
# Check MariaDB status
systemctl status mariadb
# Check MariaDB logs
journalctl -u mariadb -f
System Diagnostics
# Check failed services
systemctl --failed
# List all running services
systemctl list-units --type=service --state=running
# Check system load
systemctl status
# Analyze boot performance
systemd-analyze
Service Troubleshooting
# Check service logs
journalctl -u service-name -b
# Start service in debug mode
systemctl edit service-name
# Add: Environment=SYSTEMD_LOG_LEVEL=debug
# Check service dependencies
systemctl list-dependencies service-name
# Verify service configuration
systemd-analyze verify service-name
Unit File Examples
Basic Service Template
[Unit]
Description=My Custom Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/mycommand
Restart=always
User=myuser
[Install]
WantedBy=multi-user.target
Web Service Template
[Unit]
Description=Web Application
After=network.target postgresql.service
[Service]
Type=forking
PIDFile=/var/run/myapp.pid
ExecStart=/usr/local/bin/myapp start
ExecStop=/usr/local/bin/myapp stop
User=www-data
Group=www-data
[Install]
WantedBy=multi-user.target
Integration with Other Tools
Monitoring Services
# Watch service status changes
watch systemctl status nginx.service
# Monitor service logs in real-time
journalctl -u nginx.service -f
# Check service resource usage
systemctl status nginx.service
# Look at CPU, Memory information
Automation Scripts
#!/bin/bash
# Service restart script
SERVICE="nginx"
if systemctl is-active --quiet $SERVICE; then
echo "$SERVICE is running, restarting..."
systemctl restart $SERVICE
else
echo "$SERVICE is not running, starting..."
systemctl start $SERVICE
fi
systemctl enable $SERVICE
Troubleshooting
Common Issues
Service fails to start:
# Check status
systemctl status service-name
# Check logs
journalctl -u service-name -b
# Check configuration
systemctl cat service-name
Service won't enable:
# Check dependencies
systemctl list-dependencies service-name
# Check target
systemctl get-default
# Verify unit file exists
systemctl list-unit-files | grep service-name
Performance issues:
# Check boot time
systemd-analyze blame
# Check service startup time
systemd-analyze critical-chain service-name
# Check resource limits
systemctl show service-name | grep Limit
Security Considerations
Service Hardening
Run as specific user:
[Service]
User=serviceuser
Group=servicegroup
Restrict capabilities:
[Service]
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_BIND_SERVICE
File system restrictions:
[Service]
ProtectSystem=full
ProtectHome=true
ReadWritePaths=/var/log/myservice
Best Practices
- Always check status: Use
systemctl statusfor diagnostics - Use --now with enable/disable: Changes take effect immediately
- Test unit files: Use
systemd-analyze verifybefore deploying - Monitor logs: Use
journalctlfor troubleshooting - Use templates: Create reusable service templates
- Document dependencies: Clearly specify service dependencies
- Security first: Use service hardening options
Migration from SysV
Conversion examples:
# Old SysV commands
service httpd start
chkconfig httpd on
# New systemd equivalents
systemctl start httpd
systemctl enable httpd
Migration checklist:
- Replace service commands with systemctl
- Update startup scripts to use targets
- Convert init scripts to unit files
- Update documentation and procedures
- Train administrators on new commands
Content adapted from the linux-command project. Original content available at https://github.com/jaywcjlove/linux-command