自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

理解Systemd 啟動時在做什么

新聞
本文我將開始探索管理 Linux 啟動序列的文件和工具。我會解釋 systemd 啟動序列、如何更改默認的啟動目標(即 SystemV 術語中的運行級別)、以及在不重啟的情況下如何手動切換到不同的目標。

systemd 啟動過程提供的重要線索可以在問題出現(xiàn)時助你一臂之力。

在本系列的第一篇文章《學著愛上 systemd》,我考察了 systemd 的功能和架構,以及圍繞 systemd 作為古老的 SystemV 初始化程序和啟動腳本的替代品的爭論。在這第二篇文章中,我將開始探索管理 Linux 啟動序列的文件和工具。我會解釋 systemd 啟動序列、如何更改默認的啟動目標(即 SystemV 術語中的運行級別)、以及在不重啟的情況下如何手動切換到不同的目標。

[[419671]]

我還將考察兩個重要的 systemd 工具。第一個 systemctl 命令是和 systemd 交互、向其發(fā)送命令的基本方式。第二個是 journalctl,用于訪問 systemd 日志,后者包含了大量系統(tǒng)歷史數(shù)據(jù),比如內(nèi)核和服務的消息(包括指示性信息和錯誤信息)。

務必使用一個非生產(chǎn)系統(tǒng)進行本文和后續(xù)文章中的測試和實驗。你的測試系統(tǒng)需要安裝一個 GUI 桌面(比如 Xfce、LXDE、Gnome、KDE 或其他)。

上一篇文章中我寫道計劃在這篇文章創(chuàng)建一個 systemd 單元并添加到啟動序列。由于這篇文章比我預期中要長,這些內(nèi)容將留到本系列的下一篇文章。

使用 systemd 探索 Linux 的啟動

在觀察啟動序列之前,你需要做幾件事情得使引導和啟動序列開放可見。正常情況下,大多數(shù)發(fā)行版使用一個開機動畫或者啟動畫面隱藏 Linux 啟動和關機過程中的顯示細節(jié),在基于 Red Hat 的發(fā)行版中稱作 Plymouth 引導畫面。這些隱藏的消息能夠向尋找信息以排除程序故障、或者只是學習啟動序列的系統(tǒng)管理員提供大量有關系統(tǒng)啟動和關閉的信息。你可以通過 GRUB(大統(tǒng)一引導加載器Grand Unified Boot Loader)配置改變這個設置。

主要的 GRUB 配置文件是 /boot/grub2/grub.cfg ,但是這個文件在更新內(nèi)核版本時會被覆蓋,你不會想修改它的。相反,應該修改用于改變 grub.cfg 默認設置的 /etc/default/grub 文件。

首先看一下當前未修改的 /etc/default/grub 文件的版本:

  1. [root@testvm1 ~]# cd /etc/default ; cat grub 
  2. GRUB_TIMEOUT=5 
  3. GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" 
  4. GRUB_DEFAULT=saved 
  5. GRUB_DISABLE_SUBMENU=true 
  6. GRUB_TERMINAL_OUTPUT="console" 
  7. GRUB_CMDLINE_LINUX="resume=/dev/mapper/fedora_testvm1-swap rd.lvm. 
  8. lv=fedora_testvm1/root rd.lvm.lv=fedora_testvm1/swap rd.lvm.lv=fedora_ 
  9. testvm1/usr rhgb quiet" 
  10. GRUB_DISABLE_RECOVERY="true" 
  11. [root@testvm1 default]# 

