1. Get information about units

The systemctl list-units can be used to quickly get information about all units.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# List running Units
systemctl list-units
# List all Units, including those for which no configuration file was found or failed to start
systemctl list-units --all
# List all units that are not running
systemctl list-units --all --state=inactive
# List all units that failed to load
systemctl list-units --failed
# List all running units of type service
systemctl list-units --type=service

The systemctl list-dependencies allows you to get the dependency information of the units.

1
2
3
4
# Get the dependency tree of the ng service
systemctl list-dependencies nginx.service
# Get the details of the dependency tree of the ng service
systemctl list-dependencies --all nginx.service

2. units state management

Managing unit status (mainly service type units) via systemctl is the most common operation for users.

1
2
3
4
5
6
7
8
# Get the status of all units
systemctl status
# Display the status of a single Unit
sysystemctl status bluetooth.service
# Quick diagnostic unit status
systemctl is-active/is-failed/is-enabled application.service
# Manage individual unit states
systemctl start/stop/restart/kill apache.service

3. units configuration management

The systemctl user can quickly add/remove, configuration information for units and environment variable information. These additional additions are saved as files in the /etc/systemd/system.control/<unit-name>. <unit-type>.d/ directory.

1
2
3
4
5
6
7
8
# Print all configuration files
systemctl cat httpd.service
# Show all the underlying parameters of a Unit
systemctl show httpd.service
# Display the value of the specified property of a Unit
systemctl show -p CPUShares httpd.service
# Set the specified property of a Unit
systemctl set-property httpd.service CPUShares=500

When users manually edit the units configuration file, they need to reload the configuration with the following command.

1
2
3
4
# Reload a service's configuration file
systemctl reload apache.service
# Reload all modified configuration files
systemctl daemon-reload

4. Environment Variable Management

Systemd has a separate environment variable management system, and there are several ways for users to inject environment variables into systemd-managed processes.

  • user-defined service unit configuration using Environment or EnvironmentFile injection for a single unit.
  • Injection via systemd global/user environment variables, where there are several ways to manage these two environment variables.
    • Setting environment variables via the DefaultEnvironment parameter in /etc/systemd/system.conf and /etc/systemd/user.conf (effective upon system reboot).
    • Generating environment variables via custom scripts in the /usr/lib/systemd/system-environment-generators/ and /usr/local/lib/systemd/user-environment-generators/ directories (ref), which can take effect via systemctl daemon-reload after the user places the script
    • The global environment variables of systemd or user-level environment variables are managed with the following command (with the -user argument), but the variables generated in this way are temporary.
1
2
3
4
5
6
7
8
# Get environment variables
systemctl show-environment
# Set environment variables
systemctl set-environment NAME1=VALUE1  NAME2=VALUE2
# Remove environment variables
systemctl unset-environment NAME1 NAME2
# Import the environment variables from the current shell into systemd
systemctl import-environment NAME1 NAME2

5. remote management

systemctl can manage units on remote hosts based on sshd, and users can execute any command via passwd or password-free configuration. However, systemctl does not provide parameters to specify the sshd port on the other side, so you can modify /etc/ssh/ssh_config to specify the port number.

1
systemctl -H root@<address> status httpd.service

6. manage the operating system

Manage system restart, suspend, etc. via systemctl.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Reboot the system
$ sudo systemctl reboot
# Shut down the system and disconnect the power
$ sudo systemctl poweroff
# CPU stops working
$ sudo systemctl halt
# Pause system
$ sudo systemctl suspend
# Put the system into hibernation
$ sudo systemctl hibernate
# Put the system into an interactive hibernation state
$ sudo systemctl hybrid-sleep
# Start into rescue state (single user state)
$ sudo systemctl rescue

7. Other command line tools

systemd-analyze provides tools to analyze system boot time and boot process.

1
2
3
4
5
6
7
8
# View startup elapsed time
systemd-analyze                                                                                       
# View the start-up time of each service
systemd-analyze blame
# Showing a waterfall flow of the startup process
systemd-analyze critical-chain
# Show the startup stream of the specified service
systemd-analyze critical-chain atd.service