RHEL7下的systemd

    systemd是Linux下的系統和服務管理器,兼容SysV和LSB的啓動腳本。對於systemd有以下特性:支持並行化任務、同時採用socket和D-BUS總線式激活服務、按需啓動相應的守護進程、利用Linux的cgroup監控進程、支持快照和系統恢復、維護掛載點和自動掛載點,各服務間基於依賴關係進行精密控制。

一、分析系統狀態
1.輸出激活的單元
[root@localhost ~]# systemctl
UNIT                                            LOAD                       ACTIVE SUB       DESCRIPTION
proc-sys...t_misc.automount     loaded active waiting   Arbitrary Executable File Fo
sys-devi...-sda-sda1.device      loaded active plugged   VMware_Virtual_S
sys-devi...-sda-sda2.device      loaded active plugged   LVM PV lv3Fxq-bu9J-pkq2-wMtN
sys-devi...block-sda.device      loaded active plugged   VMware_Virtual_S
sys-devi...ooth-hci0.device      loaded active plugged   /sys/devices/pci0000:00/0000
sys-devi...o16777736.device  loaded active plugged   PRO/1000 MT Single Port Adap
sys-devi...block-sr0.device loaded active plugged   VMware_Virtual_SATA_CDRW_Dri
sys-devi...block-fd0.device loaded active plugged   /sys/devices/platform/floppy
sys-devi...tty-ttyS2.device loaded active plugged   /sys/devices/platform/serial
sys-devi...tty-ttyS3.device loaded active plugged   /sys/devices/platform/serial
sys-devi...tty-ttyS0.device loaded active plugged   /sys/devices/pnp0/00:09/tty/
sys-devi...tty-ttyS1.device loaded active plugged   /sys/devices/pnp0/00:0a/tty/
sys-devi...k-dm\x2d0.device loaded active plugged   /sys/devices/virtual/block/d
sys-devi...k-dm\x2d1.device loaded active plugged   /sys/devices/virtual/block/d
sys-module-configfs.device  loaded active plugged   /sys/module/configfs
sys-module-fuse.device      loaded active plugged   /sys/module/fuse
sys-subs...ices-hci0.device loaded active plugged   /sys/subsystem/bluetooth/dev
sys-subs...o16777736.device loaded active plugged   PRO/1000 MT Single Port Adap
-.mount                     loaded active mounted   /
boot.mount                  loaded active mounted   /boot
dev-hugepages.mount         loaded active mounted   Huge Pages File System
dev-mqueue.mount            loaded active mounted   POSIX Message Queue File Sys
proc-fs-nfsd.mount          loaded active mounted   RPC Pipe File System
run-user-1000-gvfs.mount    loaded active mounted   /run/user/1000/gvfs
sys-fs-f...onnections.mount loaded active mounted   FUSE Control File System
sys-kernel-config.mount     loaded active mounted   Configuration File System
sys-kernel-debug.mount      loaded active mounted   Debug File System
var-lib-...rpc_pipefs.mount loaded active mounted   RPC Pipe File System
brandbot.path               loaded active waiting   Flexible branding
cups.path                   loaded active waiting   CUPS Printer Service Spool
systemd-...rd-plymouth.path loaded active waiting   Forward Password Requests to
systemd-...ssword-wall.path loaded active waiting   Forward Password Requests to
session-1.scope             loaded active running   Session 1 of user admin
abrt-ccpp.service           loaded active exited    Install ABRT coredump hook
abrt-oops.service           loaded active running   ABRT kernel log watcher
abrt-xorg.service           loaded active running   ABRT Xorg log watcher
abrtd.service               loaded active running   ABRT Automated Bug Reporting
accounts-daemon.service     loaded active running   Accounts Service
alsa-state.service          loaded active running   Manage Sound Card State (res
atd.service                 loaded active running   Job spooling tools
auditd.service              loaded active running   Security Auditing Service
avahi-daemon.service        loaded active running   Avahi mDNS/DNS-SD Stack
bluetooth.service           loaded active running   Bluetooth service
chronyd.service             loaded active running   NTP client/server
colord.service              loaded active running   Manage, Install and Generate
crond.service               loaded active running   Command Scheduler
cups.service                loaded active running   CUPS Printing Service


[root@localhost ~]# systemctl list-units
UNIT                        LOAD   ACTIVE SUB       DESCRIPTION
proc-sys...t_misc.automount loaded active waiting   Arbitrary Executable File Fo
sys-devi...-sda-sda1.device loaded active plugged   VMware_Virtual_S
sys-devi...-sda-sda2.device loaded active plugged   LVM PV lv3Fxq-bu9J-pkq2-wMtN
sys-devi...block-sda.device loaded active plugged   VMware_Virtual_S
sys-devi...ooth-hci0.device loaded active plugged   /sys/devices/pci0000:00/0000
sys-devi...o16777736.device loaded active plugged   PRO/1000 MT Single Port Adap
sys-devi...block-sr0.device loaded active plugged   VMware_Virtual_SATA_CDRW_Dri
sys-devi...block-fd0.device loaded active plugged   /sys/devices/platform/floppy
sys-devi...tty-ttyS2.device loaded active plugged   /sys/devices/platform/serial
sys-devi...tty-ttyS3.device loaded active plugged   /sys/devices/platform/serial
sys-devi...tty-ttyS0.device loaded active plugged   /sys/devices/pnp0/00:09/tty/
sys-devi...tty-ttyS1.device loaded active plugged   /sys/devices/pnp0/00:0a/tty/
sys-devi...k-dm\x2d0.device loaded active plugged   /sys/devices/virtual/block/d
sys-devi...k-dm\x2d1.device loaded active plugged   /sys/devices/virtual/block/d
sys-module-configfs.device  loaded active plugged   /sys/module/configfs
sys-module-fuse.device      loaded active plugged   /sys/module/fuse

 

輸出運行失敗的單元
[root@localhost ~]# systemctl --failed
UNIT          LOAD   ACTIVE SUB    DESCRIPTION
rhnsd.service loaded failed failed LSB: Starts the Spacewalk Daemon

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

1 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.


所有可用的單元文件存放在/usr/lib/systemd/system/和/etc/systemd/system目錄中,/etc/systemd/system中單元的優先級高於/usr/lib/systemd/system

[root@localhost ~]# ls /usr/lib/systemd/system/
abrt-ccpp.service                        packagekit-offline-update.service
abrtd.service                            paths.target
abrt-oops.service                        plymouth-halt.service
abrt-pstoreoops.service                  plymouth-kexec.service
abrt-vmcore.service                      plymouth-poweroff.service
abrt-xorg.service                        plymouth-quit.service
accounts-daemon.service                  plymouth-quit-wait.service
alsa-restore.service                     plymouth-read-write.service
alsa-state.service                       plymouth-reboot.service
alsa-store.service                       plymouth-start.service
anaconda-direct.service                  plymouth-switch-root.service
anaconda-noshell.service                 polkit.service
anaconda.service                         postfix.service
[email protected]                  poweroff.target
anaconda-sshd.service                    poweroff.target.wants
anaconda.target                          printer.target
anaconda.target.wants                    proc-fs-nfsd.mount
[email protected]                   proc-sys-fs-binfmt_misc.automount
arp-ethers.service                       proc-sys-fs-binfmt_misc.mount
atd.service                              psacct.service
auditd.service                           qemu-guest-agent.service
[email protected]                          quotaon.service
avahi-daemon.service                     radvd.service
avahi-daemon.socket                      rc-local.service
basic.target                             rdisc.service
basic.target.wants                       realmd.service
blk-availability.service                 reboot.target
bluetooth.service                        reboot.target.wants
bluetooth.target                         remote-fs-pre.target
brandbot.path                            remote-fs.target
brandbot.service                         rescue.service
brltty.service                           rescue.target
canberra-system-bootup.service           rhel-autorelabel-mark.service
canberra-system-shutdown-reboot.service  rhel-autorelabel.service
canberra-system-shutdown.service         rhel-configure.service
chronyd.service                          rhel-dmesg.service
chrony-wait.service                      rhel-domainname.service
colord.service                           rhel-import-state.service
[email protected]               rhel-loadmodules.service
console-getty.service                    rhel-readonly.service
console-shell.service                    rhsmcertd.service
cpupower.service                         rngd.service
crond.service                            rpcbind.service
cryptsetup.target                        rpcbind.socket
ctrl-alt-del.target                      rpcbind.target
cups-browsed.service                     rpcgssd.service
cups.path                                rpcidmapd.service
cups.service                             rpcsvcgssd.service
cups.socket                              rsyncd.service
dbus-org.freedesktop.hostname1.service   rsyncd.socket
dbus-org.freedesktop.locale1.service     rsyslog.service
dbus-org.freedesktop.login1.service      rtkit-daemon.service
dbus-org.freedesktop.machine1.service    runlevel0.target
dbus-org.freedesktop.timedate1.service   runlevel1.target
dbus.service                             runlevel1.target.wants
dbus.socket                              runlevel2.target
dbus.target.wants                        runlevel2.target.wants
debug-shell.service                      runlevel3.target
default.target                           runlevel3.target.wants
default.target.wants                     runlevel4.target
dev-hugepages.mount                      runlevel4.target.wants
dev-mqueue.mount                         runlevel5.target
dm-event.service                         runlevel5.target.wants
dm-event.socket                          runlevel6.target
dmraid-activation.service                saslauthd.service
dnsmasq.service                          [email protected]
dracut-cmdline.service                   shutdown.target
dracut-initqueue.service                 shutdown.target.wants
dracut-mount.service                     sigpwr.target
dracut-pre-mount.service                 sleep.target
dracut-pre-pivot.service                 -.slice
dracut-pre-trigger.service               slices.target
dracut-pre-udev.service                  smartcard.target
dracut-shutdown.service                  smartd.service
ebtables.service                         sockets.target
emergency.service                        sockets.target.wants
emergency.target                         sound.target
fcoe.service                             speech-dispatcherd.service
final.target                             spice-vdagentd.service
firewalld.service                        spice-vdagentd.target
firstboot-graphical.service              sshd-keygen.service
fprintd.service                          sshd.service
gdm.service                              [email protected]
[email protected]                           sshd.socket
getty.target                             suspend.target
graphical.target                         svnserve.service
graphical.target.wants                   swap.target
halt-local.service                       sys-fs-fuse-connections.mount
halt.target                              sysinit.target
halt.target.wants                        sysinit.target.wants
hibernate.target                         sys-kernel-config.mount
hybrid-sleep.target                      sys-kernel-debug.mount
hypervkvpd.service                       syslog.socket
hypervvssd.service                       syslog.target.wants
initial-setup-graphical.service          sysstat.service
initial-setup-text.service               systemd-ask-password-console.path
initrd-cleanup.service                   systemd-ask-password-console.service
initrd-fs.target                         systemd-ask-password-plymouth.path
initrd-parse-etc.service                 systemd-ask-password-plymouth.service
initrd-root-fs.target                    systemd-ask-password-wall.path
initrd-switch-root.service               systemd-ask-password-wall.service
initrd-switch-root.target                [email protected]
initrd-switch-root.target.wants          systemd-binfmt.service
initrd.target                            systemd-fsck-root.service
initrd.target.wants                      [email protected]
initrd-udevadm-cleanup-db.service        systemd-halt.service
instperf.service                         systemd-hibernate.service
ip6tables.service                        systemd-hostnamed.service
ipsec.service                            systemd-hybrid-sleep.service
iptables.service                         systemd-initctl.service
irqbalance.service                       systemd-initctl.socket
iscsid.service                           systemd-journald.service
iscsid.socket                            systemd-journald.socket
iscsi.service                            systemd-journal-flush.service
iscsiuio.service                         systemd-kexec.service
iscsiuio.socket                          systemd-localed.service
kdump.service                            systemd-logind.service
kexec.target                             systemd-machined.service
kexec.target.wants                       systemd-modules-load.service
kmod-static-nodes.service                [email protected]
kpatch.service                           systemd-poweroff.service
ksm.service                              systemd-quotacheck.service
ksmtuned.service                         systemd-random-seed.service
libstoragemgmt.service                   systemd-readahead-collect.service
libvirtd.service                         systemd-readahead-done.service
libvirt-guests.service                   systemd-readahead-done.timer
lldpad.service                           systemd-readahead-drop.service
local-fs-pre.target                      systemd-readahead-replay.service
local-fs.target                          systemd-reboot.service
local-fs.target.wants                    systemd-remount-fs.service
lvm2-lvmetad.service                     systemd-shutdownd.service
lvm2-lvmetad.socket                      systemd-shutdownd.socket
lvm2-monitor.service                     systemd-suspend.service
[email protected]                     systemd-sysctl.service
machine.slice                            systemd-timedated.service
mdmonitor.service                        systemd-tmpfiles-clean.service
[email protected]                           systemd-tmpfiles-clean.timer
messagebus.service                       systemd-tmpfiles-setup-dev.service
microcode.service                        systemd-tmpfiles-setup.service
ModemManager.service                     systemd-udevd-control.socket
multipathd.service                       systemd-udevd-kernel.socket
multi-user.target                        systemd-udevd.service
multi-user.target.wants                  systemd-udev-settle.service
netcf-transaction.service                systemd-udev-trigger.service
NetworkManager-dispatcher.service        systemd-update-utmp-runlevel.service
NetworkManager.service                   systemd-update-utmp.service
NetworkManager-wait-online.service       systemd-user-sessions.service
network-online.target                    systemd-vconsole-setup.service
network-online.target.wants              system.slice
network.target                           system-update.target
nfs-blkmap.service                       [email protected]
nfs-idmap.service                        timers.target
nfs-lock.service                         timers.target.wants
nfslock.service                          time-sync.target
nfs-mountd.service                       tmp.mount
nfs-rquotad.service                      tuned.service
nfs-secure-server.service                udisks2.service
nfs-secure.service                       umount.target
nfs-server.service                       upower.service
nfs.service                              usbmuxd.service
nfs.target                               user.slice
nfs.target.wants                         var-lib-nfs-rpc_pipefs.mount
nss-lookup.target                        virtlockd.service
nss-user-lookup.target                   virtlockd.socket
ntpdate.service                          vmtoolsd.service
numad.service                            [email protected]
oddjobd.service                          wpa_supplicant.service

[root@localhost ~]# ls /etc/systemd/system/
basic.target.wants                           display-manager.service
bluetooth.target.wants                       getty.target.wants
dbus-org.bluez.service                       graphical.target.wants
dbus-org.fedoraproject.FirewallD1.service    multi-user.target.wants
dbus-org.freedesktop.Avahi.service           nfs.target.wants
dbus-org.freedesktop.ModemManager1.service   printer.target.wants
dbus-org.freedesktop.NetworkManager.service  sockets.target.wants
dbus-org.freedesktop.nm-dispatcher.service   spice-vdagentd.target.wants
default.target                               sysinit.target.wants
default.target.wants                         system-update.target.wants

 

查看所有已安裝的服務:
[root@localhost ~]# systemctl list-unit-files
UNIT FILE                                   STATE  
proc-sys-fs-binfmt_misc.automount           static 
dev-hugepages.mount                         static 
dev-mqueue.mount                            static 
proc-fs-nfsd.mount                          static 
proc-sys-fs-binfmt_misc.mount               static 
sys-fs-fuse-connections.mount               static 
sys-kernel-config.mount                     static 
sys-kernel-debug.mount                      static 
tmp.mount                                   disabled
var-lib-nfs-rpc_pipefs.mount                static 
brandbot.path                               disabled
cups.path                                   enabled
systemd-ask-password-console.path           static 
systemd-ask-password-plymouth.path          static 
systemd-ask-password-wall.path              static 
session-1.scope                             static 
session-5.scope                             static 
abrt-ccpp.service                           enabled
abrt-oops.service                           enabled
abrt-pstoreoops.service                     disabled

 

二、使用單元
     一個單元配置文件可以爲:系統服務(.service)、掛載點(.mount)、sockets(.sockets)、系統設備、交換分區/文件、啓動目標(target)、文件系統路徑、由systemd管理的計時器。如果想了解詳細的信息,可以使用man 5 systemd.unit


     使用systemctl控制單元時,通常使用單元文件的全名,包括擴展名,例如sshd.service。但是有些單元可以在systemctl中使用簡寫方式
1.如果無擴展名,systemctl默認把擴展名當作.service.例如netcfg和netcfg.service是等價的。
2.掛載點會自動轉化爲相應的.mount單元。例如/home等價於home.mount
3.設備會自動轉化爲相應的.device單元,所以/dev/sda2等價於dev-sda2.device

 

1.查看單元的運行狀態
[root@localhost ~]# systemctl status NetworkManager
NetworkManager.service - Network Manager
   Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled)
   Active: active (running) since Sun 2014-06-15 13:36:34 CST; 46min ago
 Main PID: 1078 (NetworkManager)
   CGroup: /system.slice/NetworkManager.service
           └─1078 /usr/sbin/NetworkManager --no-daemon