GRUB 文檔 的第 6 章列出了 /etc/default/grub 文件的所有可用項,我只關注下面的部分:

  • 我將 GRUB 菜單倒計時的秒數(shù) GRUB_TIMEOUT,從 5 改成 10,以便在倒計時達到 0 之前有更多的時間響應 GRUB 菜單。
  • GRUB_CMDLINE_LINUX 列出了引導階段傳遞給內(nèi)核的命令行參數(shù),我刪除了其中的最后兩個參數(shù)。其中的一個參數(shù) rhgb 代表 “紅帽圖形化引導Red Hat Graphical Boot”,在內(nèi)核初始化階段顯示一個小小的 Fedora 圖標動畫,而不是顯示引導階段的信息。另一個參數(shù) quiet,屏蔽顯示記錄了啟動進度和發(fā)生錯誤的消息。系統(tǒng)管理員需要這些信息,因此我刪除了 rhgb 和 quiet。如果引導階段發(fā)生了錯誤,屏幕上顯示的信息可以指向故障的原因。

更改之后,你的 GRUB 文件將會像下面一樣:

  1. [root@testvm1 default]# cat grub 
  2. GRUB_TIMEOUT=10 
  3. GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" 
  4. GRUB_DEFAULT=saved 
  5. GRUB_DISABLE_SUBMENU=true 
  6. GRUB_TERMINAL_OUTPUT="console" 
  7. GRUB_CMDLINE_LINUX="resume=/dev/mapper/fedora_testvm1-swap rd.lvm. 
  8. lv=fedora_testvm1/root rd.lvm.lv=fedora_testvm1/swap rd.lvm.lv=fedora_ 
  9. testvm1/usr" 
  10. GRUB_DISABLE_RECOVERY="false" 
  11. [root@testvm1 default]# 

grub2-mkconfig 程序使用 /etc/default/grub 文件的內(nèi)容生成 grub.cfg 配置文件,從而改變一些默認的 GRUB 設置。grub2-mkconfig 輸出到 STDOUT,你可以使用程序的 -o 參數(shù)指明數(shù)據(jù)流輸出的文件,不過使用重定向也同樣簡單。執(zhí)行下面的命令更新 /boot/grub2/grub.cfg 配置文件:

  1. [root@testvm1 grub2]# grub2-mkconfig > /boot/grub2/grub.cfg 
  2. Generating grub configuration file ... 
  3. Found linux image: /boot/vmlinuz-4.18.9-200.fc28.x86_64 
  4. Found initrd image: /boot/initramfs-4.18.9-200.fc28.x86_64.img 
  5. Found linux image: /boot/vmlinuz-4.17.14-202.fc28.x86_64 
  6. Found initrd image: /boot/initramfs-4.17.14-202.fc28.x86_64.img 
  7. Found linux image: /boot/vmlinuz-4.16.3-301.fc28.x86_64 
  8. Found initrd image: /boot/initramfs-4.16.3-301.fc28.x86_64.img 
  9. Found linux image: /boot/vmlinuz-0-rescue-7f12524278bd40e9b10a085bc82dc504 
  10. Found initrd image: /boot/initramfs-0-rescue-7f12524278bd40e9b10a085bc82dc504.img 
  11. done 
  12. [root@testvm1 grub2]# 

重新啟動你的測試系統(tǒng)查看本來會隱藏在 Plymouth 開機動畫之下的啟動信息。但是如果你沒有關閉開機動畫,又需要查看啟動信息的話又該如何操作?或者你關閉了開機動畫,而消息流過的速度太快,無法閱讀怎么辦?(實際情況如此。)

