服務管理介紹
服務(Service)本質是進程,但是是運行在后臺的,通常都會監聽某個端口,等待其他程序的請求,比如(mysql、sshd、防火墻等),因此我們又稱為守護進程,是Linux中非常重要的一個知識點。
一、service管理指令
service 服務名 [start | stop |restart |reload |stauts]
注意:在CentOS7.0后,不再使用service,而是systemctl 。centos7.0是向下兼容的,也是可以用service.
示例
-
查看當前防火墻的狀況,關閉防火墻和重啟防火墻。
//... ... 查看當前防火墻的狀況 Active: inactive (dead) 說明是關閉狀態 [root@wcl ~]# service iptables status Redirecting to /bin/systemctl status iptables.service ● iptables.service - IPv4 firewall with iptables Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled) Active: inactive (dead)
//... ...那么我們來開啟防火墻 [root@wcl ~]# service iptables start Redirecting to /bin/systemctl start iptables.service //... ...開啟完防火墻,再來重新查看一下當前防火墻的狀態 Active: active (exited) :說明防火墻成功開啟 [root@wcl ~]# service iptables status Redirecting to /bin/systemctl status iptables.service ● iptables.service - IPv4 firewall with iptables Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled) Active: active (exited) since 三 2018-05-02 11:33:45 CST; 20s ago Process: 27387 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS) Main PID: 27387 (code=exited, status=0/SUCCESS) 5月 02 11:33:45 wcl systemd[1]: Starting IPv4 firewall with iptables... 5月 02 11:33:45 wcl iptables.init[27387]: iptables: Applying firewall rules: [ 確定 ] 5月 02 11:33:45 wcl systemd[1]: Started IPv4 firewall with iptables.
-
//... ... 關閉防火墻 [root@wcl ~]# service iptables stop Redirecting to /bin/systemctl stop iptables.service //... ... 查看防火墻狀態 : Active: inactive (dead):已經關閉 [root@wcl ~]# service iptables status Redirecting to /bin/systemctl status iptables.service ● iptables.service - IPv4 firewall with iptables Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled) Active: inactive (dead) since 三 2018-05-02 11:36:24 CST; 5s ago Process: 27463 ExecStop=/usr/libexec/iptables/iptables.init stop (code=exited, status=0/SUCCESS) Process: 27387 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS) Main PID: 27387 (code=exited, status=0/SUCCESS) // ... ... ... ...此處省略部分顯示內容 5月 02 11:36:24 wcl systemd[1]: Stopped IPv4 firewall with iptables. [root@wcl ~]# systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: man:firewalld(1)
備注總結:
-
service iptables status
:查看防火墻狀態 -
service iptables start
:開啟防火墻服務 -
service iptables stop
:關閉防火墻服務; - 同理,我們可以在Centos7.0中用
systemctl
指令 -
systemctl status firewalld
:查看防火墻狀態 -
systemctl start firewalld
:開啟防火墻服務 -
systemctl stop firewalld
:關閉防火墻服務; -
細節注意 :關閉或者啟動防火墻后,能夠立即生效,但這種方式只是臨時生效,當重啟服務后,還是要回歸以前的服務設置。如果希望設置某個服務自啟動或者關閉永久生效,要使用
chkconfig
指令
-
二、查看服務名
ls -l /etc/init.d/
:列出系統中有哪些服務
[root@wcl ~]# ls -l /etc/init.d/
總用量 64
-rw-r--r-- 1 root root 17500 5月 3 2017 functions
-rwxr-xr-x 1 root root 9980 4月 11 2015 jexec
-rwxr-xr-x 1 root root 10604 4月 28 17:24 mysqld
-rwxr-xr-x 1 root root 4334 5月 3 2017 netconsole
-rwxr-xr-x 1 root root 7293 5月 3 2017 network
-rw-r--r-- 1 root root 1160 3月 7 21:27 README
三、服務的運行級別(Runlevel)
vim /etc/inittab
:查看或者修改默認級別
下面我來查看一下我的linux系統中的服務運行級別
[root@wcl ~]# vim /etc/inittab
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#// 翻譯:systemd使用“目標”而不是運行級別。 默認情況下,有兩個主要目標
# multi-user.target: analogous to runlevel 3 // 翻譯:multi-user.target:類似于運行級別3
# graphical.target: analogous to runlevel 5 // 翻譯:graphical.target:類似于運行級別5
#
# To view current default target, run: //翻譯:要查看當前的默認目標,請運行
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
// 根據/etc/inittab文件內容 ,我知道了我的linux系統運行級別為3
[root@wcl ~]# systemctl get-default
multi-user.target
//詳情參見服務運行級別 [Linux] 實用指令:運行級別和找回root密碼
四、linux開機的流程
五、chkconfig指令
通過chkconfig
命令可以給每個服務的各個運行級別設置自啟動/關閉
chkconfig --list|grep xxx
:篩選查看xxx服務
示例
-
查看所有服務
[root@wcl ~]# chkconfig --list 注:該輸出結果只顯示 SysV 服務,并不包含 原生 systemd 服務。SysV 配置數據 可能被原生 systemd 配置覆蓋。 要列出 systemd 服務,請執行 'systemctl list-unit-files'。 查看在具體 target 啟用的服務請執行 'systemctl list-dependencies [target]'。 jexec 0:關 1:開 2:開 3:開 4:開 5:開 6:關 mysqld 0:關 1:關 2:開 3:開 4:開 5:開 6:關 netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關 network 0:關 1:關 2:開 3:開 4:開 5:開 6:關
-
查看單個mysqld服務,有兩種方式;
? 方式1:
chkconfig --list | grep 服務名
? 方式2:
chkconfig 服務名--list
//方式1: chkconfig --list | grep mysqld [root@wcl ~]# chkconfig --list | grep mysqld mysqld 0:關 1:關 2:開 3:開 4:開 5:開 6:關
// 方式2:chkconfig mysqld --list [root@wcl ~]# chkconfig mysqld --list mysqld 0:關 1:關 2:開 3:開 4:開 5:開 6:關
-
關閉mysqld服務運行級別5的服務
chkconfig --level 服務運行級別 服務名 on/off
:開啟關閉某服務運行級別的服務[root@wcl ~]# chkconfig --level 5 mysqld off //關閉 [root@wcl ~]# chkconfig mysqld --list //查看驗證是否成功關閉 mysqld 0:關 1:關 2:開 3:開 4:開 5:關 6:關
注意:
注:該輸出結果只顯示 SysV 服務,并不包含
原生 systemd 服務。SysV 配置數據
可能被原生 systemd 配置覆蓋。要列出 systemd 服務,請執行 'systemctl list-unit-files'。 查看在具體 target 啟用的服務請執行 'systemctl list-dependencies [target]'。
上面指令中查詢的結果出現這段內容,只因為我是在Centos7上面操作的原因。Centos7和之前的老版本差別較大。
細節注意:chkconfig重新設置服務后自啟動或關閉,需要重啟機器reboot才能生效。
$$
Practice example:練習實例
$$
查看sshd的服務運行狀態:
service sshd status
顯示當前系統中所有服務的各個運行級別的運行狀態:
chkconfig --list
將sshd服務在運行級別5下設置為不自動啟動:
chkconfig --level 5 sshd off
在所有運行級別下,關閉防火墻:
chkconfig iptables off
-
在所有運行級別下,開啟防火墻:
chkconfig iptables on
額外補充總結
chkconfig --del mysqld
:刪除服務mysqld
chkconfig --add mysqld
:添加服務mysqld
chkconfig mysqld off
:所有運行級別下關閉服務mysqld
chkconfig mysqld on
:所有運行級別下開啟服務mysqld