2.停止某個單元
[root@localhost ~]# systemctl stop NetworkManager

3.啓動某個單元
[root@localhost ~]# systemctl start NetworkManager


4.重新啓動某個單元


[root@localhost ~]# systemctl restart NetworkManager


5.讓單元重新讀取配置
[root@localhost ~]# systemctl reload NetworkManager

 

6.查看單元是否配置爲自動啓動
[root@localhost ~]# systemctl is-enabled NetworkManager
enabled

 

7.開機自動激活某個單元

(1)關閉NetworkManager單元
[root@localhost ~]# systemctl disable NetworkManager
rm '/etc/systemd/system/multi-user.target.wants/NetworkManager.service'
rm '/etc/systemd/system/dbus-org.freedesktop.NetworkManager.service'
rm '/etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service'

(2)查看NetworkManager單元開機是否自動啓動
[root@localhost ~]# systemctl is-enabled NetworkManager
disabled

(3)設置NetworkManager單元爲開機自啓動
[root@localhost ~]# systemctl enable NetworkManager
ln -s '/usr/lib/systemd/system/NetworkManager.service' '/etc/systemd/system/dbus-org.freedesktop.NetworkManager.service'
ln -s '/usr/lib/systemd/system/NetworkManager.service' '/etc/systemd/system/multi-user.target.wants/NetworkManager.service'
ln -s '/usr/lib/systemd/system/NetworkManager-dispatcher.service' '/etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service'

(4)查看是否設置成功
[root@localhost ~]# systemctl is-enabled NetworkManager
enabled

 

8.重新載入systemd,掃描新的或有變動的單元
[root@localhost ~]# systemctl daemon-reload

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章