一、RHEL/OEL7.X前
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 disable httpd.service |
检查服务状态 |
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,并停用