Systemd ???????? ??????????: ?????? ????? ???
Systemd ?????????? ??????????: ?????? ??? ??. ??? ???, Systemd ?? ?????????? ???? ??????? ??????????? 3.1 systemctl 3.2 systemd-analyze 3.3 hostnamectl 3.4 localectl 3.5 timedatectl 3.6 loginctl ???, ????? ?.? ???? ?.? ??????? ?????? ?.? ????? ???????? ?.? ???????????? ????, ??????? ?????????? ???? ?.? ?????????? ?.? ?????????? ?????? ?????? ?.? ?????????? ?????? ??????? 5.4 ?????????? ?????? ???? ???, ?????? ???. ?? ??????????? ?????????? ????
????: ?????? ???????
?????: ???? ????? ? ?????
Systemd ?? ??????? ????????? ???? ???, ?? ???? ???? ??????? ???? ?????????? ?????? ??? ???????? ??????? ??? ??????????? ????? ???????
?? ??????? ?? ????? ??????? ?????? ??? ??????, ?? ????? ??? ????? ????? ???? ??????? ?? ??? ?? ?????? ?????????? ???????? ????,
??????? ????
??????? ???? ?????? ??????? ????? ?? ?????? ?????
???????,
????????? ????
???? ????? ??? ??????
init
???????????
????? ???????? ??????? ???? ???? ???? ??????? ??? ????
xxxxxxxxxx
$ sudo /etc/init.d/apache2 start
# ????
$ service apache2 start
?? ??????? ???? ??????? ???????
???? ?? ???? ???? ???? ??????
init
?????????????? ????????????? ???? ???, ????????? ?????????? ?????????? ??????? ??? ??????? ?????????? ???? ????
?????????, ????????? ???? ??? ?????
init
?????????? ????????? ???????? ????????? ??????, ???? ???? ????? ?????? ??? ??? ????????????? ??????? ????????? ???????? ???? ???, ?? ???????? ????????????? ??? ????? ??? ?????
Systemd ?? ?????????? ???????? ???? ???? ??????? ?? ?????? ?????? ?? ????????? ???? ??? ???????????? ???? ???? ???????? ?????? ?????? ????
????????? ?????? ???????, ?????
d
??? ???? (daemon) ?? ????????? ???? Systemd ?? ?????? ???? ??? ??? ???? ????????? ????? ?????
(????? ????? Systemd ??????) Lennart Poettering )
Systemd ??????? ???? ?? ????? ???
init
???????? ??????????? ?????
initd
????????? ????? ?????????? ???? ??? (PID ???? 1), ???? ?????????????? ?? ??????? ???????????
xxxxxxxxxx
$ systemctl --version
????? ???????? Systemd ?? ??????? ???????
Systemd 的優(yōu)點(diǎn)是功能強大,使用方便,缺點(diǎn)是體系龐大,非常復雜。事實(shí)上,現在還有很多人反對使用 Systemd,理由就是它過(guò)于復雜,與操作系統的其他部分強耦合,違反"keep simple, keep stupid"的
??????? ?????
(????? ??????? Systemd ???????? ?????)
Systemd ???? ?????? ???, ??? ??? ???? ???????? ???, ?? ??????? ???????????? ??????? ????? ???? ??????????
systemctl
??? Systemd ?? ?????? ??????, ?? ??????? ????????? ???? ??????? ????
xxxxxxxxxx
# ??????? ??????? ???? ????
$ sudo systemctl reboot
?
# ??????? ???? ????, ??????? ???? ???
$ sudo systemctl poweroff
?
# CPU ??? ??? ???? ??? ???????
$ sudo systemctl halt
?
# ??????? ?????? ????
$ sudo systemctl suspend
?
# ????????? ???????? ???????? ?????? ????
$ sudo systemctl hibernate
?
# ????????? ?????????????? ????? ???? ?????? ????
$ sudo systemctl hybrid-sleep
?
# ?????? ???????? ?????? ???? (??? ??????????? ????????)
$ sudo systemctl rescue
systemd-analyze
????????? ???? ????? ???? ???????
xxxxxxxxxx
# ???? ???? ?????
$ systemd-analyze ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
# ??????? ???????? ???? ?????? ???? ?????
$ systemd-analyze blame
?
# ?????????? ??? ?????? ??????????? ?????? ????????
$ systemd-analyze critical-chain
?
????????? ???????? ???? ?????? ?????
$ systemd-analyze critical-chain atd.service
hostnamectl
??? ??????? ??????? ???? ????? ??????? ???????
xxxxxxxxxx
# ??????? ??????? ???? ?????
$ hostnamectl
?
# ???????? ??? ?????
$ sudo hostnamectl set-hostname rhel7
localectl
????????????? ?????? ????? ???? ???????
xxxxxxxxxx
# ??????????? ?????? ?????
$ 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
??????? ?? ?? ??? ?????????????? ????? ???? ???????
xxxxxxxxxx
# ??????? ???? ?????? ????
$ loginctl list-sessions
?
??????? ???? ??? ?????????????? ?????? ????
$ loginctl list-users
?
????????? ???????????? ???? ???????? ?????
$ loginctl show-user ruanyf
Systemd ????? ??????? ????? ???????? ???? ????? ????? ????? ??????? ???????? ????? (Unit) ??? ????
????? ??? 12 ????? ???????
systemctl list-units
?????? ??????? ????????? ?? ????? ????? ?????
xxxxxxxxxx
# ????? ????????? ?????? ????
$ systemctl list-units
?
# ????? ????? ??????????? ????, ??? ????? ?????????? ???? ?????? ?????? ?? ???? ??? ?????? ???????
$ systemctl list-units --all
?
# ????? ????? ??????? ?????? ???? ????
$ systemctl list-units --all --state=inactive
?
# ????? ??? ?????? ????? ?????? ????
$ systemctl list-units --failed
?
# ????? ?????, service ????? ????? ??????????? ????
$ systemctl list-units --type=service
systemctl status
???????? ????????? ?????? ??? ???? ??? ??????? ?????? ????? ??????? ????
xxxxxxxxxx
# ????????? ?????? ???????? ????
$ systemctl status
?
# ???? ??????? ?????? ???????? ????
$ sysystemctl status bluetooth.service
?
???????? ??????? ???? ??????? ?????? ???????? ?????
$ systemctl -H root@rhel7.example.com status httpd.service
????
status
????,
systemctl
??????? ????? ??????????? ?????? ??? ?????? ?????? ??? ??????, ???? ??????????? ?????????? ????? ?????? ????????? ?????
xxxxxxxxxx
# ???? ????? ????? ?? ?? ?? ?????
$ systemctl is-active application.service
?
# ???? ????? ?? ???? ?????? ???????? ??? ?? ???????? ????
$ systemctl is-failed application.service
?
# ???? Unit ????? ???? ??????? ????? ???? ?????? ?? ?? ?? ?????
$ systemctl is-enabled application.service
?????????????? ????, ??????? ?????? ?????????? ???? ?????? ??????, ?? ????? (???? ???????) ???? ??? ???? ???? ???? ??????? ????
xxxxxxxxxx
# ???? ??????? ?????????????? ???? ????
$ 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
?
???? ??????? ????? ????? ??????????? ?????
$ systemctl show httpd.service
?
???? ??????? ????????? ????? ??? ???????? ?????
$ systemctl show -p CPUShares httpd.service
?
# ????????? ??????? ????????? ????????? ??? ???
$ sudo systemctl set-property httpd.service CPUShares=500
?????????? ????? ??????????? ??????: A B ?? ??? ?????? ???, ?? ???? ?? ?? Systemd ??? A ???? ???, ??? ??? ??? ???? B-? ???? ?????
systemctl list-dependencies
???? ??????? ????? ???????????? ??????????? ???? ???????
xxxxxxxxxx
$ systemctl list-dependencies nginx.service
????? ???????? ??????? ???? ???????????? ??????? ???????? (???? ????????? ?????), ?? ?????????? ????????? ??? ??? ??? ??????????? ????? ???, ????? ?????? ??????? ???? ???
--all
????????????
xxxxxxxxxx
$ systemctl list-dependencies --all nginx.service
??????? ??????? ???? ?????????? ???? ????, ?? ?????????? ?????? ?????? ??? ??????? ???? ???? ????
Systemd ?????????? ????????? ????
/etc/systemd/system/
?????????? ???? ?????? ???, ?? ????? ???? ??????? ????? ???? ?????????? ???? ??????? ??? ???????? ??????
/usr/lib/systemd/system/
???? ?????????? ?????? ??? ??????????? ???? ???????
systemctl enable
?? ???????? ????? ???? ?????????? ????? ???? ??????? ????? ??????? ?????? ???? ??????? ????
xxxxxxxxxx
$ sudo systemctl enable clamd@scan.service
# ???? ????
$ sudo ln -s '/usr/lib/systemd/system/clamd@scan.service' '/etc/systemd/system/multi-user.target.wants/clamd@scan.service'
??? ?????????? ????? ??? ???? ???? ???? ???? ???? ??? ??? ????,
systemctl enable
????????? ??????? ???? ???? ???? ??????? ????
?? ???? ??????????????,
systemctl disable
???? ?????????? ????? ???? ?????????? ????? ???????? ????? ??????? ????? ???? ???? ??????, ?? ??? ????????? ????? ???? ?????
xxxxxxxxxx
$ sudo systemctl disable clamd@scan.service
?????????? ?????? ????????? ?? ??? ??????? ??????, ????
sshd.socket
??? ??? ?????? ???, Systemd ?? ?????? ???? ??? ??
.service
?????
sshd
???? ???
sshd.service
。
systemctl list-unit-files
?????? ????? ?????????? ???? ??????????? ???? ??????? ????
xxxxxxxxxx
# ????? ?????????? ???? ??????????? ????
$ systemctl list-unit-files
?
????????? ????? ?????????? ???????? ??????????? ????
$ systemctl list-unit-files --type=service
?? ???????? ???? ?????? ?????? ?????
xxxxxxxxxx
$ systemctl list-unit-files
?
UNIT FILE ? ? ? ? ? ? STATE
chronyd.service ? ? ? enabled
clamd@.service ? ? ? ? static
clamd@scan.service ? ? disabled
?? ???????? ??????? ???????????? ??????? ?????? ??????, ??? ??? ??????
[Install]
??? (??????? ??? ???? ??), ????????? ???????? ?????????? ?????? ?????????? ??????? ??? ???? ?????
???? ??? ?????? ????, ?????????? ?????? ?????? ???? ???? ???? ?? ??, ?? ??????? ???? ????? ??? ?????? ???????? ????? ???? ????
systemctl status
?????
xxxxxxxxxx
$ systemctl status bluetooth.service
????? ?????????? ???? ???????? ???? ??, SystemD-?? ?????????? ???? ??????? ??? ???? ??? ??? ????? ??????? ???? ???? ???, ???????? ???????? ??????? ??? ???
xxxxxxxxxx
$ sudo systemctl daemon-reload
$ sudo systemctl restart httpd.service
?????????? ???? ?? ???? ?????? ?????? ????, ?? ?????? ????? ????? ???? ?????
systemctl cat
????????? ?????????? ?????? ?????????? ????? ?????
xxxxxxxxxx
$ systemctl cat atd.service
?
[Unit]
Description=ATD daemon
?
[Service]
Type=forking
ExecStart=/usr/bin/atd
?
[Install]
WantedBy=multi-user.target
????? ?????? ???? ???? ??????, ?????????? ?????? ??????? ????? ??? ??? ??????? ??????? ?????? ????? ??????, ???? ???????? ?????? ?????? ??????? ????? ??????? ????????? ???, ????
[Unit]
???? ??? ?????? ????, ?????????? ?????? ???? ??? ??? ????????? ??? ????? ??? ??? ??? ????? ??????? ????? ??????????
??????? ?????? ????????? ???? ???? ????? ?????? ??????? ??-????? ???? ???????
xxxxxxxxxx
[Section]
Directive1=value
Directive2=value
?
. . .
[Unit]
???? ??????? ?????????? ?????? ????? ????, ?? ??????? ???????? ?????????? ???? ??? ???????? ??????? ???? ??????? ??????? ???? ??????? ???? ?? ?????? ??????????? ???? ?????? ????
Dsecription
????????? ??????
Documentation
?????????? ??????
Requires
??????? ??????? ????????? ???????? ????? ??? ???? ?? ????, ????? ??????? ????? ???? ??? ?????? ????
Wants
??????? ??????? ???? ??????? ???????? ?????????, ??? ?????? ????? ?? ????, ??? ??????? ??????? ?????????? ???? ??? ???
BindsTo
???
Requires
??? ?????? ??? ?? ????????? ????? ??? ???? ???? ????, ????? ??????? ????? ??? ??? ???? ??? ?????
Before
??? ????????? ????????? ?????? ???? ???? ???, ??? ??? ??????? ??????? ??? ???? ???? ????
After
??? ?? ????????? ????????? ?????? ???? ???? ???, ????? ??? ??????? ??????? ??? ???? ???? ????
Conflicts
????? ????????? ??????? ??????? ??????? ???? ?????? ?????? ???? ???
Condition...
??????? ????? ??????? ???? ??????????? ???????? ???? ???? ???, ???????? ??? ???? ???
Assert...
??????? ??????? ???? ???? ?? ???????? ???? ???? ???, ?? ?? ??? ??? ???? ??? ?????? ????
[Install]
??????? ??? ?????????? ?????? ????? ????, ?? ?????? ???? ???? ??? ??? ?? ?? ??? ???? ???? ?? ???????? ???? ?? ?????? ??????????? ?????????
WantedBy
?? ??? ???? ?? ?????? ???????, ??????? ????? ??????? ??? (???????) ?????? ????? ?????? ??? ????
/etc/systemd/system
???? ???? ??????
??????? ??? +
.wants
????????? ???? ??????? ?????
RequiredBy
?? ??? ???? ?? ?????? ???????, ??????? ????? ??????? ?????????, ???????? ????????? ???? ????
/etc/systemd/system
???? ???? ??????
??????? ??? +
.required
????????? ???? ??????? ?????
Alias
??????? ??????? ???? ?????? ???? ???? ?????
Also
??????? ????? ??????? (?????) ???, ??? ???? ??????? ????? ???????? ??????
[Service]
????????? ???????????? ???? ????, ????????? ??????? ?????? ??????? ???? ?? ?????? ??????? ?? ?????? ??????????? ?????????
Type
???? ???? ???? ??????????? ???? ?????????? ????? ?? ?????????? ??????? ??? ???????
Type=simple
?????? ???, ??????? ????
ExecStart
????????? ??????, ?????? ?????????? ???? ?????
Type=forking
: ????????? ?????????? ???? ???? ???????? ???? ???? ?????????? ???? ??? ???, ???? ?????? ?? ???????? ?????????? ????????????? ??? ???? ?????
Type=oneshot
??? ??????????, Systemd ??????? ??????? ??? ????? ??????? ??????? ????, ????? ??????? ????????? ??????? ?????
Type=dbus
??????? ???? D-Bus ?? ??????? ???? ????
Type=notify
??????? ???? ???? ???? ????, ?????? ????
Systemd
??? ??????? ????
Type=idle
??? ???? ???? ??? ??????? ???, ???? ??????? ??????? ???? ????
ExecStart
??????? ?????? ???? ???? ??????
ExecStartPre
??????? ??????? ???? ???? ??? ????????? ??????
ExecStartPost
??????? ???? ???? ???? ?? ??????? ??? ??? ??? ????
ExecReload
??????? ????????? ??????? ???? ???? ???? ??????? ??? ??? ??? ??????
ExecStop
??????? ??????? ???? ???? ???? ???????? ??????
ExecStopPost
: ??????? ???? ???? ??? ??????? ??? ???? ???? ????
RestartSec
??????? ????? ???????????? ??????? ???? ???? ???? ???????? (????????)
Restart
??? ??????????? Systemd ???????????????? ??????? ??????? ??????? ???? ????, ???????? ??? ???????????:
always
(?????? ?????????)
on-success
、
on-failure
、
on-abnormal
、
on-abort
、
on-watchdog
TimeoutSec
Systemd ??????? ??????? ???? ???? ??? ??????? ???? ????????? ?????? ?????????? ?????
Environment
????????? ?????? ???????????
????? ?????????? ?????? ???????? ????????? ??????? ????, ???? ??? ?????? ?????? ????????
????????? ???? ???? ???? ???? ????? ???? ???? ???? ??? ???????? ???? ???? ???? ??????? ??????? ????????? ?????? ???? ???, ??? ??? ???????? ??? ??????????? Systemd ?? ?????? ?? ????????
簡(jiǎn)單說(shuō),Target 就是一個(gè) Unit 組,包含許多相關(guān)的 Unit 。啟動(dòng)某個(gè) Target 的時(shí)候,Systemd 就會(huì )啟動(dòng)里面所有的 Unit。從這個(gè)意義上說(shuō),Target這個(gè)概念類(lèi)似于"狀態(tài)點(diǎn)",啟動(dòng)某個(gè) Target 就好比啟動(dòng)到某種狀態(tài)。
???????
init
????????? ????, RunLevel ?? ????? ???, ?? Target ?? ??????????? ???? ???? ????????????? ???????? ???, RunLevel ????????????? ??? ????? ??? ????, ?????? RunLevel ?????? ???? ??? ???? ??, ?????? ?????? Target ?????? ???? ??? ?????
xxxxxxxxxx
# ??????? ????????? ?? ??????? ?????
$ systemctl list-unit-files --type=target
?
# ???? ?????? ??????????? ????? ????? ?????
$ systemctl list-dependencies multi-user.target
?
# ???? ???? ???? ?????? ??????? ?????
$ systemctl get-default
?
# ????? ???? ?????? ?????? ??? ????
$ sudo systemctl set-default multi-user.target
?
# ??????? ???????? ???? ????, ?????????? ????????? ???? ??? ?????????? ?????????? ???? ??? ??,
# systemctl isolate ?????? ?? ???? ???????? ???,
# ??????? ????????? ????? ?? ?????????? ???? ???? ?? ???? ????????? ??????????? ???
$ sudo systemctl isolate multi-user.target
Target ??? ??????? RunLevel ?? ????? ??????? ????????:
xxxxxxxxxx
Traditional runlevel ? ? New target name ? ? Symbolically linked to...
?
Runlevel 0 ? ? ? ? ? | ? runlevel0.target -> poweroff.target
Runlevel 1 ? ? ? ? ? | ? runlevel1.target -> rescue.target
Runlevel 2 ? ? ? ? ? | ? runlevel2.target -> multi-user.target
Runlevel 3 ? ? ? ? ? | ? runlevel3.target -> multi-user.target
Runlevel 4 ? ? ? ? ? | ? runlevel4.target -> multi-user.target
Runlevel 5 ? ? ? ? ? | ? runlevel5.target -> graphical.target
Runlevel 6 ? ? ? ? ? | ? runlevel6.target -> reboot.target
??? ????
init
??????????? ?????? ???????????? ?????????
?????? ????????
(???)
/etc/inittab
???? ??????) ??? ?????? ??????? ?????? ???????????? ??????, ??????? ??
/etc/systemd/system/default.target
??????? ????????? ??????? ???
graphical.target
(????????? ?????????) ????
multi-user.target
(??? ??????????? ?????? ????)
(?) ????????? ???? ???? ???????
??? ???
/etc/init.d
?????????, ??????? ??? ????? ??????????? ???????? ????? (????
/etc/rc3.d
、
/etc/rc5.d
??? ??? ???????? ???
/lib/systemd/system
???
/etc/systemd/system
???????????
(?) ?????????? ?????? ???????
???
init
??????????? ?????????? ???? ??
/etc/inittab
??????? ???????? ?????????? ???????? ???????? ????
/etc/sysconfig
????? ???????? ?????????? ???????? ??????? ???????? ??????
/lib/systemd
??????, ?????
/etc/systemd
?????????? ????? ???????????? ??? ???????? ???????? ???? ?????
Systemd ????? ??????? ??????? ?? ??????????? ???????? ???? ?? ??? ?????? ???, ???? ?????????
journalctl
???? ??????, ????? ?? ????? ???? (?????? ?? ??? ???????????? ??)? ???? ?????????? ???? ??
/etc/systemd/journald.conf
。
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
?
# ???? ??????? ?? ?????
$ sudo journalctl -u nginx.service
$ sudo journalctl -u nginx.service --since today
?
# ???? ??????? ??????? ?????? ?????? ????? ????? ??????
$ sudo journalctl -u nginx.service -f
?
?????? ??????? ?? ??????? ??? ???????? ?????
$ 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
{
"翻譯": "JSON ???????? (?????? ????) ?????? ????, ?? ??? ????? ??????????"
}
```
$ sudo journalctl -b -u nginx.serviceqq
-o json-pretty
?
# ?? ???? ?????? ?????? ????? ????? ???????? ????
$ sudo journalctl --disk-usage
?
?? ?????? ???????? ????? ???????? ????
$ sudo journalctl --vacuum-size=1G
?
# ?? ???? ??????? ????? ???
$ sudo journalctl --vacuum-time=1years
(??????)
???? ?????????????? ??????? ??????: