場景:在使用chkconfig查看vsftpd是否看機啟動時候看不到啟動項,用systemctl 才看到自己想要的結果
1 總結
from:https://linux.cn/article-5926-1.html(相當的詳盡)
Systemctl是一個systemd工具,主要負責控制systemd系統和服務管理器。
Systemd是一個系統管理守護進程、工具和庫的集合,用於取代System V初始進程。Systemd的功能是用於集中管理和配置類UNIX系統。
先來個簡單總結(后面才是from的鏈接的內容):下面是以postfix服務為例
啟動一個服務: systemctl start postfix.service 關閉一個服務: systemctl stop postfix.service 重啟一個服務: systemctl restart postfix.service 顯示一個服務的狀態: systemctl status postfix.service 在開機時啟用一個服務: systemctl enable postfix.service 在開機時禁用一個服務: systemctl disable postfix.service 查看服務是否開機啟動: systemctl is-enabled postfix.service 查看已啟動的服務列表: systemctl list-unit-files|grep enabled
說明:啟用服務就是在當前“runlevel”的配置文件目錄/etc/systemd/system/multi-user.target.wants/里,建立/usr/lib/systemd/system里面對應服務配置文件的軟鏈接;禁用服務就是刪除此軟鏈接。
既然都能夠設置開機啟動項,讓我不禁疑惑chkconfig和systemctl 的區別?????下面就來個對比
2 service與chkconfig的替代者systemctl
linux中有很多命令已經存在了N多年,漸漸一些已被一些新命令所代替,不過由於習慣的原因,很多時候我們並不能一下子適應過來 ,例如ifconfig之於ip命令。最近在玩ubuntu和opensuse時發現了systemctl命令了,后來在試玩centos7時也發現了該命令,systemctl是systemd下的一個工具。網上查了下,該命令已經存在很久了。該命令是用來替代service和chkconfig兩個命令的 --- 盡管個人感覺還是后者好用。
為了順應時間的發展,這里總結下。在目前很多linux的新發行版本里,系統對於daemon的啟動管理方法不再采用SystemV形式,而是使用了sytemd的架構來管理daemon的啟動。
2.1 runlevel 到 target的改變
在systemd的管理體系里面,以前的運行級別(runlevel)的概念被新的運行目標(target)所取代。tartget的命名類似於multi-user.target等這種形式,比如原來的運行級別3(runlevel3)就對應新的多用戶目標(multi-user.target),run level 5就相當於graphical.target。
由於不再使用runlevle概念,所以/etc/inittab也不再被系統使用 --- 無怪乎在新版本ubuntu上找不到inittab文件了。
而在systemd的管理體系里面,默認的target(相當於以前的默認運行級別)是通過軟鏈來實現。如:
ln -s /lib/systemd/system/runlevel3.target /etc/systemd/system/default.target
在/lib/systemd/system/ 下面定義runlevelX.target文件,目的主要是為了能夠兼容以前的運行級別level的管理方法。 事實上/lib/systemd/system/runlevel3.target,同樣是被軟連接到multi-user.target。
注:opensuse下是在/usr/lib/systemd/system/目錄下。
2.2 單元控制(unit)
在systemd管理體系里,稱呼需要管理的daemon為單元(unit)。對於單元(unit)的管理是通過命令systemctl來進行控制的。例如顯示當前的處於運行狀態的unit(即daemon),如:
#systemctl #systemctl --all #systemctl list-units --type=sokect #systemctl list-units --type=service
注:type后面可以接的類型可以通過help查看
361way:~ # systemctl -t help
Available unit types:
service
socket
target
device
mount
automount
snapshot
timer
swap
path
slice
scope

