systemd configuration in linux

systemd configuration

Systemd recommended management process, compared to the use supervisord systemd provides system-level support.

First, the system management

Systemd is not a command, but a set of commands, involving all aspects of system administration.

Service type in / usr / lib / systemd / system /

centos7 default /usr/lib/systemd/system/

The default is ubuntu 18 /lib/systemd/system/

  • systemd startup services are stored in this directory, different service types have different file extensions. Therefore, the type of service can be distinguished by file extension
.service General type of service . Mainly: system services, including local services, and network services required for the server itself, etc., most often used to this type of service. It is also the most common type
.socket Internal services data exchange socket services : mainly IPC to transfer information socket file function. This type of service is generally socket file monitoring transmission of information, when information is transmitted to the service via this link to the socket file, then it is transmitted according to the state of the user's request to the corresponding daemon, if the daemon does not start, start the daemon then transmits the user's type of service required sokcet generally less frequently used, therefore usually be a slight delay at boot time to start (and therefore not very common). More generally used for local services, such as many of our software is a graphical interface to operate the program data exchange via socket (which is earlier in this super daemon xinted similar portion)
.target Execution Environment Type : In fact, a group of collection unit, such as a collection of multi-user.target is actually a bunch of services. That, in fact, choose to perform multi-user.target perform a bunch of other .service or (and) .socket services such
.mount .automount File system mount-related services : for example, automatically mount from the network, NFS file system mount file systems such as highly correlated with process management
.path Detection of a specific file or directory type : some services need to detect specific directories from the queue to provide services, such as the most common printing service is to start printing by detecting the print queue directory, which is the type of service you need to .path stand by
.timer Service loop execution : This service is somewhat similar to anacrontab, but the initiative is provided by systemd, more flexible than anacrontab

Other profiles

  • / etc / sysconfig / directory: Almost all of the services will be initialized some options settings are written to this directory. For example: mandb to be updated man page index, parameters need to be added to writes mandb under which this directory. Set network services is written in the / etc / sysconfig / network-scripts / directory within this
  • / var / lib / directory: Some services will generate data writes its data into / var / lib / directory. For example: MariaDB database management system database is written by default / var / lib / mysql / directory
  • / run / directory: placing a lot of cache daemon, including the lock file and PID files

1.1 systemctl

systemctlIt is the main command Systemd for management systems.

# 重启系统
$ sudo systemctl reboot

# 关闭系统,切断电源
$ sudo systemctl poweroff

# CPU停止工作
$ sudo systemctl halt

# 暂停系统
$ sudo systemctl suspend

# 让系统进入冬眠状态
$ sudo systemctl hibernate

# 让系统进入交互式休眠状态
$ sudo systemctl hybrid-sleep

# 启动进入救援状态(单用户状态)
$ sudo systemctl rescue

1.2 systemd-analyze

systemd-analyzeCommand is used to view the start time consuming.

# 查看启动耗时
$ systemd-analyze                                                                                       

# 查看每个服务的启动耗时
$ systemd-analyze blame

# 显示瀑布状的启动过程流
$ systemd-analyze critical-chain

# 显示指定服务的启动流
$ systemd-analyze critical-chain atd.service

1.3 hostnamectl

hostnamectlCommand is used to view information about the current host.

# 显示当前主机的信息
$ hostnamectl

# 设置主机名。
$ sudo hostnamectl set-hostname rhel7

1.4 localectl

localectlCommand for viewing locale.

# 查看本地化设置
$ localectl

# 设置本地化参数。
$ sudo localectl set-locale LANG=en_GB.utf8
$ sudo localectl set-keymap en_GB

1.5 timedatectl

timedatectlCommand is used to view the current time zone setting.

# 查看当前时区设置
$ timedatectl

# 显示所有可用的时区
$ timedatectl list-timezones                                                                                   

# 设置当前时区
$ sudo timedatectl set-timezone America/New_York
$ sudo timedatectl set-time YYYY-MM-DD
$ sudo timedatectl set-time HH:MM:SS

1.6 loginctl

loginctlCommand is used to view the user currently logged on.

# 列出当前session
$ loginctl list-sessions

# 列出当前登录用户
$ loginctl list-users

# 列出显示指定用户的信息
$ loginctl show-user ruanyf

Two, Unit

2.1 Meaning

Systemd can manage all system resources. Different resources collectively referred to as Unit (unit).

Unit is divided into a total of 12 kinds.

  • Service unit: System Services
  • Target unit: Unit consisting of a plurality of
  • Device Unit: hardware
  • Mount point of the file system: Mount Unit
  • Automount Unit: automatic mount point
  • Path Unit: file or path
  • Scope Unit: Systemd not initiated by an external process
  • Slice Unit: Process Group
  • Snapshot Unit: Systemd snapshot, you can cut back to a snapshot
  • Socket Unit: interprocess communication socket
  • Swap Unit:swap 文件
  • Timer Unit: Timer

