首页 > 代码库 > RHEL7下的systemd
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
anaconda-shell@.service poweroff.target
anaconda-sshd.service poweroff.target.wants
anaconda.target printer.target
anaconda.target.wants proc-fs-nfsd.mount
anaconda-tmux@.service 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
autovt@.service 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
configure-printer@.service 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 serial-getty@.service
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 sshd@.service
getty@.service 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 systemd-backlight@.service
initrd-switch-root.target.wants systemd-binfmt.service
initrd.target systemd-fsck-root.service
initrd.target.wants systemd-fsck@.service
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 systemd-nspawn@.service
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
lvm2-pvscan@.service systemd-sysctl.service
machine.slice systemd-timedated.service
mdmonitor.service systemd-tmpfiles-clean.service
mdmon@.service 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 teamd@.service
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 wacom-inputattach@.service
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
本文出自 “云人生” 博客,请务必保留此出处http://ovcer.blog.51cto.com/1145188/1426575