天天看點

Centos7下的systemctl指令與service和chkconfig

部落客使用的作業系統是最新的CentOS 7,是以可能和網上一些老的博文有一定出入,那是因為版本更新的原因。

這裡寫圖檔描述

1 service

service指令用于對系統服務進行管理,比如啟動(start)、停止(stop)、重新開機(restart)、重新加載配置(reload)、檢視狀态(status)等。

下面我們來看看在Centos 7上service指令的使用情況。

相信看到這裡,大家已經發現問題了。那就是service指令的使用情況已經和以前的老版本不一樣了。(作為一個初學Linux的人,我也是通過看網上的資料,發現把網友貼出的指令執行時,發現的問題。)同樣的情況對于 chkconfig 指令也是一樣。

2 chkconfig

老版本的使用說明:

chkconfig

提供了一個維護/etc/rc[0~6] d 檔案夾的指令行工具,它減輕了系統直接管理這些檔案夾中的符号連接配接的負擔。chkconfig主要包括5個原始功能:為系統管理增加新的服務、為系統管理移除服務、列出單簽服務的啟動資訊、改變服務的啟動資訊和檢查特殊服務的啟動狀态。當單獨運作chkconfig指令而不加任何參數時,他将顯示服務的使用資訊。

必要參數

–add 開啟指定的服務程式

–del 關閉指定的服務程式

–list 列出chkconfig所知道的所有服務

選擇參數

–level<代号> 設定服務程式的等級代号,它是一串0~7的數字,如“-level35”代表指定運作等級3和5

–help 顯示幫助資訊

–version 顯示版本資訊

我們在Centos7中試一試嘛。

是以問題已經很明顯了,service和chkconfig指令的功能好像都被閹割了,而且好像已經被systemctl指令取代了。是這樣嗎?我們來看一看systemctl指令的介紹。

3 systemctl

文檔中是這麼介紹它的:

systemctl may be used to introspect and control the state of the “systemd” system and service manager.

再結合上面兩個指令的執行結果,我們已經可以大緻猜出systemctl的作用了,那就是:主要負責控制systemd系統和服務管理器。

什麼是systemd系統?

CentOS 7 使用systemd替換了SysV。Systemd目的是要取代Unix時代以來一直在使用的init系統,相容SysV和LSB的啟動腳本,而且夠在程序啟動過程中更有效地引導加載服務。

systemd的特性有:

    支援并行化任務;

    同時采用socket式與D-Bus總線式激活服務;

    按需啟動守護程序(daemon);

    利用 Linux 的 cgroups 監視程序;

    支援快照和系統恢複;

    維護挂載點和自動挂載點;

    各服務間基于依賴關系進行精密控制。

我們再來看看維基百科對它的介紹:

systemd is an init system used by some Linux distributions to bootstrap the user space and manage all processes subsequently, instead of the UNIX System V or Berkeley Software Distribution (BSD) init systems. The name systemd adheres to the Unix convention of naming daemons by appending the letter d.[6] It is published as free and open-source software under the terms of the GNU Lesser General Public License (LGPL) version 2.1 or later.[5] One of systemd’s main goals is to unify basic Linux configurations and service behaviors across all distributions.[7]

As of 2015, many Linux distributions have adopted systemd as their default init system.[8] The increasing adoption of systemd has been controversial, with critics arguing the software has violated the Unix philosophy by becoming increasingly complex, and that distributions have been forced to adopt it due to the dependency of various other software upon it, including, most notably, the GNOME 3 desktop environment.

總結一下關鍵資訊:

    systemd是一個取代了SysV和LSB的初始化系統;

    現在的大多數Linux發行版本都進行了這個更新;

    systemd不僅僅隻是個初始化系統,它還包括了還包括了管理系統各種的方面的 daemon;

    systemd是大勢所趨又存在争議。

是以,我們可以把systemctl了解為systemd的一個工具。也可以認為systemctl指令将service和chkconfig指令結合在了一起。總之,需要的時候會用就行。下面我們來看一些常見用法。

檢視systemctl的相關資訊

[root@master ~]# systemctl --version

systemd 219

+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN

[root@master ~]# whereis systemctl

systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz

[root@master ~]#

    1

    2

    3

    4

    5

    6

列出所有可用單元

# systemctl list-unit-files

列出所有運作中單元

# systemctl list-units

列出所有失敗的單元

[root@master ~]# systemctl --failed

0 loaded units listed. Pass --all to see loaded but inactive units, too.

To show all installed unit files use 'systemctl list-unit-files'.

檢查某個單元是否啟用

[root@master ~]# systemctl is-enabled mysqld.service

disabled

檢視某個服務(單元)的狀态

[root@master ~]# systemctl status firewalld.service

● firewalld.service - firewalld - dynamic firewall daemon

   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)

   Active: inactive (dead)

*啟動、重新開機、停止、重載服務

# systemctl start httpd.service

# systemctl restart httpd.service

# systemctl stop httpd.service

# systemctl reload httpd.service

# systemctl status httpd.service

*激活/禁止自動啟動

# systemctl enable httpd.service

# systemctl disable httpd.service

*殺死服務

# systemctl kill httpd

以上就是我目前需要用的和想要和大家分享的功能,下面的隻是補充,也沒有詳細的示例,是以有興趣的朋友可以再簡單看一看說明文檔就好了。

附錄

使用Systemctl控制并管理挂載點:

systemctl list-unit-files --type=mount

# systemctl start tmp.mount

# systemctl stop tmp.mount

# systemctl restart tmp.mount

# systemctl reload tmp.mount

# systemctl status tmp.mount

# systemctl is-active tmp.mount

# systemctl enable tmp.mount

# systemctl disable tmp.mount

# systemctl mask tmp.mount

    7

    8

    9

    10

    11

    12

    13

使用Systemctl控制并管理套接口

# systemctl list-unit-files --type=socket

# systemctl start cups.socket

# systemctl restart cups.socket

# systemctl stop cups.socket

# systemctl reload cups.socket

# systemctl status cups.socket

# systemctl is-active cups.socket

# systemctl enable cups.socket

# systemctl disable cups.socket

# systemctl mask cups.socket

使用Systemctl管理服務的CPU使用率

# systemctl show -p CPUShares httpd.service

# systemctl set-property httpd.service CPUShares=2000

# systemctl show httpd

其他

# systemd-analyze critical-chain httpd.service

# systemctl list-dependencies httpd.service

# systemd-cgls

# systemd-cgtop

# systemctl emergency

# systemctl reboot

# systemctl halt

# systemctl suspend

# systemctl hibernate

# systemctl hybrid-sleep

---------------------

繼續閱讀