联系:QQ(5163721)
标题: Linux7(CentOS,RHEL,OEL)和Oracle RAC环境系列–4-target(图形界面和字符界面)
作者:Lunar©版权所有[文章允许转载,但必须以链接方式注明源地址,否则追究法律责任.]
systemd可以创建不同的状态,状态提供了灵活的机制来设置启动时的配置项。这些状态是由多个unit文件组成的,状态又叫做启动目标(target)。
unit文件可以控制服务、设备、套接字和挂载点,这些文件存放在下面的两个目录下:
/etc/systemd/system/
其中/etc/systemd/system/的优先级更高。
如果需要创建自己的unit,可以修改第一个目录中的文件来进行自定义配置,而第二个目录中的文件是package安装时保存的备份。
目录/etc/systemd/system/目录中只有很少的配置文件,在这里面配置的服务优先级高于配置在/usr/lib/systemd/system/中的服务:
[root@lunar system]# ll /etc/systemd/system/|grep ohasd -rw-r--r-- 1 root root 361 Oct 9 12:29 oracle-ohasd.service [root@lunar system]# [root@lunar system]# ll /etc/systemd/system/ total 16 drwxr-xr-x. 2 root root 30 Oct 8 07:17 basic.target.wants drwxr-xr-x. 2 root root 30 Oct 7 23:08 bluetooth.target.wants lrwxrwxrwx. 1 root root 41 Oct 7 23:08 dbus-org.bluez.service -> /usr/lib/systemd/system/bluetooth.service lrwxrwxrwx. 1 root root 44 Oct 7 23:10 dbus-org.freedesktop.ModemManager1.service -> /usr/lib/systemd/system/ModemManager.service lrwxrwxrwx. 1 root root 46 Oct 7 23:04 dbus-org.freedesktop.NetworkManager.service -> /usr/lib/systemd/system/NetworkManager.service lrwxrwxrwx. 1 root root 57 Oct 7 23:04 dbus-org.freedesktop.nm-dispatcher.service -> /usr/lib/systemd/system/NetworkManager-dispatcher.service lrwxrwxrwx 1 root root 41 Oct 8 16:51 default.target -> /usr/lib/systemd/system/multi-user.target drwxr-xr-x. 2 root root 85 Oct 7 23:02 default.target.wants lrwxrwxrwx. 1 root root 35 Oct 7 23:08 display-manager.service -> /usr/lib/systemd/system/gdm.service drwxr-xr-x. 2 root root 31 Oct 7 23:02 getty.target.wants drwxr-xr-x. 2 root root 90 Oct 9 12:29 graphical.target.wants drwxr-xr-x. 2 root root 4096 Oct 9 12:29 multi-user.target.wants lrwxrwxrwx. 1 root root 38 Oct 7 23:04 mysql.service -> /usr/lib/systemd/system/mysqld.service -rw-r--r-- 1 root root 361 Oct 9 12:29 oracle-ohasd.service drwxr-xr-x. 2 root root 25 Oct 7 23:05 printer.target.wants drwxr-xr-x. 2 root root 4096 Oct 8 07:32 sockets.target.wants drwxr-xr-x. 2 root root 35 Oct 7 23:10 spice-vdagentd.target.wants drwxr-xr-x. 2 root root 4096 Oct 8 22:19 sysinit.target.wants drwxr-xr-x. 2 root root 83 Oct 7 23:05 system-update.target.wants [root@lunar system]#
基本target中,只有微码服务:
[root@lunar1 ~]# ll /etc/systemd/system/basic.target.wants 总用量 0 lrwxrwxrwx. 1 root root 41 1月 16 03:00 microcode.service -> /usr/lib/systemd/system/microcode.service [root@lunar1 ~]#
sysinit的目标:
[root@lunar1 ~]# ll /etc/systemd/system/sysinit.target.wants 总用量 0 lrwxrwxrwx. 1 root root 49 1月 16 02:57 dmraid-activation.service -> /usr/lib/systemd/system/dmraid-activation.service lrwxrwxrwx. 1 root root 37 1月 16 02:53 iscsi.service -> /usr/lib/systemd/system/iscsi.service lrwxrwxrwx. 1 root root 43 1月 16 02:54 lvm2-lvmetad.socket -> /usr/lib/systemd/system/lvm2-lvmetad.socket lrwxrwxrwx. 1 root root 44 1月 16 02:54 lvm2-lvmpolld.socket -> /usr/lib/systemd/system/lvm2-lvmpolld.socket lrwxrwxrwx. 1 root root 44 1月 16 02:54 lvm2-monitor.service -> /usr/lib/systemd/system/lvm2-monitor.service lrwxrwxrwx. 1 root root 42 1月 16 02:56 multipathd.service -> /usr/lib/systemd/system/multipathd.service [root@lunar1 ~]#
缺省目标:
[root@lunar1 ~]# ll /etc/systemd/system/default.target.wants 总用量 0 lrwxrwxrwx. 1 root root 57 1月 16 02:52 systemd-readahead-collect.service -> /usr/lib/systemd/system/systemd-readahead-collect.service lrwxrwxrwx. 1 root root 56 1月 16 02:52 systemd-readahead-replay.service -> /usr/lib/systemd/system/systemd-readahead-replay.service [root@lunar1 ~]#
多用户字符界面的目标(runlevel 3):
[root@lunar1 ~]# ll /etc/systemd/system/multi-user.target.wants/ 总用量 0 lrwxrwxrwx. 1 root root 41 1月 16 02:54 abrt-ccpp.service -> /usr/lib/systemd/system/abrt-ccpp.service lrwxrwxrwx. 1 root root 37 1月 16 02:53 abrtd.service -> /usr/lib/systemd/system/abrtd.service lrwxrwxrwx. 1 root root 41 1月 16 02:53 abrt-oops.service -> /usr/lib/systemd/system/abrt-oops.service lrwxrwxrwx. 1 root root 43 1月 16 02:54 abrt-vmcore.service -> /usr/lib/systemd/system/abrt-vmcore.service lrwxrwxrwx. 1 root root 41 1月 16 02:53 abrt-xorg.service -> /usr/lib/systemd/system/abrt-xorg.service lrwxrwxrwx. 1 root root 35 1月 16 03:01 atd.service -> /usr/lib/systemd/system/atd.service lrwxrwxrwx. 1 root root 39 1月 16 02:56 chronyd.service -> /usr/lib/systemd/system/chronyd.service lrwxrwxrwx. 1 root root 37 1月 16 02:53 crond.service -> /usr/lib/systemd/system/crond.service lrwxrwxrwx. 1 root root 42 1月 16 02:55 hypervkvpd.service -> /usr/lib/systemd/system/hypervkvpd.service lrwxrwxrwx. 1 root root 42 1月 16 02:55 hypervvssd.service -> /usr/lib/systemd/system/hypervvssd.service lrwxrwxrwx. 1 root root 42 1月 16 03:01 irqbalance.service -> /usr/lib/systemd/system/irqbalance.service lrwxrwxrwx. 1 root root 37 1月 16 02:54 kdump.service -> /usr/lib/systemd/system/kdump.service lrwxrwxrwx. 1 root root 35 1月 16 02:56 ksm.service -> /usr/lib/systemd/system/ksm.service lrwxrwxrwx. 1 root root 40 1月 16 02:56 ksmtuned.service -> /usr/lib/systemd/system/ksmtuned.service lrwxrwxrwx. 1 root root 46 1月 16 02:54 libstoragemgmt.service -> /usr/lib/systemd/system/libstoragemgmt.service lrwxrwxrwx. 1 root root 40 1月 16 02:54 libvirtd.service -> /usr/lib/systemd/system/libvirtd.service lrwxrwxrwx. 1 root root 41 1月 16 02:53 mdmonitor.service -> /usr/lib/systemd/system/mdmonitor.service lrwxrwxrwx. 1 root root 44 1月 16 03:02 ModemManager.service -> /usr/lib/systemd/system/ModemManager.service lrwxrwxrwx. 1 root root 46 1月 16 02:55 NetworkManager.service -> /usr/lib/systemd/system/NetworkManager.service lrwxrwxrwx. 1 root root 41 1月 16 02:56 nfs-client.target -> /usr/lib/systemd/system/nfs-client.target lrwxrwxrwx. 1 root root 39 1月 16 02:55 postfix.service -> /usr/lib/systemd/system/postfix.service lrwxrwxrwx. 1 root root 40 1月 16 02:52 remote-fs.target -> /usr/lib/systemd/system/remote-fs.target lrwxrwxrwx. 1 root root 36 1月 16 03:01 rngd.service -> /usr/lib/systemd/system/rngd.service lrwxrwxrwx. 1 root root 39 1月 16 02:56 rsyslog.service -> /usr/lib/systemd/system/rsyslog.service lrwxrwxrwx. 1 root root 36 1月 16 03:01 sshd.service -> /usr/lib/systemd/system/sshd.service lrwxrwxrwx. 1 root root 39 1月 16 02:54 sysstat.service -> /usr/lib/systemd/system/sysstat.service lrwxrwxrwx. 1 root root 40 1月 16 02:53 vmtoolsd.service -> /usr/lib/systemd/system/vmtoolsd.service [root@lunar1 ~]#
而/usr/lib/systemd/system下面定义的unit就太多了:
[root@lunar1 ~]# ls -lrt /usr/lib/systemd/system/|wc -l 404 [root@lunar1 ~]# ls -lrt /usr/lib/systemd/system|grep -v grep|grep graphical -rw-r--r--. 1 root root 469 3月 3 2014 firstboot-graphical.service -rw-r--r--. 1 root root 558 11月 20 12:49 graphical.target lrwxrwxrwx. 1 root root 16 1月 16 02:52 default.target -> graphical.target drwxr-xr-x. 2 root root 49 1月 16 02:52 graphical.target.wants lrwxrwxrwx. 1 root root 16 1月 16 02:52 runlevel5.target -> graphical.target [root@lunar1 ~]# ls -lrt /usr/lib/systemd/system|grep -v grep|grep multi -rw-r--r--. 1 root root 492 11月 20 12:49 multi-user.target -rw-r--r--. 1 root root 597 11月 20 23:34 multipathd.service lrwxrwxrwx. 1 root root 17 1月 16 02:52 runlevel2.target -> multi-user.target lrwxrwxrwx. 1 root root 17 1月 16 02:52 runlevel3.target -> multi-user.target lrwxrwxrwx. 1 root root 17 1月 16 02:52 runlevel4.target -> multi-user.target drwxr-xr-x. 2 root root 4096 1月 16 02:53 multi-user.target.wants [root@lunar1 ~]# ls -lrt /usr/lib/systemd/system|grep -v grep|grep runlevel -rw-r--r--. 1 root root 761 11月 20 12:49 systemd-update-utmp-runlevel.service lrwxrwxrwx. 1 root root 17 1月 16 02:52 runlevel2.target -> multi-user.target drwxr-xr-x. 2 root root 49 1月 16 02:52 runlevel1.target.wants lrwxrwxrwx. 1 root root 13 1月 16 02:52 runlevel1.target -> rescue.target lrwxrwxrwx. 1 root root 15 1月 16 02:52 runlevel0.target -> poweroff.target lrwxrwxrwx. 1 root root 17 1月 16 02:52 runlevel3.target -> multi-user.target drwxr-xr-x. 2 root root 49 1月 16 02:52 runlevel2.target.wants drwxr-xr-x. 2 root root 49 1月 16 02:52 runlevel4.target.wants lrwxrwxrwx. 1 root root 17 1月 16 02:52 runlevel4.target -> multi-user.target drwxr-xr-x. 2 root root 49 1月 16 02:52 runlevel3.target.wants lrwxrwxrwx. 1 root root 13 1月 16 02:52 runlevel6.target -> reboot.target drwxr-xr-x. 2 root root 49 1月 16 02:52 runlevel5.target.wants lrwxrwxrwx. 1 root root 16 1月 16 02:52 runlevel5.target -> graphical.target [root@lunar1 ~]# [root@lunar1 ~]# ls -lrt /usr/lib/systemd/system|grep -v grep|grep NetworkManager -rw-r--r--. 1 root root 284 11月 21 00:55 NetworkManager-wait-online.service -rw-r--r--. 1 root root 419 11月 21 00:55 NetworkManager.service -rw-r--r--. 1 root root 353 11月 21 00:55 NetworkManager-dispatcher.service [root@lunar1 ~]# ls -lrt /usr/lib/systemd/system|grep -v grep|grep network -rw-r--r--. 1 root root 585 11月 20 12:49 systemd-networkd.socket -rw-r--r--. 1 root root 480 11月 20 12:49 network.target -rw-r--r--. 1 root root 461 11月 20 12:49 network-pre.target -rw-r--r--. 1 root root 464 11月 20 12:49 network-online.target lrwxrwxrwx. 1 root root 24 1月 16 02:52 dbus-org.freedesktop.network1.service -> systemd-networkd.service drwxr-xr-x. 2 root root 47 1月 16 02:55 network-online.target.wants [root@lunar1 ~]# ls -lrt /usr/lib/systemd/system|grep -v grep|grep rsyslog -rw-r--r--. 1 root root 288 11月 20 19:34 rsyslog.service [root@lunar1 ~]# ls -lrt /usr/lib/systemd/system|grep -v grep|grep udev -rw-r--r--. 1 root root 751 11月 20 12:49 systemd-udev-trigger.service -rw-r--r--. 1 root root 827 11月 20 12:49 systemd-udev-settle.service -rw-r--r--. 1 root root 829 11月 20 12:49 systemd-udevd.service -rw-r--r--. 1 root root 570 11月 20 12:49 systemd-udevd-kernel.socket -rw-r--r--. 1 root root 578 11月 20 12:49 systemd-udevd-control.socket -rw-r--r--. 1 root root 668 11月 20 12:49 initrd-udevadm-cleanup-db.service lrwxrwxrwx. 1 root root 56 1月 16 02:52 dracut-pre-udev.service -> ../../dracut/modules.d/98systemd/dracut-pre-udev.service [root@lunar1 ~]#
[root@lunar1 ~]# cat /etc/inittab # inittab is no longer used when using systemd. # # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target # # systemd uses 'targets' instead of runlevels. By default, there are two main targets: # # multi-user.target: analogous to runlevel 3 # graphical.target: analogous to runlevel 5 # # To view current default target, run: # systemctl get-default # # To set a default target, run: # systemctl set-default TARGET.target # [root@lunar1 ~]#
从上面的说明不难看出,systemd不使用inittab作为配置文件,因此在这里添加的服务将不会生效systemd使用’targets’代替了runlevels,缺省有两个targets:
# multi-user.target: analogous to runlevel 3 # graphical.target: analogous to runlevel 5
其中,multi-user.target类似于以前的runlevel 3,也就是字符界面的多用户模式
graphical.target就是以前的runlevel 5,即图形界面模式。
可以使用systemctl get-default来查看缺省的target,也可以使用systemctl set-default来设置缺省的target。
.
查看当前正处在哪个target中:
[root@lunar1 ~]# systemctl list-units --type=target UNIT LOAD ACTIVE SUB DESCRIPTION basic.target loaded active active Basic System cryptsetup.target loaded active active Encrypted Volumes getty.target loaded active active Login Prompts local-fs-pre.target loaded active active Local File Systems (Pre) local-fs.target loaded active active Local File Systems multi-user.target loaded active active Multi-User System network-online.target loaded active active Network is Online network.target loaded active active Network nfs-client.target loaded active active NFS client services paths.target loaded active active Paths remote-fs-pre.target loaded active active Remote File Systems (Pre) remote-fs.target loaded active active Remote File Systems slices.target loaded active active Slices sockets.target loaded active active Sockets swap.target loaded active active Swap sysinit.target loaded active active System Initialization timers.target loaded active active Timers 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. 17 loaded units listed. Pass --all to see loaded but inactive units, too. To show all installed unit files use 'systemctl list-unit-files'. [root@lunar1 ~]#
查看默认target:
[root@lunar1 ~]# systemctl get-default multi-user.target [root@lunar1 ~]#
也就是当前运行在多用户字符集面的模式。查看当前所有的运行级别的定义:
[root@lunar1 ~]# ll /usr/lib/systemd/system/runlevel* lrwxrwxrwx. 1 root root 15 1月 16 02:52 /usr/lib/systemd/system/runlevel0.target -> poweroff.target lrwxrwxrwx. 1 root root 13 1月 16 02:52 /usr/lib/systemd/system/runlevel1.target -> rescue.target lrwxrwxrwx. 1 root root 17 1月 16 02:52 /usr/lib/systemd/system/runlevel2.target -> multi-user.target lrwxrwxrwx. 1 root root 17 1月 16 02:52 /usr/lib/systemd/system/runlevel3.target -> multi-user.target lrwxrwxrwx. 1 root root 17 1月 16 02:52 /usr/lib/systemd/system/runlevel4.target -> multi-user.target lrwxrwxrwx. 1 root root 16 1月 16 02:52 /usr/lib/systemd/system/runlevel5.target -> graphical.target lrwxrwxrwx. 1 root root 13 1月 16 02:52 /usr/lib/systemd/system/runlevel6.target -> reboot.target /usr/lib/systemd/system/runlevel1.target.wants: 总用量 0 lrwxrwxrwx. 1 root root 39 1月 16 02:52 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service /usr/lib/systemd/system/runlevel2.target.wants: 总用量 0 lrwxrwxrwx. 1 root root 39 1月 16 02:52 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service /usr/lib/systemd/system/runlevel3.target.wants: 总用量 0 lrwxrwxrwx. 1 root root 39 1月 16 02:52 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service /usr/lib/systemd/system/runlevel4.target.wants: 总用量 0 lrwxrwxrwx. 1 root root 39 1月 16 02:52 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service /usr/lib/systemd/system/runlevel5.target.wants: 总用量 0 lrwxrwxrwx. 1 root root 39 1月 16 02:52 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service [root@lunar1 ~]#
这里我们看到了熟悉的 运行级别的身影:
runlevel0.target -> poweroff.target ---关机 runlevel1.target -> rescue.target ----营救模式 runlevel2.target -> multi-user.target runlevel3.target -> multi-user.target ----多用户的字符界面 runlevel4.target -> multi-user.target runlevel5.target -> graphical.target ----图形界面 runlevel6.target -> reboot.target ----reboot主机
查看当前的正处在哪个target中:
[root@lunar system]# systemctl list-units --type=target UNIT LOAD ACTIVE SUB DESCRIPTION basic.target loaded active active Basic System cryptsetup.target loaded active active Encrypted Volumes getty.target loaded active active Login Prompts local-fs-pre.target loaded active active Local File Systems (Pre) local-fs.target loaded active active Local File Systems multi-user.target loaded active active Multi-User System network.target loaded active active Network paths.target loaded active active Paths remote-fs-pre.target loaded active active Remote File Systems (Pre) remote-fs.target loaded active active Remote File Systems slices.target loaded active active Slices sockets.target loaded active active Sockets swap.target loaded active active Swap sysinit.target loaded active active System Initialization timers.target loaded active active Timers 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. 15 loaded units listed. Pass --all to see loaded but inactive units, too. To show all installed unit files use 'systemctl list-unit-files'. [root@lunar system]# [root@lunar system]# systemctl list-units --type=target|grep graphical [root@lunar system]#
可以看到当前处在runlevel的运行界别类似于以前的level 3 ,即字符界面启动的我们切换到图形界面启动的运行级别(类似于以前的runlevel 5):
[root@lunar system]# systemctl isolate graphical.target [root@lunar system]# systemctl list-units --type=target UNIT LOAD ACTIVE SUB DESCRIPTION basic.target loaded active active Basic System cryptsetup.target loaded active active Encrypted Volumes getty.target loaded active active Login Prompts graphical.target loaded active active Graphical Interface local-fs-pre.target loaded active active Local File Systems (Pre) local-fs.target loaded active active Local File Systems multi-user.target loaded active active Multi-User System network.target loaded active active Network paths.target loaded active active Paths remote-fs-pre.target loaded active active Remote File Systems (Pre) remote-fs.target loaded active active Remote File Systems slices.target loaded active active Slices sockets.target loaded active active Sockets swap.target loaded active active Swap sysinit.target loaded active active System Initialization timers.target loaded active active Timers 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. 16 loaded units listed. Pass --all to see loaded but inactive units, too. To show all installed unit files use 'systemctl list-unit-files'. [root@lunar system]# [root@lunar system]# systemctl list-units --type=target|grep graphical graphical.target loaded active active Graphical Interface [root@lunar system]#
这时候我们可以看到关键字:graphical.target
查看当前缺省的运行级别:
[root@lunar system]# systemctl get-default multi-user.target [root@lunar system]#
查看unit名为ohasd.serviced的依赖关系:
[root@lunar system]# systemctl list-dependencies ohasd.service ohasd.service ├─system.slice └─basic.target ├─alsa-restore.service ├─alsa-state.service ├─microcode.service ├─rhel-autorelabel-mark.service ├─rhel-autorelabel.service ├─rhel-configure.service ├─rhel-dmesg.service ├─rhel-loadmodules.service ├─paths.target ├─slices.target │ ├─-.slice │ └─system.slice ├─sockets.target │ ├─cups.socket │ ├─dbus.socket │ ├─dm-event.socket │ ├─iscsid.socket │ ├─iscsiuio.socket │ ├─rpcbind.socket │ ├─systemd-initctl.socket │ ├─systemd-journald.socket │ ├─systemd-shutdownd.socket │ ├─systemd-udevd-control.socket │ └─systemd-udevd-kernel.socket ├─sysinit.target │ ├─dev-hugepages.mount │ ├─dev-mqueue.mount │ ├─dmraid-activation.service │ ├─iscsi.service │ ├─kmod-static-nodes.service │ ├─lvm2-lvmetad.socket │ ├─lvm2-monitor.service │ ├─plymouth-read-write.service │ ├─plymouth-start.service │ ├─proc-sys-fs-binfmt_misc.automount │ ├─sys-fs-fuse-connections.mount │ ├─sys-kernel-config.mount │ ├─sys-kernel-debug.mount │ ├─systemd-ask-password-console.path │ ├─systemd-binfmt.service │ ├─systemd-journal-flush.service │ ├─systemd-journald.service │ ├─systemd-modules-load.service │ ├─systemd-random-seed.service │ ├─systemd-sysctl.service │ ├─systemd-tmpfiles-setup-dev.service │ ├─systemd-tmpfiles-setup.service │ ├─systemd-udev-trigger.service │ ├─systemd-udevd.service │ ├─systemd-update-utmp.service │ ├─systemd-vconsole-setup.service │ ├─cryptsetup.target │ ├─local-fs.target │ │ ├─-.mount │ │ ├─boot.mount │ │ ├─rhel-import-state.service │ │ ├─rhel-readonly.service │ │ └─systemd-remount-fs.service │ └─swap.target │ ├─dev-disk-by/x2did-dm/x2dname/x2dol/x2dswap.swap │ ├─dev-disk-by/x2did-dm/x2duuid/x2dLVM/x2dwmmUMFgSImFgksKwxBbGxaioeKcqTyRQ88RtxS76T3TS3XEM247vheZm8SQ77Xgw.swap │ ├─dev-disk-by/x2duuid-1a736d5d/x2de52a/x2d430c/x2dbe02/x2dff81252749db.swap │ ├─dev-dm/x2d1.swap │ ├─dev-mapper-ol/x2dswap.swap │ ├─dev-mapper-ol/x2dswap.swap │ └─dev-ol-swap.swap └─timers.target └─systemd-tmpfiles-clean.timer [root@lunar system]#