YouTip LogoYouTip

Linux Comm Systemctl

[![Image 1: Linux Command Encyclopaedia](#) Linux Command Encyclopaedia](#)\n\n* * *\n\n## What is systemctl\n\nsystemctl is a command-line tool used to control the systemd system and service manager in Linux systems. As a core component of modern Linux distributions, it replaces the traditional init system and service commands.\n\n### Introduction to systemd\n\nsystemd is a system and service manager that:\n\n* Runs as PID 1 (the first process started)\n* Responsible for starting, stopping, and managing all other processes\n* Provides parallel startup capability, significantly speeding up system boot\n* Supports on-demand service startup\n* Provides log collection functionality (via journald)\n\n* * *\n\n## systemctl Basic Syntax\n\nsystemctl \n\n### Common Options\n\n| Option | Description |\n| --- | --- |\n| -t, --type | Specify unit type (service, socket, device, etc.) |\n| -a, --all | Display all units, including inactive ones |\n| --state | Filter units by state |\n| -l, --full | Display complete unit information |\n| -H, --host | Operate on remote host |\n\n* * *\n\n## Service Management Commands\n\n### Start/Stop/Restart Service\n\n## Example\n\n# Start service\n\nsudo systemctl start \n\n# Stop service\n\nsudo systemctl stop \n\n# Restart service\n\nsudo systemctl restart \n\n# Reload configuration (without restarting service)\n\nsudo systemctl reload \n\n### View Service Status\n\n## Example\n\n# View single service status\n\n systemctl status \n\n# View all running services\n\n systemctl list-units --type=service --state=running\n\n# View failed services\n\n systemctl --failed\n\n### Enable/Disable Service\n\n## Example\n\n# Enable service (auto-start on boot)\n\nsudo systemctl enable\n\n# Disable service (disable auto-start on boot)\n\nsudo systemctl disable \n\n# Check if service is enabled\n\n systemctl is-enabled \n\n* * *\n\n## Unit File Management\n\n### Unit File Locations\n\n* System units: `/usr/lib/systemd/system/`\n* Administrator-defined units: `/etc/systemd/system/`\n\n### Common Unit File Operations\n\n## Example\n\n# Reload all unit files (required after modifying configuration)\n\nsudo systemctl daemon-reload\n\n# Display unit file content\n\n systemctl cat\n\n# Edit unit file (creates override file)\n\nsudo systemctl edit --full\n\n* * *\n\n## System State Management\n\n### System Power Management\n\n## Example\n\n# Power off\n\nsudo systemctl poweroff\n\n# Reboot\n\nsudo systemctl reboot\n\n# Suspend\n\nsudo systemctl suspend\n\n# Hibernate\n\nsudo systemctl hibernate\n\n### System Runlevels\n\n## Example\n\n# Get current target (runlevel)\n\n systemctl get-default\n\n# Set default target\n\nsudo systemctl set-default \n\n# Switch target (takes effect immediately)\n\nsudo systemctl isolate \n\nCommon targets:\n\n* graphical.target - Graphical interface mode\n* multi-user.target - Multi-user text mode\n* rescue.target - Rescue mode\n* emergency.target - Emergency mode\n\n* * *\n\n## Practical Examples\n\n### Example 1: Manage Nginx Service\n\n## Example\n\n# Start Nginx\n\nsudo systemctl start nginx\n\n# Enable auto-start on boot\n\nsudo systemctl enable nginx\n\n# Check status\n\n systemctl status nginx\n\n# Reload after testing configuration\n\nsudo nginx -t# First test configuration\n\nsudo systemctl reload nginx\n\n### Example 2: Create Custom Service\n\n1. Create service file `/etc/systemd/system/myapp.service`:\n\n## Example\n\n\n\nDescription=My Custom Application\n\nAfter=network.target\n\n\n\nExecStart=/usr/bin/python3 /opt/myapp/app.py\n\nWorkingDirectory=/opt/myapp\n\nUser=myappuser\n\nGroup=myappgroup\n\nRestart=always\n\n\n\nWantedBy=multi-user.target\n\n1. Enable and start the service:\n\n## Example\n\nsudo systemctl daemon-reload\n\nsudo systemctl enable myapp\n\nsudo systemctl start myapp\n\n* * *\n\n## Common Troubleshooting\n\n### Service Fails to Start\n\n1. View detailed logs:\n\njournalctl -u -xe\n2. Check dependencies:\n\nsystemctl list-dependencies \n3. Run in debug mode:\n\nsystemctl status -l --no-pager\n\n### Performance Analysis\n\n## Example\n\n# Show system boot time\n\n systemd-analyze\n\n# Show startup time for each service\n\n systemd-analyze blame\n\n# Generate boot process graph (requires graphical interface)\n\n systemd-analyze plot > boot.svg\n\n[![Image 2: Linux Command Encyclopaedia](#) Linux Command Encyclopaedia](#)
← Linux Comm InitLinux Comm Lsb_Release β†’