有兩個解決方案,都涉及到日志文件和 systemd 日志 —— 兩個都是你的好伙伴。你可以使用 less 命令查看 /var/log/messages 文件的內(nèi)容。這個文件包含引導和啟動信息,以及操作系統(tǒng)執(zhí)行正常操作時生成的信息。你也可以使用不加任何參數(shù)的 journalctl 命令查看 systemd 日志,包含基本相同的信息:

  1. [root@testvm1 grub2]# journalctl 
  2. -- Logs begin at Sat 2020-01-11 21:48:08 EST, end at Fri 2020-04-03 08:54:30 EDT. -- 
  3. Jan 11 21:48:08 f31vm.both.org kernel: Linux version 5.3.7-301.fc31.x86_64 (mockbuild@bkernel03.phx2.fedoraproject.org) (gcc version 9.2.1 20190827 (Red Hat 9.2.1-1) (GCC)) #1 SMP Mon Oct > 
  4. Jan 11 21:48:08 f31vm.both.org kernel: Command line: BOOT_IMAGE=(hd0,msdos1)/vmlinuz-5.3.7-301.fc31.x86_64 root=/dev/mapper/VG01-root ro resume=/dev/mapper/VG01-swap rd.lvm.lv=VG01/root rd> 
  5. Jan 11 21:48:08 f31vm.both.org kernel: x86/fpu: Supporting XSAVE feature 0x001: 'x87 floating point registers' 
  6. Jan 11 21:48:08 f31vm.both.org kernel: x86/fpu: Supporting XSAVE feature 0x002: 'SSE registers' 
  7. Jan 11 21:48:08 f31vm.both.org kernel: x86/fpu: Supporting XSAVE feature 0x004: 'AVX registers' 
  8. Jan 11 21:48:08 f31vm.both.org kernel: x86/fpu: xstate_offset[2]:  576, xstate_sizes[2]:  256 
  9. Jan 11 21:48:08 f31vm.both.org kernel: x86/fpu: Enabled xstate features 0x7, context size is 832 bytes, using 'standard' format. 
  10. Jan 11 21:48:08 f31vm.both.org kernel: BIOS-provided physical RAM map: 
  11. Jan 11 21:48:08 f31vm.both.org kernel: BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable 
  12. Jan 11 21:48:08 f31vm.both.org kernel: BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved 
  13. Jan 11 21:48:08 f31vm.both.org kernel: BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved 
  14. Jan 11 21:48:08 f31vm.both.org kernel: BIOS-e820: [mem 0x0000000000100000-0x00000000dffeffff] usable 
  15. Jan 11 21:48:08 f31vm.both.org kernel: BIOS-e820: [mem 0x00000000dfff0000-0x00000000dfffffff] ACPI data 
  16. Jan 11 21:48:08 f31vm.both.org kernel: BIOS-e820: [mem 0x00000000fec00000-0x00000000fec00fff] reserved 
  17. Jan 11 21:48:08 f31vm.both.org kernel: BIOS-e820: [mem 0x00000000fee00000-0x00000000fee00fff] reserved 
  18. Jan 11 21:48:08 f31vm.both.org kernel: BIOS-e820: [mem 0x00000000fffc0000-0x00000000ffffffff] reserved 
  19. Jan 11 21:48:08 f31vm.both.org kernel: BIOS-e820: [mem 0x0000000100000000-0x000000041fffffff] usable 
  20. Jan 11 21:48:08 f31vm.both.org kernel: NX (Execute Disable) protection: active 
  21. Jan 11 21:48:08 f31vm.both.org kernel: SMBIOS 2.5 present. 
  22. Jan 11 21:48:08 f31vm.both.org kernel: DMI: innotek GmbH VirtualBox/VirtualBox, BIOS VirtualBox 12/01/2006 
  23. Jan 11 21:48:08 f31vm.both.org kernel: Hypervisor detected: KVM 
  24. Jan 11 21:48:08 f31vm.both.org kernel: kvm-clock: Using msrs 4b564d01 and 4b564d00 
  25. Jan 11 21:48:08 f31vm.both.org kernel: kvm-clock: cpu 0, msr 30ae01001, primary cpu clock 
  26. Jan 11 21:48:08 f31vm.both.org kernel: kvm-clock: using sched offset of 8250734066 cycles 
  27. Jan 11 21:48:08 f31vm.both.org kernel: clocksource: kvm-clock: mask: 0xffffffffffffffff max_cycles: 0x1cd42e4dffb, max_idle_ns: 881590591483 ns 
  28. Jan 11 21:48:08 f31vm.both.org kernel: tsc: Detected 2807.992 MHz processor 
  29. Jan 11 21:48:08 f31vm.both.org kernel: e820: update [mem 0x00000000-0x00000fff] usable ==> reserved 
  30. Jan 11 21:48:08 f31vm.both.org kernel: e820: remove [mem 0x000a0000-0x000fffff] usable 
  31. <snip> 

由于數(shù)據(jù)流可能長達幾十萬甚至幾百萬行,我在這里截斷了它。(我的主要工作站上列出的日志長度是 1,188,482 行。)請確保是在你的測試系統(tǒng)嘗試的這個命令。如果系統(tǒng)已經(jīng)運行了一段時間 —— 即使重啟過很多次 —— 還是會顯示大量的數(shù)據(jù)。查看這些日志數(shù)據(jù),因為它包含了很多信息,在進行問題判斷時可能非常有用。了解這個數(shù)據(jù)文件在正常的引導和啟動過程中的模樣,可以幫助你在問題出現(xiàn)時定位問題。

我將在本系列之后的文章討論 systemd 日志、journalctl 命令、以及如何整理輸出的日志數(shù)據(jù)來尋找更詳細的信息。

內(nèi)核被 GRUB 加載到內(nèi)存后,必須先將自己從壓縮后的文件中解壓出來,才能執(zhí)行任何有意義的操作。解壓自己后,內(nèi)核開始運行,加載 systemd 并轉交控制權。

引導boot階段到此結束,此時 Linux 內(nèi)核和 systemd 正在運行,但是無法為用戶執(zhí)行任何生產(chǎn)性任務,因為其他的程序都沒有執(zhí)行,沒有命令行解釋器提供命令行,沒有后臺進程管理網(wǎng)絡和其他的通信鏈接,也沒有任何東西能夠控制計算機執(zhí)行生產(chǎn)功能。

現(xiàn)在 systemd 可以加載所需的功能性單元以便將系統(tǒng)啟動到選擇的目標運行狀態(tài)。

目標

一個 systemd 目標target代表一個 Linux 系統(tǒng)當前的或期望的運行狀態(tài)。與 SystemV 啟動腳本十分類似,目標定義了系統(tǒng)運行必須存在的服務,以及處于目標狀態(tài)下必須激活的服務。圖表 1 展示了使用 systemd 的 Linux 系統(tǒng)可能的運行狀態(tài)目標。就像在本系列的第一篇文章以及 systemd 啟動的手冊頁(man bootup)所看到的一樣,有一些開啟不同必要服務的其他中間目標,包括 swap.target、timers.target、local-fs.target 等。一些目標(像 basic.target)作為檢查點使用,在移動到下一個更高級的目標之前保證所有需要的服務已經(jīng)啟動并運行。

除非開機時在 GRUB 菜單進行更改,systemd 總是啟動 default.target。default.target 文件是指向真實的目標文件的符號鏈接。對于桌面工作站,default.target 通常是 graphical.target,等同于 SystemV 的運行等級 5。對于服務器,默認目標多半是 multi-user.target,就像 SystemV 的運行等級 3。emergency.target 文件類似單用戶模式。目標和服務service都是一種 systemd 單元。

下面的圖表,包含在本系列的上一篇文章中,比較了 systemd 目標和古老的 SystemV 啟動運行等級。為了向后兼容,systemd 提供了 systemd 目標別名,允許腳本和系統(tǒng)管理員使用像 init 3 一樣的 SystemV 命令改變運行等級。當然,SystemV 命令被轉發(fā)給 systemd 進行解釋和執(zhí)行。

每個目標在配置文件中都描述了一組依賴關系。systemd 啟動需要的依賴,即 Linux 主機運行在特定功能級別所需的服務。加載目標配置文件中列出的所有依賴并運行后,系統(tǒng)就運行在那個目標等級。如果愿意,你可以在本系列的第一篇文章《學著愛上 systemd》中回顧 systemd 的啟動序列和運行時目標。

探索當前的目標

許多 Linux 發(fā)行版默認安裝一個 GUI 桌面界面,以便安裝的系統(tǒng)可以像工作站一樣使用。我總是從 Fedora Live USB 引導驅動器安裝 Xfce 或 LXDE 桌面。即使是安裝一個服務器或者其他基礎類型的主機(比如用于路由器和防火墻的主機),我也使用 GUI 桌面的安裝方式。

我可以安裝一個沒有桌面的服務器(數(shù)據(jù)中心的典型做法),但是這樣不滿足我的需求。原因不是我需要 GUI 桌面本身,而是 LXDE 安裝包含了許多其他默認的服務器安裝沒有提供的工具,這意味著初始安裝之后我需要做的工作更少。

但是,僅僅因為有 GUI 桌面并不意味著我要使用它。我有一個 16 端口的 KVM,可以用于訪問我的大部分 Linux 系統(tǒng)的 KVM 接口,但我和它們交互的大部分交互是通過從我的主要工作站建立的遠程 SSH 連接。這種方式更安全,而且和 graphical.target 相比,運行 multi-user.target 使用更少的系統(tǒng)資源。

首先,檢查默認目標,確認是 graphical.target:

  1. [root@testvm1 ~]# systemctl get-default 
  2. graphical.target 
  3. [root@testvm1 ~]# 

然后確認當前正在運行的目標,應該和默認目標相同。你仍可以使用老方法,輸出古老的 SystemV 運行等級。注意,前一個運行等級在左邊,這里是 N(意思是 None),表示主機啟動后沒有修改過運行等級。數(shù)字 5 是當前的目標,正如古老的 SystemV 術語中的定義:

  1. [root@testvm1 ~]# runlevel 
  2. N 5 
  3. [root@testvm1 ~]# 

注意,runlevel 的手冊頁指出運行等級已經(jīng)被淘汰,并提供了一個轉換表。

你也可以使用 systemd 方式,命令的輸出有很多行,但確實用 systemd 術語提供了答案:

  1. [root@testvm1 ~]# systemctl list-units --type target 
  2. UNIT                   LOAD   ACTIVE SUB    DESCRIPTION                 
  3. basic.target           loaded active active Basic System               
  4. cryptsetup.target      loaded active active Local Encrypted Volumes     
  5. getty.target           loaded active active Login Prompts               
  6. graphical.target       loaded active active Graphical Interface         
  7. local-fs-pre.target    loaded active active Local File Systems (Pre)   
  8. local-fs.target        loaded active active Local File Systems         
  9. multi-user.target      loaded active active Multi-User System           
  10. network-online.target  loaded active active Network is Online           
  11. network.target         loaded active active Network                     
  12. nfs-client.target      loaded active active NFS client services         
  13. nss-user-lookup.target loaded active active User and Group Name Lookups 
  14. paths.target           loaded active active Paths                       
  15. remote-fs-pre.target   loaded active active Remote File Systems (Pre)   
  16. remote-fs.target       loaded active active Remote File Systems         
  17. rpc_pipefs.target      loaded active active rpc_pipefs.target           
  18. slices.target          loaded active active Slices                     
  19. sockets.target         loaded active active Sockets                     
  20. sshd-keygen.target     loaded active active sshd-keygen.target         
  21. swap.target            loaded active active Swap                       
  22. sysinit.target         loaded active active System Initialization       
  23. timers.target          loaded active active Timers                     
  24. LOAD   = Reflects whether the unit definition was properly loaded. 
  25. ACTIVE = The high-level unit activation state, i.e. generalization of SUB. 
  26. SUB    = The low-level unit activation state, values depend on unit type. 
  27. 21 loaded units listed. Pass --all to see loaded but inactive units, too. 
  28. To show all installed unit files use 'systemctl list-unit-files'

上面列出了當前加載的和激活的目標,你也可以看到 graphical.target 和 multi-user.target。multi-user.target 需要在 graphical.target 之前加載。這個例子中,graphical.target 是激活的。

切換到不同的目標

切換到 multi-user.target 很簡單:

  1. [root@testvm1 ~]# systemctl isolate multi-user.target 

顯示器現(xiàn)在應該從 GUI 桌面或登錄界面切換到了一個虛擬控制臺。登錄并列出當前激活的 systemd 單元,確認 graphical.target 不再運行:

  1. [root@testvm1 ~]# systemctl list-units --type target 

務必使用 runlevel 確認命令輸出了之前的和當前的“運行等級”:

  1. [root@testvm1 ~]# runlevel 
  2. 5 3 

更改默認目標

現(xiàn)在,將默認目標改為 multi-user.target,以便系統(tǒng)總是啟動進入 multi-user.target,從而使用控制臺命令行接口而不是 GUI 桌面接口。使用你的測試主機的根用戶,切換到保存 systemd 配置的目錄,執(zhí)行一次快速列出操作:

  1. [root@testvm1 ~]# cd /etc/systemd/system/ ; ll 
  2. drwxr-xr-x. 2 root root 4096 Apr 25  2018  basic.target.wants 
  3. &lt;snip&gt; 
  4. lrwxrwxrwx. 1 root root   36 Aug 13 16:23  default.target -> /lib/systemd/system/graphical.target 
  5. lrwxrwxrwx. 1 root root   39 Apr 25  2018  display-manager.service -> /usr/lib/systemd/system/lightdm.service 
  6. drwxr-xr-x. 2 root root 4096 Apr 25  2018  getty.target.wants 
  7. drwxr-xr-x. 2 root root 4096 Aug 18 10:16  graphical.target.wants 
  8. drwxr-xr-x. 2 root root 4096 Apr 25  2018  local-fs.target.wants 
  9. drwxr-xr-x. 2 root root 4096 Oct 30 16:54  multi-user.target.wants 
  10. &lt;snip&gt; 
  11. [root@testvm1 system]# 

為了強調(diào)一些有助于解釋 systemd 如何管理啟動過程的重要事項,我縮短了這個列表。你應該可以在虛擬機看到完整的目錄和鏈接列表。

default.target 項是指向目錄 /lib/systemd/system/graphical.target 的符號鏈接(軟鏈接),列出那個目錄查看目錄中的其他內(nèi)容:

  1. [root@testvm1 system]# ll /lib/systemd/system/ | less 

你應該在這個列表中看到文件、目錄、以及更多鏈接,但是專門尋找一下 multi-user.target 和 graphical.target?,F(xiàn)在列出 default.target(指向 /lib/systemd/system/graphical.target 的鏈接)的內(nèi)容:

  1. [root@testvm1 system]# cat default.target 
  2. #  SPDX-License-Identifier: LGPL-2.1+ 
  3. #  This file is part of systemd. 
  4. #  systemd is free software; you can redistribute it and/or modify it 
  5. #  under the terms of the GNU Lesser General Public License as published by 
  6. #  the Free Software Foundation; either version 2.1 of the License, or 
  7. #  (at your optionany later version. 
  8. [Unit] 
  9. Description=Graphical Interface 
  10. Documentation=man:systemd.special(7) 
  11. Requires=multi-user.target 
  12. Wants=display-manager.service 
  13. Conflicts=rescue.service rescue.target 
  14. After=multi-user.target rescue.service rescue.target display-manager.service 
  15. AllowIsolate=yes 
  16. [root@testvm1 system]# 

graphical.target 文件的這個鏈接描述了圖形用戶接口需要的所有必備條件。我會在本系列的下一篇文章至少探討其中的一些選項。

為了使主機啟動到多用戶模式,你需要刪除已有的鏈接,創(chuàng)建一個新鏈接指向正確目標。如果你的 PWD 不是 /etc/systemd/system,切換過去:

  1. [root@testvm1 system]# rm -f default.target 
  2. [root@testvm1 system]# ln -s /lib/systemd/system/multi-user.target default.target 