systemctl 默認有5列輸出,如下:
361way:~ # systemctl
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File System Automount Point
列表中的第一欄是單位的名字;
第二欄則表示該單位的定義是否已由 systemd 正確加載;
第三欄則告訴我們該單位是否正在運行。如果你使用了 -a 參數,那么該程序將僅顯示非正在運行的單位,即已安裝但並未在啟動時使用的單位,同時也包含引導系統未能正常加載的單位文件(原因很可能為該單位文件出現錯誤);
第四欄則給出了當前狀態:“exited”表示該進程已經無任何錯誤地完成,這種情況適用於一諸如進程在啟動后並不在后台繼續運行的情況,例如,在系統啟動時由於考慮到兼容性因素執行在 sysvinit 里面常用的 /etc/rc.d/rc.local 文件的服務單位。“Running”表示正在后台運行的服務,如 cron、dbus、sshd 和 udev ;
第五欄是對該單位的描述。標有“LSB”或“SYSV”的單位已由 systemd 自動創建以管理傳統啟動腳本。
不能啟動或啟動后崩潰的服務在第四欄中用紅色標為“failed”(如果終端可以顯示彩色)。
對於失敗的服務,可以通過systemctl status 服務名 的方式查看詳細信息,具體輸出信息,systemctl用法和示例中會給出。
2.3 systemctl用法及示例
chkconfig、service命令與systemctl命令的區別見下表:
| 任務 | 舊指令 | 新指令 |
| 使某服務自動啟動 | chkconfig --level 3 httpd on | systemctl enable httpd.service |
| 使某服務不自動啟動 | chkconfig --level 3 httpd off | systemctl disable httpd.service |
| 檢查服務狀態 | service httpd status | systemctl status httpd.service (服務詳細信息) systemctl is-active httpd.service (僅顯示是否 Active) |
| 加入自定義服務 | chkconfig --add test | systemctl load test.service |
| 刪除服務 | chkconfig --del xxx | 停掉應用,刪除相應的配置文件 |
| 顯示所有已啟動的服務 | chkconfig --list | systemctl list-units --type=service |
| 啟動某服務 | service httpd start | systemctl start httpd.service |
| 停止某服務 | service httpd stop | systemctl stop httpd.service |
| 重啟某服務 | service httpd restart | systemctl restart httpd.service |
注:systemctl后的服務名可以到/usr/lib/systemd/system目錄查看(opensuse下),其他發行版是位於/lib/systemd/system/ 下。
//opensuse下 361way:~ # systemctl status network.service network.service - LSB: Configure network interfaces and set up routing Loaded: loaded (/usr/lib/systemd/system/network.service; enabled) Active: active (exited) since Mon 2014-09-01 20:05:45 CST; 2h 14min ago Process: 1022 ExecStart=/etc/init.d/network start (code=exited, status=0/SUCCESS) Sep 01 20:05:15 linux-pnp8 systemd[1]: Starting LSB: Configure network interfaces and set up routing... Sep 01 20:05:15 linux-pnp8 network[1022]: Setting up network interfaces: Sep 01 20:05:15 linux-pnp8 network[1022]: lo Sep 01 20:05:15 linux-pnp8 network[1022]: lo IP address: 127.0.0.1/8 Sep 01 20:05:45 linux-pnp8 network[1022]: ..done..done..doneSetting up service network . . . . . . . . . . . . ...done Sep 01 20:05:45 linux-pnp8 systemd[1]: Started LSB: Configure network interfaces and set up routing. //centos下,apache服務未啟動時的顯示 # systemctl status httpd.service httpd.service - The Apache HTTP Server (prefork MPM) Loaded: loaded (/lib/systemd/system/httpd.service; disabled) Active: inactive (dead) <-- 表示未啟動 CGroup: name=systemd:/system/httpd.service
上面兩個命令相當於/etc/init.d/network status 和 /etc/init.d/httpd status,opensuse和centos下的用法相同,只不過顯示的路徑不同。其他操作類似。
2.4 service配置文件
還以上面提到的httpd.service配置為例,httpd.service文件里可以找到如下行:
[Install]
WantedBy=multi-user.target
則表明在多用戶目標(multi-user.target,相當於level3)時自動啟動。如果想在runlevel 5下也自啟動,則可以將配置改為如下:
[Install]
WantedBy=multi-user.target graphical.target
一旦設定了自動啟動(enbale),就在/etc/systemd/system/<target名>.wants/下面建了一個httpd.service的軟連接,連接到/lib/systemd/system/下的相應服務那里 。所以顯示自動啟動狀態的unit (類似於chekconfig --list命令的結果),可以通過下面的方法:
#ls /etc/systemd/system/multi-user.target.wants/
systemctl的總結先寫到這里,其是systemd包內的一個工具,也是該包中最為常用的工具。回頭再針對systemd做一個總結。