systemctl list-unitsCommand Unit can view all current system.

# 列出正在运行的 Unit
$ systemctl list-units

# 列出所有Unit,包括没有找到配置文件的或者启动失败的
$ systemctl list-units --all

# 列出所有没有运行的 Unit
$ systemctl list-units --all --state=inactive

# 列出所有加载失败的 Unit
$ systemctl list-units --failed

# 列出所有正在运行的、类型为 service 的 Unit
$ systemctl list-units --type=service

2.2 Unit status

systemctl statusCommand to view the system status and the status of the individual Unit.

# 显示系统状态
$ systemctl status

# 显示单个 Unit 的状态
$ sysystemctl status bluetooth.service

# 显示远程主机的某个 Unit 的状态
$ systemctl -H centos@k8smaster status httpd.service

Unit Management

For users, the most commonly used is the following commands to start and stop the Unit (main service).

# 立即启动一个服务
$ sudo systemctl start apache.service

# 立即停止一个服务
$ sudo systemctl stop apache.service

# 重启一个服务
$ sudo systemctl restart apache.service

# 杀死一个服务的所有子进程
$ sudo systemctl kill apache.service

# 重新加载一个服务的配置文件
$ sudo systemctl reload apache.service

# 重载所有修改过的配置文件
$ sudo systemctl daemon-reload

# 显示某个 Unit 的所有底层参数
$ systemctl show httpd.service

# 显示某个 Unit 的指定属性的值
$ systemctl show -p CPUShares httpd.service

# 设置某个 Unit 的指定属性
$ sudo systemctl set-property httpd.service CPUShares=500

Dependencies

There is a dependency between the Unit: A depends on B, it means that when you start Systemd A, but will start to B.

systemctl list-dependenciesCommand lists all the dependencies of a Unit.

$ systemctl list-dependencies nginx.service

State profiles

systemctl list-unit-filesCommand lists all profiles.

# 列出所有配置文件
$ systemctl list-unit-files

# 列出指定类型的配置文件
$ systemctl list-unit-files --type=service

This command will output a list.

$ systemctl list-unit-files

UNIT FILE              STATE
chronyd.service        enabled
[email protected]         static
[email protected]     disabled

This list shows the status of each profile, a total of four.

  • enabled: Start link has been established
  • disabled: No link established start
  • static: The configuration file is not [Install]part of the (unable to perform), only as dependent on other profiles
  • masked: The configuration file is prohibited to establish links start

Once modify the configuration file, you should let SystemD reload the configuration file, then restart, otherwise the changes will not take effect.

$ sudo systemctl daemon-reload
$ sudo systemctl restart httpd.service

Configuration file is an ordinary text file that can be opened with a text editor.

systemctl catCommand to view the contents of the configuration file.

$ systemctl cat atd.service

[Unit]
Description=ATD daemon

[Service]
Type=forking
ExecStart=/usr/bin/atd

[Install]
WantedBy=multi-user.target

Block profile

[Unit]Usually the first block block the configuration file, the metadata defined for Unit, and the arrangement relationship with other Unit. Its main fields are as follows.

  • Description: Short description
  • Documentation: Document Address
  • Requires: Current Unit Unit dependent other, if they are not running, the current Unit fail to start
  • Wants: Other Unit Unit with the current, if they are not running, the current failure Unit will not start
  • BindsTo: And Requiressimilarly, it specifies Unit If you quit, it will cause the current Unit stop running
  • Before: If the field specified Unit should start, you must start after the current Unit
  • After: If the field specified Unit should start, you must start before the current Unit
  • Conflicts: Unit specified here can not run simultaneously with the current Unit
  • Condition...: Unit is currently running conditions must be met, or will not run
  • Assert...: Unit is currently running conditions must be met, otherwise it will fail to start newspaper

[Install]Usually the last block of the configuration file used to define how to start, and whether the boot. Its main fields are as follows.

  • WantedBy: It is the value of one or more Target, the current Unit activation (enable) symbolic links into the /etc/systemd/systemdirectory name to Target + .wantssubdirectory suffix consisting of
  • RequiredBy: It is the value of one or more Target, the current Unit activated, the symbolic link will be placed in /etc/systemd/systemthe list below to Target name + .requiredsub-directory suffix consisting of
  • Alias: Current Unit can be used to start an alias
  • Also: Current Unit when activated (enable), will be activated at the same time other Unit

