Linux服務器管理與啟停
service 命令用於對系統服務進行管理,比如啟動(start),停止(stop),重啟(restart),查看狀態(status)等。
chkconfig 用於查看、設置服務的運行級別。
ntsysv 用於直觀方便的設置各個服務是否自動啟動。
service命令本身是一個shell腳本,它在/etc/init.d/目錄查找指定的服務腳本,然後調用該服務腳本來完成任務。
二、RHEL/OEL7.X後
systemctl是新的系統服務管理器命令,該命令是用來替代service和chkconfig兩個命令的。
systemctl 是一個systemd工具,主要負責控制systemd系統和服務管理器。
說明:
啟用服務就是在當前"runlevel" 的配置文件目錄 /etc/systemd/system/multi-user.target.wants/裏,建立/usr/lib/systemd/system 裏面對應服務配置文件的軟連接;禁用服務就是刪除此軟連接。
Linux6和Linux7上任務和命令的對比
任務 | Linux6命令 | linux7命令 |
使某個服務自動啟動 | chkconfig --level 3,5 httpd on | systemctl enable httpd.service |
使某個服務不自動啟動 | chkconfig --level 3,5 httpd off | systemctl |
檢查服務狀態 | service httpd status | systemctl status httpd.service |
加入自定義服務 | chkconfig --add test | systemctl load test.service |
刪除服務 | chkconfig --del test | 停掉應用,刪除對應的配置文件 |
顯示所有已啟動的服務 | 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 |
【實戰一】在Linux6下,以防火墻服務(iptables)為例
[root@db01 ~]# service iptables stop
[root@db01 ~]# chkconfig --list|more
ip6tables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@db01 ~]# chkconfig --level 2345 iptables off
[root@db01 ~]# chkconfig --list|more
iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@db01 ~]# chkconfig --level 5 iptables on
[root@db01 ~]# chkconfig --list|more
iptables 0:off 1:off 2:off 3:off 4:off 5:on 6:off
[root@db01 ~]# chkconfig --level 5 iptables off
相關腳本在[root@db01 ~]# cd /etc/init.d/下。
【實戰二】在Linux7下,以防火墻服務(iptables)為例
[root@italex7 ~]# cd /etc/systemd/system
[root@italex7 system]# find . -name fire*
./basic.target.wants/firewalld.service
設置完成在[root@italex7 system]# cd /usr/lib/systemd/建立連接。
[root@italex7 systemd]# service iptables stop ##提示使用
Redirecting to /bin/systemctl stop iptables.service
Failed to stop iptables.service: Unit iptables.service not loaded.
[root@italex7 systemd]#
[root@italex7 systemd]# systemctl status firewalld.service
* firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2017-11-20 16:01:08 CST; 7s ago
Docs: man:firewalld(1)
Main PID: 2925 (firewalld)
CGroup: /system.slice/firewalld.service
`-2925 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
[root@italex7 ~]# systemctl stop firewalld.service
[root@italex7 ~]# systemctl status firewalld.service
* firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Mon 2017-11-20 16:03:04 CST; 2min 34s ago
Docs: man:firewalld(1)
Process: 2925 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
Main PID: 2925 (code=exited, status=0/SUCCESS)
[root@italex7 ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@italex7 ~]# 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)
Docs: man:firewalld(1)
[root@italex7 ~]# systemctl enable firewalld.service
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/basic.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.
[root@italex7 ~]# systemctl status firewalld.service
* firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
一般情況關閉firewalld,並停用
Linux服務器管理與啟停