系统结构
Systemd 的设计目标是为系统的启动和管理提供一套完整的解决方案,具体的结构如上图所示。Systtemd不是一个命令,而是一组命令,这组命令涉及系统管理的多个方面。
Systemd的相关命令
systemctl
#查看版本
$ sudo systemctl --version
# 重启系统
$ sudo systemctl reboot
# 关闭系统,切断电源
$ sudo systemctl poweroff
# CPU停止工作
$ sudo systemctl halt
# 暂停系统
$ sudo systemctl suspend
# 让系统进入休眠状态
$ sudo systemctl hibernate
# 让系统进入交互式休眠状态
$ sudo systemctl hybrid-sleep
# 启动进入救援状态(单用户状态)
$ sudo systemctl rescue
systemctl 子命令大全
Unit Commands:
list-units [PATTERN...] List units currently in memory
list-sockets [PATTERN...] List socket units currently in memory,
ordered by address
list-timers [PATTERN...] List timer units currently in memory,
ordered by next elapse
start UNIT... Start (activate) one or more units
stop UNIT... Stop (deactivate) one or more units
reload UNIT... Reload one or more units
restart UNIT... Start or restart one or more units
try-restart UNIT... Restart one or more units if active
reload-or-restart UNIT... Reload one or more units if possible,
otherwise start or restart
try-reload-or-restart UNIT... If active, reload one or more units,
if supported, otherwise restart
isolate UNIT Start one unit and stop all others
kill UNIT... Send signal to processes of a unit
clean UNIT... Clean runtime, cache, state, logs or
configuration of unit
is-active PATTERN... Check whether units are active
is-failed PATTERN... Check whether units are failed
status [PATTERN...|PID...] Show runtime status of one or more units
show [PATTERN...|JOB...] Show properties of one or more
units/jobs or the manager
cat PATTERN... Show files and drop-ins of specified units
set-property UNIT PROPERTY=VALUE... Sets one or more properties of a unit
help PATTERN...|PID... Show manual for one or more units
reset-failed [PATTERN...] Reset failed state for all, one, or more
units
list-dependencies [UNIT...] Recursively show units which are required
or wanted by the units or by which those
units are required or wanted
Unit File Commands:
list-unit-files [PATTERN...] List installed unit files
enable [UNIT...|PATH...] Enable one or more unit files
disable UNIT... Disable one or more unit files
reenable UNIT... Reenable one or more unit files
preset UNIT... Enable/disable one or more unit files
based on preset configuration
preset-all Enable/disable all unit files based on
preset configuration
is-enabled UNIT... Check whether unit files are enabled
mask UNIT... Mask one or more units
unmask UNIT... Unmask one or more units
link PATH... Link one or more units files into
the search path
revert UNIT... Revert one or more unit files to vendor
version
add-wants TARGET UNIT... Add 'Wants' dependency for the target
on specified one or more units
add-requires TARGET UNIT... Add 'Requires' dependency for the target
on specified one or more units
edit UNIT... Edit one or more unit files
get-default Get the name of the default target
set-default TARGET Set the default target
Machine Commands:
list-machines [PATTERN...] List local containers and host
Job Commands:
list-jobs [PATTERN...] List jobs
cancel [JOB...] Cancel all, one, or more jobs
Environment Commands:
show-environment Dump environment
set-environment VARIABLE=VALUE... Set one or more environment variables
unset-environment VARIABLE... Unset one or more environment variables
import-environment [VARIABLE...] Import all or some environment variables
Manager State Commands:
daemon-reload Reload systemd manager configuration
daemon-reexec Reexecute systemd manager
log-level [LEVEL] Get/set logging threshold for manager
log-target [TARGET] Get/set logging target for manager
service-watchdogs [BOOL] Get/set service watchdog state
System Commands:
is-system-running Check whether system is fully running
default Enter system default mode
rescue Enter system rescue mode
emergency Enter system emergency mode
halt Shut down and halt the system
poweroff Shut down and power-off the system
reboot [ARG] Shut down and reboot the system
kexec Shut down and reboot the system with kexec
exit [EXIT_CODE] Request user instance or container exit
switch-root ROOT [INIT] Change to a different root file system
suspend Suspend the system
hibernate Hibernate the system
hybrid-sleep Hibernate and suspend the system
suspend-then-hibernate Suspend the system, wake after a period of
time, and hibernate
systemd-analyze
# 查看启动耗时
$ systemd-analyze
# 查看每个服务的启动耗时
$ systemd-analyze blame
# 显示瀑布状的启动过程流
$ systemd-analyze critical-chain
# 显示指定服务的启动流
$ systemd-analyze critical-chain atd.service
hosnametctl
# 显示当前主机的信息
$ hostnamectl
# 设置主机名。
$ sudo hostnamectl set-hostname rhel7
localectl
# 查看本地化设置
$ localectl
# 设置本地化参数。
$ sudo localectl set-locale LANG=en_GB.utf8
$ sudo localectl set-keymap en_GB
timedatectl
# 查看当前时区设置
$ 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
loginctl
#查看当前登录的用户
$ loginctl
journalctl
管理日志的命令
# 查看所有日志(默认情况下 ,只保存本次启动的日志)
$ 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
如: