新聞中心
- 9.1. 系統(tǒng)啟動
-
- 9.1.1. Systemd 啟動系統(tǒng)
- 9.1.2. System V 初始系統(tǒng)
- 9.2. 遠(yuǎn)程登錄
-
- 9.2.1. 安全遠(yuǎn)程登錄:SSH
- 9.2.2. 使用遠(yuǎn)程圖形桌面
- 9.3. 管理權(quán)限
- 9.4. 管理員界面
-
-
9.4.1. 管理網(wǎng)頁接口:
webmin -
9.4.2. 用于管理配置的軟件包:
debconf
-
9.4.1. 管理網(wǎng)頁接口:
-
9.5.
syslog系統(tǒng)事件 -
- 9.5.1. 原則與機制
- 9.5.2. 配置文件
-
9.6. The
inetd超級服務(wù)器 -
9.7. 使用
cron和atd運行計劃任務(wù) -
-
9.7.1.
crontab文件的格式 -
9.7.2. 使用
at命令
-
9.7.1.
-
9.8. 調(diào)度異步任務(wù):
anacron - 9.9. 配額
- 9.10. 備份
-
-
9.10.1. 使用
rsync備份 - 9.10.2. 不使用備份恢復(fù)系統(tǒng)
-
9.10.1. 使用
- 9.11. 熱插拔: 熱插拔
-
- 9.11.1. 介紹
- 9.11.2. 命名問題
- 9.11.3. udev 如何工作
- 9.11.4. 一個具體例子
- 9.12. 電源管理:高端配置與電源接口 (ACPI)
本章涵蓋一些Unix系統(tǒng)共通的服務(wù)。管理員應(yīng)當(dāng)熟悉他們。