列出 default.target 鏈接,確認其指向了正確的文件:

  1. [root@testvm1 system]# ll default.target 
  2. lrwxrwxrwx 1 root root 37 Nov 28 16:08 default.target -&gt; /lib/systemd/system/multi-user.target 
  3. [root@testvm1 system]# 

如果你的鏈接看起來不一樣,刪除并重試。列出 default.target 鏈接的內(nèi)容:

  1. [root@testvm1 system]# cat default.target 
  2. #  SPDX-License-Identifier: LGPL-2.1+ 
  3. #  This file is part of systemd. 
  4. #  systemd is free software; you can redistribute it and/or modify it 
  5. #  under the terms of the GNU Lesser General Public License as published by 
  6. #  the Free Software Foundation; either version 2.1 of the License, or 
  7. #  (at your optionany later version. 
  8. [Unit] 
  9. Description=Multi-User System 
  10. Documentation=man:systemd.special(7) 
  11. Requires=basic.target 
  12. Conflicts=rescue.service rescue.target 
  13. After=basic.target rescue.service rescue.target 
  14. AllowIsolate=yes 
  15. [root@testvm1 system]# 

default.target(這里其實是指向 multi-user.target 的鏈接)其中的 [Unit] 部分現(xiàn)在有不同的必需條件。這個目標不需要有圖形顯示管理器。

重啟,你的虛擬機應該啟動到虛擬控制臺 1 的控制臺登錄,虛擬控制臺 1 在顯示器標識為 tty1。現(xiàn)在你已經(jīng)知道如何修改默認的目標,使用所需的命令將默認目標改回 graphical.target。

首先檢查當前的默認目標:

  1. [root@testvm1 ~]# systemctl get-default 
  2. multi-user.target 
  3. [root@testvm1 ~]# systemctl set-default graphical.target 
  4. Removed /etc/systemd/system/default.target. 
  5. Created symlink /etc/systemd/system/default.target → /usr/lib/systemd/system/graphical.target. 
  6. [root@testvm1 ~]# 

輸入下面的命令直接切換到 graphical.target 和顯示管理器的登錄界面,不需要重啟:

  1. [root@testvm1 system]# systemctl isolate default.target 

我不清楚為何 systemd 的開發(fā)者選擇了術語 isolate 作為這個子命令。我的研究表明指的可能是運行指明的目標,但是“隔離”并終結其他所有啟動該目標不需要的目標。然而,命令執(zhí)行的效果是從一個運行的目標切換到另一個——在這個例子中,從多用戶目標切換到圖形目標。上面的命令等同于 SystemV 啟動腳本和 init 程序中古老的 init 5 命令。

登錄 GUI 桌面,確認能正常工作。

總結

本文探索了 Linux systemd 啟動序列,開始探討兩個重要的 systemd 工具 systemctl 和 journalctl,還說明了如何從一個目標切換到另一個目標,以及如何修改默認目標。

本系列的下一篇文章中將會創(chuàng)建一個新的 systemd 單元,并配置為啟動階段運行。下一篇文章還會查看一些配置選項,可以幫助確定某個特定的單元在序列中啟動的位置,比如在網(wǎng)絡啟動運行后。

資源

關于 systemd 網(wǎng)絡上有大量的信息,但大部分都簡短生硬、愚鈍、甚至令人誤解。除了本文提到的資源,下面的網(wǎng)頁提供了關于 systemd 啟動更詳細可靠的信息。

  • Fedora 項目有一個優(yōu)質實用的 systemd 指南,幾乎有你使用 systemd 配置、管理、維護一個 Fedora 計算機需要知道的一切。
  • Fedora 項目還有一個好用的 速查表,交叉引用了古老的 SystemV 命令和對應的 systemd 命令。
  • 要獲取 systemd 的詳細技術信息和創(chuàng)立的原因,查看 Freedesktop.org 的 systemd 描述。
  • Linux.com 上“systemd 的更多樂趣”提供了更高級的 systemd 信息和提示。

 

責任編輯:未麗燕 來源: Linux中國
點贊
收藏

51CTO技術棧公眾號