linux中有很多命令已經存在了N多年,漸漸一些已被一些新命令所代替,不過由于習慣的原因,很多時候我們并不能一下子適應過來 ,例如ifconfig之于ip命令。
最近在玩ubuntu和opensuse時發現了systemctl命令了,后來在試玩centos7時也發現了該命令,systemctl是systemd下的一個工具。網上查了下,該命令已經存在很久了。該命令是用來替代service和chkconfig兩個命令的 --- 盡管個人感覺還是后者好用。
為了順應時間的發展,這里總結下。在目前很多linux的新發行版本里,系統對于daemon的啟動管理方法不再采用SystemV形式,而是使用了sytemd的架構來管理daemon的啟動。
一、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/目錄下。
二、單元控制(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用法及示例
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下 --Vevb.com
- # 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下的用法相同,只不過顯示的路徑不同。其他操作類似。
四、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/.wants/下面建了一個httpd.service的軟連接,連接到/lib/systemd/system/下的相應服務那里,所以顯示自動啟動狀態的unit(類似于chekconfig --list命令的結果),可以通過下面的方法:
#ls /etc/systemd/system/multi-user.target.wants/
systemctl的總結先寫到這里,其是systemd包內的一個工具,也是該包中最為常用的工具。回頭再針對systemd做一個總結.
新聞熱點
疑難解答