Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux systemctl Command

Linux systemctl Command

Linux Command Reference Linux Command Reference


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:


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

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:


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

  1. 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

  1. 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

  1. 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 Linux Command Reference