以前 Linux 的 runlevel 是依據 /etc/inittab 檔案來決定開機後要進入哪一種模式.現在都需要透過 #systemctl ( systemd ) 指令來做 runlevel 的設定.
測試環境為 CentOS 7 X64 ,一樣適用於 RedHat 7
目前定義的 runlevel 為
Runlevel Target Units Description 0 runlevel0.target, poweroff.target Shut down and power off the system. 1 runlevel1.target, rescue.targe Set up a rescue shell. 2 runlevel2.target, multi-user.target Set up a non-graphical multi-user system. 3 runlevel3.target, multi-user.target Set up a non-graphical multi-user system. 4 runlevel4.target, multi-user.target Set up a non-graphical multi-user system. 5 runlevel5.target, graphical.target Set up a graphical multi-user system. 6 runlevel6.target, reboot.target Shut down and reboot the system.
透過 systemctl 的參數 get-default 可以知道目前設定開機的 runlevel 為何
[root@localhost ~]# systemctl get-default multi-user.target [root@localhost ~]# runlevel N 3
不過光是 multi-user.target 就有 runlevel 2,3,4 .
runlevel 設定有兩種方式,一為 設定開機時候的 runlevel ,二則是立即改變 runlevel 同 (#init #)
設定開機的 runlevel
先來看第一種設定開機時候的 runlevel, 我們從原先的 multi-user.target 改成 graphical.target , 下次重開機就會生效了.
我們可以先用指令確認目前的 runlevel 為何,可以看到目前 multi-user.target 是 loaded 且 active 的.
[root@localhost ~]# 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'.
檢查一下開機時所使用的 runlevel .
[root@localhost ~]# systemctl get-default multi-user.target
並設定成為 graphical.target
[root@localhost ~]# systemctl set-default graphical.target Removed symlink /etc/systemd/system/default.target. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.
再檢查一下.
[root@localhost ~]# systemctl get-default graphical.target
需要重新開機才會生效.
[root@localhost ~]# reboot
這時候會發現變成為 graphical.target 是 loaded 而且是 active 的. multi-user.target 也是 loaded 且 active 的,應該是 Linux 同時支援多種 terminal console (可以透過 Alt+F# 來切換).
[root@localhost ~]# 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-online.target loaded active active Network is Online network.target loaded active active Network nfs-client.target loaded active active NFS client services nss-user-lookup.target loaded active active User and Group Name Lookups 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. 19 loaded units listed. Pass --all to see loaded but inactive units, too. To show all installed unit files use 'systemctl list-unit-files'.
立即改變 runlevel
立即改變 runlevel 同 (#init #),我剛剛已經設定開機後為 graphical.target 可以直接透過 systemctl isolate name.target (前面所定義的 runlevel) 就可以立即改變 runlevel
[root@localhost ~]# systemctl get-default graphical.target
[root@localhost ~]# systemctl isolate multi-user.target
[root@localhost ~]# 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'.
其他 runlevel
除了常見定義的 runlevel 外,其他的 ACPI 模式也可以透過 systemctl 做改變
- suspend
Suspend the system. This will trigger activation of the special suspend.target target. - hibernate
Hibernate the system. This will trigger activation of the special hibernate.target target. - hybrid-sleep
Hibernate and suspend the system. This will trigger activation of the special hybrid-sleep.target target.
救模模式
- rescue
Enter rescue mode. This is mostly equivalent to isolate rescue.target, but also prints a wall message to all users. - emergency
Enter emergency mode. This is mostly equivalent to isolate emergency.target, but also prints a wall message to all users.
相關檔案位於 /etc/systemd/system/ ,不建議變動,建議還是直接透過指令做修改.
Gnome Desktop Manager
GDM(GNOME Display Manager) 這是 RedHat / CentOS 預設的桌面管理程式 XDM(X display manager) 使用 XDM 協定為 X Display Manager Control Protocol (XDMCP) .
這協定的 Display manager 提供給使用者登入系統的畫面, 使用者輸入正確的使用者名稱以及密碼之後建立 X server 的 session. 如果 display manager 同時存在使用者電腦上時, 他會預先啟動 x server 的 session .如果你想要讓你的 Linux 桌面能夠在遠端登入以及被管控只需設定一下即可使用.
在 systemd 系統下 gdm 的服務可透過以下的方式重新啟動.
[root@localhost ~]# systemctl restart gdm.service
RHEL / CentOS 5,6 runlevel
RHEL / CentOS 5,6 要立即改變 runlevel,可以透過指令 #init 3 (Text mode 文字模式) 以及 #init 5 (Graphical Mode 圖形模式),開機後的模式則需要修改檔案 /etc/inittab .
[root@benjr ~]# cat /etc/inittab # inittab is only used by upstart for the default runlevel. # # ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # System initialization is started by /etc/init/rcS.conf # # Individual runlevels are started by /etc/init/rc.conf # # Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf # # Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf, # with configuration in /etc/sysconfig/init. # # For information on how to write upstart event handlers, or how # upstart works, see init(5), init(8), and initctl(8). # # Default runlevel. The runlevels used are: # 0 – halt (Do NOT set initdefault to this) # 1 – Single user mode # 2 – Multiuser, without NFS (The same as 3, if you do not have networking) # 3 – Full multiuser mode # 4 – unused # 5 – X11 # 6 – reboot (Do NOT set initdefault to this) # id:5:initdefault:
下面是各個 runlevel 的定義,預設為 id:5:initdefault:
- runlevel 0 – halt (Do NOT set initdefault to this)
- runlevel 1 – Single user mode
- runlevel 2 – Multiuser, without NFS (The same as 3, if you do not have networking)
- runlevel 3 – Full multiuser mode
- runlevel 4 – unused
- runlevel 5 – X11
- runlevel 6 – reboot (Do NOT set initdefault to this)