9.1. 系統(tǒng)啟動
當(dāng)啟動計算機時,控制臺上滾動的大量信息顯示許多初始化和配置工作自動正在執(zhí)行。有時候你可能稍稍的改變這一階段的操作,就要求你需要很好的理解他們。這正是本章節(jié)的目的所在。 首先,BIOS 控制電腦,探測磁盤,加載
Master Boot Record,并執(zhí)行啟動加載器。啟動程序接手后,找到磁盤上的內(nèi)核,加載并執(zhí)行。然后,內(nèi)核被初始化,并開始尋找和掛載包含根文件系統(tǒng)的分區(qū),最后執(zhí)行第一個程序-
init。根分區(qū)和啟動程序
init常常駐留在僅存在于 RAM 中的虛擬文件系統(tǒng)(正如它的名字,“initramfs”,一般稱之為“initrd”初始內(nèi)存磁盤)。啟動加載器將文件系統(tǒng)加載到內(nèi)存中,文件通常位于硬盤或者源于網(wǎng)絡(luò)。它包含了內(nèi)核需要的最少裸信息,以便用來加載“真正”的根文件系統(tǒng):可能是硬盤上的驅(qū)動模塊,或者其他系統(tǒng)啟動必須的設(shè)備,或者更常見的是初始化腳本和模塊以組建 RAID 陣列,打開加密分區(qū),激活 LVM ,等等。一旦根分區(qū)掛載,initramfs 就會把控制權(quán)交到真正的啟動程序,機器則回到標(biāo)準(zhǔn)的啟動過程。
9.1.1. Systemd 啟動系統(tǒng)
“真正的啟動器”當(dāng)前是由
systemd 提供的,本章節(jié)講述該啟動系統(tǒng)。
文化 在 systemd 之前
systemd 是相當(dāng)新的 “啟動系統(tǒng)”,雖然在
Wheezy 里已經(jīng)可以使用到某個程度,直到 Debian
Jessie 才納入默認(rèn)值。稍最的版本,缺省是 “System V init” (在
sysv-rc 軟件包內(nèi)),算是傳統(tǒng)的系統(tǒng)。以下描述的是 System V init。
選擇 其它啟動系統(tǒng)
This book describes the boot system used by default in Debian
Buster (as implemented by the
systemd package), as well as the previous default,
sysvinit, which is derived and inherited from
System V Unix systems; there are others.
file-rc 是一個過程很簡單的啟動系統(tǒng)。它保留運行等級的原則,但是用配置文件取代了目錄和符號鏈接,來告訴
init哪些進(jìn)程必須啟動及其順序。 The
upstart system is still not perfectly tested on Debian. It is event based: init scripts are no longer executed in a sequential order but in response to events such as the completion of another script upon which they are dependent. This system, started by Ubuntu, was present in Debian
Jessie, but was not the default; it came, in fact, as a replacement for
sysvinit, and one of the tasks launched by
upstart was to launch the scripts written for traditional systems, especially those from the
sysv-rc package. 也有其他的系統(tǒng)和操作模式,例如:
runit 或者
minit,但是他們相對專門且沒有那么普遍。
圖 9.1. 運行 sytemd 的 Linux 計算機的啟動順序
特例 從網(wǎng)絡(luò)啟動
在某些配置中,BIOS 可以配置為不執(zhí)行 MBR,而是在網(wǎng)絡(luò)上尋找類似的東西,這樣就可以制作不需要硬盤的電腦,在每次啟動后可以完全重裝。不是所有的硬件都支持該選項,它需要 BIOS 和網(wǎng)卡很好的配合。 從網(wǎng)絡(luò)啟動可以用于執(zhí)行
debian-installer or FAI (參考 第 4.1 節(jié) “安裝方式”)。
回到基礎(chǔ)進(jìn)程,一個程序?qū)嵗?/strong>
一個進(jìn)程代表一個在內(nèi)存中運行的程序。它包含了需要正確執(zhí)行軟件的所有必要信息(代碼本身,內(nèi)存數(shù)據(jù),打開的文件清單,建立的網(wǎng)絡(luò)連接,等等)。一個程序可能初始化為幾個進(jìn)程,而沒必要使用不同的用戶 ID 。
安全 使用 shell 作為 init 獲取超級用戶權(quán)限
通常,第一個啟動的進(jìn)程是
init 程序(默認(rèn)是到
/lib/systemd/systemd 的符號鏈接)。然而,也可以通過傳遞
init 選項告訴內(nèi)核使用其他的程序。 任何可以接近電腦的人都能按下
復(fù)位 按,并重新啟動。然后,在啟動提示下,傳遞
init=/bin/sh選項給內(nèi)核,無需知道密碼而獲取超級用戶權(quán)限。 為了防止此類事件,可以給加載器設(shè)定密碼。你也許會考慮保護(hù) BIOS (密碼保護(hù)總是可行的),這樣可以防止侵入者使用包含自己的 Linux 系統(tǒng)的移動介質(zhì)啟動電腦,使用該系統(tǒng)他們可以讀取硬盤上的數(shù)據(jù)。 最后,應(yīng)該知道大多數(shù) BIOS 有一個通用的密碼。最初用于讓那些忘記自己密碼的人解決問題,這些密碼現(xiàn)在是公開的并且可以在網(wǎng)上找到(通過搜索引擎搜索“通用 BIOS 密碼”)。所有這些保護(hù)措施都不太可能完全阻止非授權(quán)用戶操作機器。如果攻擊者能直接接觸電腦,就沒有可靠的方法保護(hù)電腦;他們可以拆下硬盤連到自己的電腦上,甚至是偷走整個機器,或者擦除 BIOS 來重置密碼… Systemd executes several processes, in charge of setting up the system: keyboard, drivers, filesystems, network, services. It does this while keeping a global view of the system as a whole, and the requirements of the components. Each component is described by a “unit file” (sometimes more); the general syntax is derived from the widely-used “*.ini files“ syntax, with
key = value pairs grouped between
[section] headers. Unit files are stored under
/lib/systemd/system/ and
/etc/systemd/system/; they come in several flavors, but we will focus on “services” and “targets” here. systemd “服務(wù)文件” 描述被 systemd 管理的進(jìn)程。包括與舊型的 init-scripts 相同的數(shù)據(jù),但以聲明 (同時較為簡潔) 的方式表述。Systemd 處理大量重復(fù)的工作 (啟動與終止進(jìn)程、檢查其狀態(tài)、日注記錄、去除特權(quán)等),以及只供特定進(jìn)程使用的服務(wù)文件。例如,以下是 SSH 用到的服務(wù)檔:
[Unit] Description=OpenBSD Secure Shell server After=network.target auditd.service ConditionPathExists=!/etc/ssh/sshd_not_to_be_run [Service] EnvironmentFile=-/etc/default/ssh ExecStart=/usr/sbin/sshd -D $SSHD_OPTS ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure [Install] WantedBy=multi-user.target Alias=sshd.service
如上文所示,代碼極少,只有聲明。Systemd 管理顯示進(jìn)度報表、追蹤進(jìn)程、以及必要的重啟。 systemd 的 “目標(biāo)文件” 描述系統(tǒng)的現(xiàn)狀,包括可操作的服務(wù)。不妨視為相當(dāng)于舊型的運行階段作業(yè)。其中一個目標(biāo)是
local-fs.target;進(jìn)入之后,系統(tǒng)的其他部分假設(shè)所有的本地文件系統(tǒng)均己掛載并可近用。其他的目標(biāo)包括
network-online.target 與
sound.target。目標(biāo)的相依性可以列在目標(biāo)文件內(nèi) (于
Requires= 列) 或使用符號鏈接至在
/lib/systemd/system/targetname.target.wants/ 文件夾內(nèi)的服務(wù)文件。例如,
/etc/systemd/system/printer.target.wants/ 包括一個鏈接至
/lib/systemd/system/cups.service;systemd 將確保 CUPS 已運行至
printer.target。 單元文件是聲明性的而不是腳本或程序,不能直接運行,只能被 systemd 解譯;因些有些工具允許管理者與 systemd 交互且控制系統(tǒng)的狀態(tài)與其組件。 第一種這類工具是
systemctl。未使用參數(shù)運行時,它列出 systemd 已知的所有單元檔 (除了已經(jīng)停用的),及其現(xiàn)況。
systemctl status 則以更佳的角度查看服務(wù),以及相關(guān)的進(jìn)程。若提供服務(wù)的名稱 (如
systemctl status ntp.service),則送回更多詳細(xì)的數(shù)據(jù),以及與該服務(wù)有關(guān)的最后幾個日志檔 (還有更多的)。 運行
systemctl start servicename.service 就能以人工方式啟動服務(wù)。同樣的,運行
systemctl stop servicename.service 就能停止已完成的服務(wù);其他的次命令包括
reload 與
restart。 以
systemctl enable servicename.service (或
disable) 控制啟動服務(wù) (即開機后自動啟動)。
is-enabled 可以檢查服務(wù)的狀態(tài)。 An interesting feature of systemd is that it includes a logging component named
journald. It comes as a complement to more traditional logging systems such as
syslogd, but it adds interesting features such as a formal link between a service and the messages it generates, and the ability to capture error messages generated by its initialization sequence. The messages can be displayed later on, with a little help from the
journalctl command. Without any arguments, it simply spews all log messages that occurred since system boot; it will rarely be used in such a manner. Most of the time, it will be used with a service identifier:
#journalctl -u ssh.service-- Logs begin at Tue 2015-03-31 10:08:49 CEST, end at Tue 2015-03-31 17:06:02 CEST. -- Mar 31 10:08:55 mirtuel sshd[430]: Server listening on 0.0.0.0 port 22. Mar 31 10:08:55 mirtuel sshd[430]: Server listening on :: port 22. Mar 31 10:09:00 mirtuel sshd[430]: Received SIGHUP; restarting. Mar 31 10:09:00 mirtuel sshd[430]: Server listening on 0.0.0.0 port 22. Mar 31 10:09:00 mirtuel sshd[430]: Server listening on :: port 22. Mar 31 10:09:32 mirtuel sshd[1151]: Accepted password for roland from 192.168.1.129 port 53394 ssh2 Mar 31 10:09:32 mirtuel sshd[1151]: pam_unix(sshd:session): session opened for user roland by (uid=0)
另個有用的命令行旗標(biāo)是
-f,用于指示
journalctl 繼續(xù)顯示溢出的添加消息 (大部分是在
tail -f file 之內(nèi))。 若服務(wù)狀況不如預(yù)共,第一個步驟是以
systemctl status 檢查該服務(wù)是否真的已啟動;若沒有,則第一個命令給的消息就不足以診斷問題之所在,檢查 journald 產(chǎn)生的日志檔。例如,假設(shè) SSH 服務(wù)器未啟動時:
#systemctl status ssh.service● ssh.service - OpenBSD Secure Shell server Loaded: loaded (/lib/systemd/system/ssh.service; enabled) Active: failed (Result: start-limit) since Tue 2015-03-31 17:30:36 CEST; 1s ago Process: 1023 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS) Process: 1188 ExecStart=/usr/sbin/sshd -D $SSHD_OPTS (code=exited, status=255) Main PID: 1188 (code=exited, status=255) Mar 31 17:30:36 mirtuel systemd[1]: ssh.service: main process exited, code=exited, status=255/n/a Mar 31 17:30:36 mirtuel systemd[1]: Unit ssh.service entered failed state. Mar 31 17:30:36 mirtuel systemd[1]: ssh.service start request repeated too quickly, refusing to start. Mar 31 17:30:36 mirtuel systemd[1]: Failed to start OpenBSD Secure Shell server. Mar 31 17:30:36 mirtuel systemd[1]: Unit ssh.service entered failed state. #journalctl -u ssh.service-- Logs begin at Tue 2015-03-31 17:29:27 CEST, end at Tue 2015-03-31 17:30:36 CEST. -- Mar 31 17:29:27 mirtuel sshd[424]: Server listening on 0.0.0.0 port 22. Mar 31 17:29:27 mirtuel sshd[424]: Server listening on :: port 22. Mar 31 17:29:29 mirtuel sshd[424]: Received SIGHUP; restarting. Mar 31 17:29:29 mirtuel sshd[424]: Server listening on 0.0.0.0 port 22. Mar 31 17:29:29 mirtuel sshd[424]: Server listening on :: port 22. Mar 31 17:30:10 mirtuel sshd[1147]: Accepted password for roland from 192.168.1.129 port 38742 ssh2 Mar 31 17:30:10 mirtuel sshd[1147]: pam_unix(sshd:session): session opened for user roland by (uid=0) Mar 31 17:30:35 mirtuel sshd[1180]: /etc/ssh/sshd_config line 28: unsupported option "yess". Mar 31 17:30:35 mirtuel systemd[1]: ssh.service: main process exited, code=exited, status=255/n/a Mar 31 17:30:35 mirtuel systemd[1]: Unit ssh.service entered failed state. Mar 31 17:30:35 mirtuel sshd[1182]: /etc/ssh/sshd_config line 28: unsupported option "yess". Mar 31 17:30:35 mirtuel systemd[1]: ssh.service: main process exited, code=exited, status=255/n/a Mar 31 17:30:35 mirtuel systemd[1]: Unit ssh.service entered failed state. Mar 31 17:30:35 mirtuel sshd[1184]: /etc/ssh/sshd_config line 28: unsupported option "yess". Mar 31 17:30:35 mirtuel systemd[1]: ssh.service: main process exited, code=exited, status=255/n/a Mar 31 17:30:35 mirtuel systemd[1]: Unit ssh.service entered failed state. Mar 31 17:30:36 mirtuel sshd[1186]: /etc/ssh/sshd_config line 28: unsupported option "yess". Mar 31 17:30:36 mirtuel systemd[1]: ssh.service: main process exited, code=exited, status=255/n/a Mar 31 17:30:36 mirtuel systemd[1]: Unit ssh.service entered failed state. Mar 31 17:30:36 mirtuel sshd[1188]: /etc/ssh/sshd_config line 28: unsupported option "yess". Mar 31 17:30:36 mirtuel systemd[1]: ssh.service: main process exited, code=exited, status=255/n/a Mar 31 17:30:36 mirtuel systemd[1]: Unit ssh.service entered failed state. Mar 31 17:30:36 mirtuel systemd[1]: ssh.service start request repeated too quickly, refusing to start. Mar 31 17:30:36 mirtuel systemd[1]: Failed to start OpenBSD Secure Shell server. Mar 31 17:30:36 mirtuel systemd[1]: Unit ssh.service entered failed state. #vi /etc/ssh/sshd_config#systemctl start ssh.service#systemctl status ssh.service● ssh.service - OpenBSD Secure Shell server Loaded: loaded (/lib/systemd/system/ssh.service; enabled) Active: active (running) since Tue 2015-03-31 17:31:09 CEST; 2s ago Process: 1023 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS) Main PID: 1222 (sshd) CGroup: /system.slice/ssh.service └─1222 /usr/sbin/sshd -D #
檢查服務(wù)的狀態(tài) (失敗) 后,再檢查日志檔;它們會指出配置的錯誤。編輯配置檔并修正錯誤后,重啟服務(wù),確認(rèn)運行中。
下一步 其他類型的單元檔
本區(qū)只描述 systemd 最基本的功能。其他的功能只能在此列出若干:
- 啟用插口:“插口” 單元文件可用于描述 systemd 管理的網(wǎng)絡(luò)或 Unix 插口;也就是由 systemd 創(chuàng)建的插口,可以在需要的時候再啟動實際的服務(wù)。通常重制
inetd的功能。見 systemd.socket(5)。 - 定時器:“定時器” 單元文件描述定時或在指定時間發(fā)生的事件;與定時器鏈接的服務(wù),其映射的工作將在定時器的要求下才運行。 允許重制
cron的部分功能。見 systemd.timer(5)。 - 網(wǎng)絡(luò):“網(wǎng)絡(luò)“ 單元文件描述網(wǎng)絡(luò)接口,允許配置該等接口以及表述在特定接口的服務(wù)。
9.1.2. System V 初始系統(tǒng)
System V 初始系統(tǒng) (簡稱初始) 運行若干進(jìn)程,根據(jù)
/etc/inittab 文件的指令做事。第一個運行的程序 (映射于
sysinit 步驟) 是
/etc/init.d/rcS,一個運行在
/etc/rcS.d/ 文件夾內(nèi)所有程序的腳本。 其中,你會發(fā)現(xiàn)相繼的程序會負(fù)責(zé):
- 配置控制臺鍵盤;
- 加載驅(qū)動:當(dāng)探測到硬件,大部分內(nèi)核模塊通過內(nèi)核自身加載;然后,自動加載
/etc/modules中列出的模塊; - 檢查文件系統(tǒng)的完整性;
- 掛載本地分區(qū);
- 配置網(wǎng)絡(luò);
- 掛載網(wǎng)絡(luò)文件系統(tǒng)(NFS)。
回到基礎(chǔ) 內(nèi)核模塊和選項
內(nèi)核模塊也有一些選項,可以通過在
/etc/modprobe.d/ 中放置一些文件配置。這些選項通過諸如此類的語法定義:
選項 模塊名字 選項名字=選項值。如果有必要,一些選項可以通過單獨的定向命令指示。 這些配置檔系供
modprobe 使用 — 這個程序加載核心模塊及其相依者 (才能直正的調(diào)用其他模塊)。這個程序由
kmod 軟件包提供。 到了這個地步,
init 接手并啟動運行階段缺省的程序 (通常是運行階段 2)。它運行
/etc/init.d/rc 2,一個啟動列在
/etc/rc2.d/ 之內(nèi)的所有服務(wù)并命名為 “S” 字母開頭。接著的兩位數(shù),曾經(jīng)做為服務(wù)啟動的順序,不過現(xiàn)在的缺省啟動系統(tǒng)使用
insserv,根據(jù)腳本的相依性自動決定其先后順序。每個啟動腳本聲明的情況必須符合啟動或停止服務(wù) (例如,必須在另個服務(wù)之前或之后啟動);
init 再依此情況啟動它們。不再考慮靜態(tài)的腳本編號 (但仍需按相依性使用 “S” 及兩個數(shù)字與實際的腳本名稱)。通常,基本的服務(wù) (諸如以
rsyslog 登錄,或以
portmap 指定端口口) 先列出來,然后才是標(biāo)準(zhǔn)服務(wù)與圖形接口 (
gdm3)。 這種以依賴為基礎(chǔ)的啟動系統(tǒng)使自動排序成為可能,這樣的排序如果要手工完成則顯得冗長乏味。由于調(diào)度根據(jù)明確給出的參數(shù)進(jìn)行,這樣就避免了人為錯誤。另一個好處是,如果兩個服務(wù)彼此獨立,則可以并行啟動,進(jìn)而加速啟動過程。
init分幾個運行等級,它可以通過
telinit new-level 命令,從一個等級切換到另一個等級。馬上就會在新等級下重新執(zhí)行
init executes
/etc/init.d/rc。這個腳本會啟動漏掉的服務(wù)并中止不再需要的服務(wù)。為了做到這一點,它讀取
/etc/rcX.d 文件的內(nèi)容(此處
X 代表新的運行等級)。以“S"(Start的首字母)開頭的服務(wù)腳本要啟動;以“K"(Kill的首字母)開頭的服務(wù)要停止。腳本不會啟動在之前運行等級已經(jīng)生效的服務(wù)。 缺省,Debian 的 System V init 使用四個不同的運行階層:
- 等級0僅作電腦關(guān)機時的臨時應(yīng)用。這樣,它只包含許多“K”腳本。
- 等級1,也被稱為單用戶模式,對應(yīng)于降級的系統(tǒng)模式;它僅包含基本服務(wù),用于維護(hù),此時不需要與一般用戶交互。
- 等級2用于正常運行,包含網(wǎng)絡(luò)服務(wù),圖形界面,用戶登陸,等等。
- 等級6和等級0類似,不同在于它用于系統(tǒng)重啟之前的關(guān)機。
也存在其他等級,從3到5。默認(rèn)情況,他們配置為和等級2相同,但是管理員可以修改(通過添加和刪除對應(yīng)
/etc/rcX.d目錄下的腳本)它們來適應(yīng)不同的需求。
圖 9.2. 以 System V init 運行 Linux 的啟動進(jìn)程
所有包含在
/etc/rcX.d目錄下的腳本都只是符號聯(lián)接-有
update-rc.d程序在安裝時創(chuàng)建-指向存儲在
/etc/init.d/中的實際腳本。管理員可以使用調(diào)整后的參數(shù)重新運行
update-rc.d 來微調(diào)每個運行等級的服務(wù)。
update-rc.d(1)手冊詳細(xì)介紹了語法。請注意,使用
remove 參數(shù)移除所有的符號連接不是停用服務(wù)的好辦法。取而代之的方法是,你可以在特定的運行等級將其配置為不啟動(而保留先前等級對應(yīng)事件的停止調(diào)用)。由于
update-rc.d的接口有些繞,可以考慮使用
rcconf(出自
rcconf 軟件包),它提供了更加友好的界面。
DEBIAN 策略 重啟服務(wù)
Debian 軟件包的維護(hù)者腳本將不時重新啟動特定服務(wù)以確保其有效性或取得特定的選項??刂品?wù)的命令 —
service service operation — 未考量運行階層,假設(shè) (錯誤地) 該服務(wù)仍在使用中,且可能啟動錯誤的作業(yè) (啟動應(yīng)該停止的服務(wù),或停止已經(jīng)停止的服務(wù)等)。因此,Debian 有個
invoke-rc.d 程序:必須由維護(hù)者的腳本啟動,運行服務(wù)的初啟腳本且只運行必要的命令。注意,不同于常見的用法,在程序名之前使用
.d 前置,且不能在文件夾內(nèi)。 最后,
init啟動各種虛擬控制臺的控制程序(
getty)。顯示提示符,等待輸入用戶名,然后執(zhí)行
login user發(fā)起會話。
詞匯 控制臺和終端
早期的計算機通常被分成幾個很大的部分:存儲箱和中央處理單元與外圍操作員的控制設(shè)備相分離。這些部件是單獨的裝置,即“控制臺”。該術(shù)語被保留下來,但是其意義改變了?;蚨嗷蛏偎呀?jīng)和“終端”同義,代表鍵盤和屏幕。 隨著計算機的發(fā)展,操作系統(tǒng)提供了許多虛擬終端,從而允許同時存在幾個獨立的會話,即使是只有一套鍵盤和屏幕。大部分 GNU/Linux 系統(tǒng)提供六個虛擬終端(在文本模式),通過組合鍵
Control+
Alt+
F1 到
Control+
Alt+
F6 開啟。 推而廣之,術(shù)語“控制臺”和“終端”也代表在X11圖形會話中的終端模擬器(類似
xterm,
gnome-terminal 或者
konsole)。
網(wǎng)頁標(biāo)題:【Debian管理員手冊】第?9?章?Unix服務(wù)
文章位置:http://www.dlmjj.cn/article/dpcecpd.html


咨詢
建站咨詢
