800 瀏覽數

CentOS 7 – runlevel

以前 Linux 的 runlevel 是依據 /etc/inittab 檔案來決定開機後要進入哪一種模式.現在都需要透過 #systemctl 指令來做 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, 我們從原先的 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 同 (#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 外,其他的 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/ ,不建議變動,建議還是直接透過指令做修改.

發表迴響