Systemd
Notes for managing services on RHEL/Linux systems.
Systemd is used to start, stop, restart, enable, disable, and troubleshoot services.
Basic service commands
Check service status
systemctl status SERVICE_NAME
Example:
systemctl status sshd
Start a service
sudo systemctl start SERVICE_NAME
Stop a service
sudo systemctl stop SERVICE_NAME
Restart a service
sudo systemctl restart SERVICE_NAME
Use restart carefully on production systems because it stops and starts the service.
Reload a service
sudo systemctl reload SERVICE_NAME
Reload is safer than restart when supported because it reloads configuration without fully stopping the service.
Not every service supports reload.
Enable service at boot
sudo systemctl enable SERVICE_NAME
Disable service at boot
sudo systemctl disable SERVICE_NAME
Check if service is enabled
systemctl is-enabled SERVICE_NAME
Check if service is active
systemctl is-active SERVICE_NAME
Failed services
Show failed services
systemctl --failed
Reset failed state
sudo systemctl reset-failed
For one service:
sudo systemctl reset-failed SERVICE_NAME
This does not fix the service. It only clears the failed state.
Service logs
Show logs for a service
journalctl -u SERVICE_NAME
Show latest service logs
journalctl -u SERVICE_NAME -n 100
Follow service logs live
journalctl -u SERVICE_NAME -f
Show service logs from current boot
journalctl -u SERVICE_NAME -b
Show service errors only
journalctl -u SERVICE_NAME -p err
List services
List running services
systemctl list-units --type=service --state=running
List all services
systemctl list-units --type=service --all
List service unit files
systemctl list-unit-files --type=service
Search for a service
systemctl list-unit-files | grep SERVICE_NAME
Example:
systemctl list-unit-files | grep ssh
Unit files
Show unit file content
systemctl cat SERVICE_NAME
Example:
systemctl cat sshd
Show service properties
systemctl show SERVICE_NAME
Show important service properties
systemctl show SERVICE_NAME -p ActiveState -p SubState -p MainPID -p ExecMainStatus
Reload systemd after unit changes
After creating or editing a systemd unit file:
sudo systemctl daemon-reload
Then restart or start the service:
sudo systemctl restart SERVICE_NAME
Common service states
active/running = service is running
inactive/dead = service is stopped
failed = service failed
activating = service is starting
deactivating = service is stopping
enabled = starts at boot
disabled = does not start at boot
masked = blocked from starting
Mask and unmask
Mask a service
sudo systemctl mask SERVICE_NAME
This prevents the service from being started manually or automatically.
Unmask a service
sudo systemctl unmask SERVICE_NAME
Restart a stuck service
Basic flow:
systemctl status SERVICE_NAME
journalctl -u SERVICE_NAME -n 100
sudo systemctl restart SERVICE_NAME
systemctl status SERVICE_NAME
If it fails again:
journalctl -u SERVICE_NAME -p err
journalctl -xe
Check what changed recently
Useful commands:
journalctl -u SERVICE_NAME --since "1 hour ago"
journalctl -u SERVICE_NAME --since "today"
systemctl status SERVICE_NAME
Also check configuration files if the service was edited recently.
Create a simple service file
Example service file:
[Unit]
Description=Example custom service
After=network.target
[Service]
Type=simple
User=randy
WorkingDirectory=/home/randy/example
ExecStart=/home/randy/example/start.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
Save it as:
/etc/systemd/system/example.service
Then run:
sudo systemctl daemon-reload
sudo systemctl enable example.service
sudo systemctl start example.service
systemctl status example.service
Common troubleshooting checklist
When a service fails:
1. Am I checking the correct service name?
2. Is the service active, inactive, or failed?
3. What does systemctl status show?
4. What do the last 100 journal lines show?
5. Did the config file change?
6. Is a required file missing?
7. Are permissions correct?
8. Is the port already in use?
9. Is the disk full?
10. Did the service fail after reboot?
First commands for service issues
systemctl status SERVICE_NAME
journalctl -u SERVICE_NAME -n 100
journalctl -u SERVICE_NAME -p err
systemctl --failed
df -h
ss -tulpn
Notes
Add service-specific notes here over time.