[Service]Block used to configure Service Only Service Unit have this type of block. Its main fields are as follows.

  • Type: Define the start of the process behavior. It has the following values.
  • Type=simple: The default value, execute ExecStartthe specified command to start the main process
  • Type=forking: Create a way to fork child process from the parent process, created after the parent process exits immediately
  • Type=oneshot: A one-time process, Systemd will wait for the current service exit, and then continue down
  • Type=dbus: Current service via D-Bus start
  • Type=notify: Current service boot is completed, we will inform Systemd, before proceeding down
  • Type=idle: If there are other task is finished, the current service will not run
  • ExecStart: Start current service command
  • ExecStartPre: Command executed before the start of the current service
  • ExecStartPost: Command executed after starting the current service
  • ExecReload: Command to execute when the current service restart
  • ExecStop: Command to execute when to stop the current service
  • ExecStopPost: After the stop command execution of its services
  • RestartSec: Automatic restart seconds of the current service interval
  • Restart: Define what circumstances Systemd will automatically restart the current service, including the possible values always(always restart),on-success , , , ,on-failureon-abnormalon-aborton-watchdog
  • TimeoutSec: Definition of Systemd wait before stopping the current service in seconds
  • Environment: Specifies the environment variables

Unit configuration file complete field list, please refer to the official documentation .

Target

When you start your computer, you need to start a lot of Unit. If every time you start, must all be stated in this Unit need to start which is obviously very inconvenient. Systemd solution is Target.

Simply put, Target Unit is a group that contains many relevant Unit. When you start a Target, Systemd all starts inside the Unit. In this sense, the concept is similar to Target "status points" to start a Target like to start some kind of state.

The traditional initinside boot mode, the concept RunLevel, with the role Target is very similar. The difference is, RunLevel are mutually exclusive, it is impossible RunLevel multiple simultaneous start, but more Target can be started simultaneously.

# 查看当前系统的所有 Target
$ systemctl list-unit-files --type=target

# 查看一个 Target 包含的所有 Unit
$ systemctl list-dependencies multi-user.target

# 查看启动时的默认 Target
$ systemctl get-default

# 设置启动时的默认 Target
$ sudo systemctl set-default multi-user.target

# 切换 Target 时,默认不关闭前一个 Target 启动的进程,
# systemctl isolate 命令改变这种行为,
# 关闭前一个 Target 里面所有不属于后一个 Target 的进程
$ sudo systemctl isolate multi-user.target

Log Management

Systemd unified management of all Unit startup log. Benefits is that you can only use journalctla single command to view all the log (kernel log and application log). Log configuration file is /etc/systemd/journald.conf.

journalctlPowerful, use very much.

# 查看所有日志(默认情况下 ,只保存本次启动的日志)
$ sudo journalctl

# 查看内核日志(不显示应用日志)
$ sudo journalctl -k

# 查看系统本次启动的日志
$ sudo journalctl -b
$ sudo journalctl -b -0

# 查看上一次启动的日志(需更改设置)
$ sudo journalctl -b -1

# 查看指定时间的日志
$ sudo journalctl --since="2012-10-30 18:17:16"
$ sudo journalctl --since "20 min ago"
$ sudo journalctl --since yesterday
$ sudo journalctl --since "2015-01-10" --until "2015-01-11 03:00"
$ sudo journalctl --since 09:00 --until "1 hour ago"

# 显示尾部的最新10行日志
$ sudo journalctl -n

# 显示尾部指定行数的日志
$ sudo journalctl -n 20

# 实时滚动显示最新日志
$ sudo journalctl -f

# 查看指定服务的日志
$ sudo journalctl /usr/lib/systemd/systemd

# 查看指定进程的日志
$ sudo journalctl _PID=1

# 查看某个路径的脚本的日志
$ sudo journalctl /usr/bin/bash

# 查看指定用户的日志
$ sudo journalctl _UID=33 --since today

# 查看某个 Unit 的日志
$ sudo journalctl -u nginx.service
$ sudo journalctl -u nginx.service --since today

# 实时滚动显示某个 Unit 的最新日志
$ sudo journalctl -u nginx.service -f

# 合并显示多个 Unit 的日志
$ journalctl -u nginx.service -u php-fpm.service --since today

# 查看指定优先级(及其以上级别)的日志,共有8级
# 0: emerg
# 1: alert
# 2: crit
# 3: err
# 4: warning
# 5: notice
# 6: info
# 7: debug
$ sudo journalctl -p err -b

# 日志默认分页输出,--no-pager 改为正常的标准输出
$ sudo journalctl --no-pager

# 以 JSON 格式(单行)输出
$ sudo journalctl -b -u nginx.service -o json

# 以 JSON 格式(多行)输出,可读性更好
$ sudo journalctl -b -u nginx.serviceqq
 -o json-pretty

# 显示日志占据的硬盘空间
$ sudo journalctl --disk-usage

# 指定日志文件占据的最大空间
$ sudo journalctl --vacuum-size=1G

# 指定日志文件保存多久
$ sudo journalctl --vacuum-time=1years

Quote:

https://www.freedesktop.org/software/systemd/man/systemd.unit.html

http://www.jinbuguo.com/systemd/systemctl.html

http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html

Guess you like

Origin www.cnblogs.com/lovesKey/p/11002042.html