新聞中心
- 9.1. 系統(tǒng)啟動
-
- 9.1.1. Systemd 啟動系統(tǒng)
- 9.1.2. System V 初始系統(tǒng)
- 9.2. 遠程登錄
-
- 9.2.1. 安全遠程登錄:SSH
- 9.2.2. 使用遠程圖形桌面
- 9.3. 管理權限
- 9.4. 管理員界面
-
-
9.4.1. 管理網頁接口:
webmin -
9.4.2. 用于管理配置的軟件包:
debconf
-
9.4.1. 管理網頁接口:
-
9.5.
syslog系統(tǒng)事件 -
- 9.5.1. 原則與機制
- 9.5.2. 配置文件
-
9.6. The
inetd超級服務器 -
9.7. 使用
cron和atd運行計劃任務 -
-
9.7.1.
crontab文件的格式 -
9.7.2. 使用
at命令
-
9.7.1.
-
9.8. 調度異步任務:
anacron - 9.9. 配額
- 9.10. 備份
-
-
9.10.1. 使用
rsync備份 - 9.10.2. 不使用備份恢復系統(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)共通的服務。管理員應當熟悉他們。

9.1. 系統(tǒng)啟動
當啟動計算機時,控制臺上滾動的大量信息顯示許多初始化和配置工作自動正在執(zhí)行。有時候你可能稍稍的改變這一階段的操作,就要求你需要很好的理解他們。這正是本章節(jié)的目的所在。 首先,BIOS 控制電腦,探測磁盤,加載
Master Boot Record,并執(zhí)行啟動加載器。啟動程序接手后,找到磁盤上的內核,加載并執(zhí)行。然后,內核被初始化,并開始尋找和掛載包含根文件系統(tǒng)的分區(qū),最后執(zhí)行第一個程序-
init。根分區(qū)和啟動程序
init常常駐留在僅存在于 RAM 中的虛擬文件系統(tǒng)(正如它的名字,“initramfs”,一般稱之為“initrd”初始內存磁盤)。啟動加載器將文件系統(tǒng)加載到內存中,文件通常位于硬盤或者源于網絡。它包含了內核需要的最少裸信息,以便用來加載“真正”的根文件系統(tǒng):可能是硬盤上的驅動模塊,或者其他系統(tǒng)啟動必須的設備,或者更常見的是初始化腳本和模塊以組建 RAID 陣列,打開加密分區(qū),激活 LVM ,等等。一旦根分區(qū)掛載,initramfs 就會把控制權交到真正的啟動程序,機器則回到標準的啟動過程。
9.1.1. Systemd 啟動系統(tǒng)
“真正的啟動器”當前是由
systemd 提供的,本章節(jié)講述該啟動系統(tǒng)。
文化 在 systemd 之前
systemd 是相當新的 “啟動系統(tǒng)”,雖然在
Wheezy 里已經可以使用到某個程度,直到 Debian
Jessie 才納入默認值。稍最的版本,缺省是 “System V init” (在
sysv-rc 軟件包內),算是傳統(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哪些進程必須啟動及其順序。 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 計算機的啟動順序
特例 從網絡啟動
在某些配置中,BIOS 可以配置為不執(zhí)行 MBR,而是在網絡上尋找類似的東西,這樣就可以制作不需要硬盤的電腦,在每次啟動后可以完全重裝。不是所有的硬件都支持該選項,它需要 BIOS 和網卡很好的配合。 從網絡啟動可以用于執(zhí)行
debian-installer or FAI (參考 第 4.1 節(jié) “安裝方式”)。
回到基礎進程,一個程序實例
一個進程代表一個在內存中運行的程序。它包含了需要正確執(zhí)行軟件的所有必要信息(代碼本身,內存數據,打開的文件清單,建立的網絡連接,等等)。一個程序可能初始化為幾個進程,而沒必要使用不同的用戶 ID 。
安全 使用 shell 作為 init 獲取超級用戶權限
通常,第一個啟動的進程是
init 程序(默認是到
/lib/systemd/systemd 的符號鏈接)。然而,也可以通過傳遞
init 選項告訴內核使用其他的程序。 任何可以接近電腦的人都能按下
復位 按,并重新啟動。然后,在啟動提示下,傳遞
init=/bin/sh選項給內核,無需知道密碼而獲取超級用戶權限。 為了防止此類事件,可以給加載器設定密碼。你也許會考慮保護 BIOS (密碼保護總是可行的),這樣可以防止侵入者使用包含自己的 Linux 系統(tǒng)的移動介質啟動電腦,使用該系統(tǒng)他們可以讀取硬盤上的數據。 最后,應該知道大多數 BIOS 有一個通用的密碼。最初用于讓那些忘記自己密碼的人解決問題,這些密碼現在是公開的并且可以在網上找到(通過搜索引擎搜索“通用 BIOS 密碼”)。所有這些保護措施都不太可能完全阻止非授權用戶操作機器。如果攻擊者能直接接觸電腦,就沒有可靠的方法保護電腦;他們可以拆下硬盤連到自己的電腦上,甚至是偷走整個機器,或者擦除 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 “服務文件” 描述被 systemd 管理的進程。包括與舊型的 init-scripts 相同的數據,但以聲明 (同時較為簡潔) 的方式表述。Systemd 處理大量重復的工作 (啟動與終止進程、檢查其狀態(tài)、日注記錄、去除特權等),以及只供特定進程使用的服務文件。例如,以下是 SSH 用到的服務檔:
[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 管理顯示進度報表、追蹤進程、以及必要的重啟。 systemd 的 “目標文件” 描述系統(tǒng)的現狀,包括可操作的服務。不妨視為相當于舊型的運行階段作業(yè)。其中一個目標是
local-fs.target;進入之后,系統(tǒng)的其他部分假設所有的本地文件系統(tǒng)均己掛載并可近用。其他的目標包括
network-online.target 與
sound.target。目標的相依性可以列在目標文件內 (于
Requires= 列) 或使用符號鏈接至在
/lib/systemd/system/targetname.target.wants/ 文件夾內的服務文件。例如,
/etc/systemd/system/printer.target.wants/ 包括一個鏈接至
/lib/systemd/system/cups.service;systemd 將確保 CUPS 已運行至
printer.target。 單元文件是聲明性的而不是腳本或程序,不能直接運行,只能被 systemd 解譯;因些有些工具允許管理者與 systemd 交互且控制系統(tǒng)的狀態(tài)與其組件。 第一種這類工具是
systemctl。未使用參數運行時,它列出 systemd 已知的所有單元檔 (除了已經停用的),及其現況。
systemctl status 則以更佳的角度查看服務,以及相關的進程。若提供服務的名稱 (如
systemctl status ntp.service),則送回更多詳細的數據,以及與該服務有關的最后幾個日志檔 (還有更多的)。 運行
systemctl start servicename.service 就能以人工方式啟動服務。同樣的,運行
systemctl stop servicename.service 就能停止已完成的服務;其他的次命令包括
reload 與
restart。 以
systemctl enable servicename.service (或
disable) 控制啟動服務 (即開機后自動啟動)。
is-enabled 可以檢查服務的狀態(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)
另個有用的命令行旗標是
-f,用于指示
journalctl 繼續(xù)顯示溢出的添加消息 (大部分是在
tail -f file 之內)。 若服務狀況不如預共,第一個步驟是以
systemctl status 檢查該服務是否真的已啟動;若沒有,則第一個命令給的消息就不足以診斷問題之所在,檢查 journald 產生的日志檔。例如,假設 SSH 服務器未啟動時:
#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 #
檢查服務的狀態(tài) (失敗) 后,再檢查日志檔;它們會指出配置的錯誤。編輯配置檔并修正錯誤后,重啟服務,確認運行中。
下一步 其他類型的單元檔
本區(qū)只描述 systemd 最基本的功能。其他的功能只能在此列出若干:
- 啟用插口:“插口” 單元文件可用于描述 systemd 管理的網絡或 Unix 插口;也就是由 systemd 創(chuàng)建的插口,可以在需要的時候再啟動實際的服務。通常重制
inetd的功能。見 systemd.socket(5)。 - 定時器:“定時器” 單元文件描述定時或在指定時間發(fā)生的事件;與定時器鏈接的服務,其映射的工作將在定時器的要求下才運行。 允許重制
cron的部分功能。見 systemd.timer(5)。 - 網絡:“網絡“ 單元文件描述網絡接口,允許配置該等接口以及表述在特定接口的服務。
9.1.2. System V 初始系統(tǒng)
System V 初始系統(tǒng) (簡稱初始) 運行若干進程,根據
/etc/inittab 文件的指令做事。第一個運行的程序 (映射于
sysinit 步驟) 是
/etc/init.d/rcS,一個運行在
/etc/rcS.d/ 文件夾內所有程序的腳本。 其中,你會發(fā)現相繼的程序會負責:
- 配置控制臺鍵盤;
- 加載驅動:當探測到硬件,大部分內核模塊通過內核自身加載;然后,自動加載
/etc/modules中列出的模塊; - 檢查文件系統(tǒng)的完整性;
- 掛載本地分區(qū);
- 配置網絡;
- 掛載網絡文件系統(tǒng)(NFS)。
回到基礎 內核模塊和選項
內核模塊也有一些選項,可以通過在
/etc/modprobe.d/ 中放置一些文件配置。這些選項通過諸如此類的語法定義:
選項 模塊名字 選項名字=選項值。如果有必要,一些選項可以通過單獨的定向命令指示。 這些配置檔系供
modprobe 使用 — 這個程序加載核心模塊及其相依者 (才能直正的調用其他模塊)。這個程序由
kmod 軟件包提供。 到了這個地步,
init 接手并啟動運行階段缺省的程序 (通常是運行階段 2)。它運行
/etc/init.d/rc 2,一個啟動列在
/etc/rc2.d/ 之內的所有服務并命名為 “S” 字母開頭。接著的兩位數,曾經做為服務啟動的順序,不過現在的缺省啟動系統(tǒng)使用
insserv,根據腳本的相依性自動決定其先后順序。每個啟動腳本聲明的情況必須符合啟動或停止服務 (例如,必須在另個服務之前或之后啟動);
init 再依此情況啟動它們。不再考慮靜態(tài)的腳本編號 (但仍需按相依性使用 “S” 及兩個數字與實際的腳本名稱)。通常,基本的服務 (諸如以
rsyslog 登錄,或以
portmap 指定端口口) 先列出來,然后才是標準服務與圖形接口 (
gdm3)。 這種以依賴為基礎的啟動系統(tǒng)使自動排序成為可能,這樣的排序如果要手工完成則顯得冗長乏味。由于調度根據明確給出的參數進行,這樣就避免了人為錯誤。另一個好處是,如果兩個服務彼此獨立,則可以并行啟動,進而加速啟動過程。
init分幾個運行等級,它可以通過
telinit new-level 命令,從一個等級切換到另一個等級。馬上就會在新等級下重新執(zhí)行
init executes
/etc/init.d/rc。這個腳本會啟動漏掉的服務并中止不再需要的服務。為了做到這一點,它讀取
/etc/rcX.d 文件的內容(此處
X 代表新的運行等級)。以“S"(Start的首字母)開頭的服務腳本要啟動;以“K"(Kill的首字母)開頭的服務要停止。腳本不會啟動在之前運行等級已經生效的服務。 缺省,Debian 的 System V init 使用四個不同的運行階層:
- 等級0僅作電腦關機時的臨時應用。這樣,它只包含許多“K”腳本。
- 等級1,也被稱為單用戶模式,對應于降級的系統(tǒng)模式;它僅包含基本服務,用于維護,此時不需要與一般用戶交互。
- 等級2用于正常運行,包含網絡服務,圖形界面,用戶登陸,等等。
- 等級6和等級0類似,不同在于它用于系統(tǒng)重啟之前的關機。
也存在其他等級,從3到5。默認情況,他們配置為和等級2相同,但是管理員可以修改(通過添加和刪除對應
/etc/rcX.d目錄下的腳本)它們來適應不同的需求。
圖 9.2. 以 System V init 運行 Linux 的啟動進程
所有包含在
/etc/rcX.d目錄下的腳本都只是符號聯接-有
update-rc.d程序在安裝時創(chuàng)建-指向存儲在
/etc/init.d/中的實際腳本。管理員可以使用調整后的參數重新運行
update-rc.d 來微調每個運行等級的服務。
update-rc.d(1)手冊詳細介紹了語法。請注意,使用
remove 參數移除所有的符號連接不是停用服務的好辦法。取而代之的方法是,你可以在特定的運行等級將其配置為不啟動(而保留先前等級對應事件的停止調用)。由于
update-rc.d的接口有些繞,可以考慮使用
rcconf(出自
rcconf 軟件包),它提供了更加友好的界面。
DEBIAN 策略 重啟服務
Debian 軟件包的維護者腳本將不時重新啟動特定服務以確保其有效性或取得特定的選項??刂品盏拿?—
service service operation — 未考量運行階層,假設 (錯誤地) 該服務仍在使用中,且可能啟動錯誤的作業(yè) (啟動應該停止的服務,或停止已經停止的服務等)。因此,Debian 有個
invoke-rc.d 程序:必須由維護者的腳本啟動,運行服務的初啟腳本且只運行必要的命令。注意,不同于常見的用法,在程序名之前使用
.d 前置,且不能在文件夾內。 最后,
init啟動各種虛擬控制臺的控制程序(
getty)。顯示提示符,等待輸入用戶名,然后執(zhí)行
login user發(fā)起會話。
詞匯 控制臺和終端
早期的計算機通常被分成幾個很大的部分:存儲箱和中央處理單元與外圍操作員的控制設備相分離。這些部件是單獨的裝置,即“控制臺”。該術語被保留下來,但是其意義改變了?;蚨嗷蛏偎呀浐汀敖K端”同義,代表鍵盤和屏幕。 隨著計算機的發(fā)展,操作系統(tǒng)提供了許多虛擬終端,從而允許同時存在幾個獨立的會話,即使是只有一套鍵盤和屏幕。大部分 GNU/Linux 系統(tǒng)提供六個虛擬終端(在文本模式),通過組合鍵
Control+
Alt+
F1 到
Control+
Alt+
F6 開啟。 推而廣之,術語“控制臺”和“終端”也代表在X11圖形會話中的終端模擬器(類似
xterm,
gnome-terminal 或者
konsole)。
本文題目:【Debian管理員手冊】第?9?章?Unix服務
文章源于:http://fisionsoft.com.cn/article/dpcecpd.html


咨詢
建站咨詢
