[Linux]服务管理:service、systemctl、chkconfig

服务管理介绍
服务(Service)本质是进程,但是是运行在后台的,通常都会监听某个端口,等待其他程序的请求,比如(mysql、sshd、防火墙等),因此我们又称为守护进程,是Linux中非常重要的一个知识点。

 

 

 service管理指令

service 服务名 [start | stop |restart |reload |stauts]

注意:在CentOS7.0后,不再使用service,而是systemctl 。centos7.0是向下兼容的,也是可以用service.

示例

查看当前防火墙的状况,关闭防火墙和重启防火墙。

1 //... ... 查看当前防火墙的状况  Active: inactive (dead)  说明是关闭状态
2 [root@wcl ~]# service iptables status
3 Redirecting to /bin/systemctl status iptables.service
4 ● iptables.service - IPv4 firewall with iptables
5 Loaded: loaded (/usr/lib/systemd/system/iptables.service; 
6 disabled; vendor preset: disabled)
7 Active: inactive (dead)  
复制代码
 1 //... ...那么我们来开启防火墙
 2 [root@wcl ~]# service iptables start
 3 Redirecting to /bin/systemctl start iptables.service
 4 //... ...开启完防火墙,再来重新查看一下当前防火墙的状态  Active: active (exited) :
 5 //说明防火墙成功开启
 6 [root@wcl ~]# service iptables status
 7 Redirecting to /bin/systemctl status iptables.service
 8 ● iptables.service - IPv4 firewall with iptables
 9 Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; 
10 vendor preset: disabled)
11 Active: active (exited) since 三 2018-05-02 11:33:45 CST; 20s ago
12 (code=exited, status=0/SUCCESS)
13 Main PID: 27387 (code=exited, status=0/SUCCESS)
14 
15 5月 02 11:33:45 wcl systemd[1]: Starting IPv4 firewall with iptables...
16 5月 02 11:33:45 wcl iptables.init[27387]: iptables: Applying firewall rules: [  确定  ]
17 5月 02 11:33:45 wcl systemd[1]: Started IPv4 firewall with iptables.
复制代码
复制代码
 1 //... ...  关闭防火墙
 2 [root@wcl ~]# service iptables stop
 3 Redirecting to /bin/systemctl stop iptables.service
 4 //... ...  查看防火墙状态  :   Active: inactive (dead):已经关闭
 5 [root@wcl ~]# service iptables status
 6 Redirecting to /bin/systemctl status iptables.service
 7 ● iptables.service - IPv4 firewall with iptables
 8  vendor preset: disabled)
 9 Active: inactive (dead) since 三 2018-05-02 11:36:24 CST; 5s ago
10 Process: 27463 ExecStop=/usr/libexec/iptables/iptables.init stop 
11    (code=exited, status=0/SUCCESS)
12 (code=exited, status=0/SUCCESS)
13 Main PID: 27387 (code=exited, status=0/SUCCESS)
14 
15 // ... ... ... ...此处省略部分显示内容
16 5月 02 11:36:24 wcl systemd[1]: Stopped IPv4 firewall with iptables.
17 [root@wcl ~]# systemctl status firewalld
18 ● firewalld.service - firewalld - dynamic firewall daemon
19 Loaded: loaded (/usr/lib/systemd/system/firewalld.service; 
20 disabled; vendor preset: enabled)
21 Active: inactive (dead)
22   Docs: man:firewalld(1)
复制代码

备注总结:

1 service iptables status:查看防火墙状态
2 service iptables start:开启防火墙服务
3 service iptables stop:关闭防火墙服务;
4 同理,我们可以在Centos7.0中用systemctl指令
5 systemctl status firewalld:查看防火墙状态
6 systemctl start firewalld:开启防火墙服务
7 systemctl stop firewalld:关闭防火墙服务;
8 细节注意 :关闭或者启动防火墙后,能够立即生效,但这种方式只是临时生效,当重启服务后,还是要回归以前的服务设置。如果希望设置某个服务自启动或者关闭永久生效,要使用chkconfig指令

查看服务名

1 ls -l /etc/init.d/:列出系统中有哪些服务
1 [root@wcl ~]# ls -l /etc/init.d/        
2 总用量 64
3 -rw-r--r-- 1 root root 17500 5月   3 2017 functions
4 -rwxr-xr-x 1 root root  9980 4月  11 2015 jexec
5 -rwxr-xr-x 1 root root 10604 4月  28 17:24 mysqld
6 -rwxr-xr-x 1 root root  4334 5月   3 2017 netconsole
7 -rwxr-xr-x 1 root root  7293 5月   3 2017 network
8 -rw-r--r-- 1 root root  1160 3月   7 21:27 README

服务的运行级别(runlevel)

1 vim /etc/inittab:查看或者修改默认级别

面我来查看一下我的linux系统中的服务运行级别

复制代码
 1 [root@wcl ~]# vim /etc/inittab
 2 # inittab is no longer used when using systemd.
 3 #
 4 # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
 5 #
 6 # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
 7 #
 8 # systemd uses 'targets' instead of runlevels. By default, there are two main targets:
 9 #// 翻译:systemd使用“目标”而不是运行级别。 默认情况下,有两个主要目标
10 # multi-user.target: analogous to runlevel 3    // 翻译:multi-user.target:类似于运行级别3
11 # graphical.target: analogous to runlevel 5     // 翻译:graphical.target:类似于运行级别5
12 #
13 # To view current default target, run:          //翻译:要查看当前的默认目标,请运行
14 # systemctl get-default
15 #
16 # To set a default target, run:
17 # systemctl set-default TARGET.target
复制代码
1 // 根据/etc/inittab文件内容 ,我知道了我的linux系统运行级别为3
2 [root@wcl ~]# systemctl get-default
3 multi-user.target     

//详情参见服务运行级别 [Linux]实用指令:运行级别和找回root密码

linux开机的流程

 

 

 

chkconfig指令

通过chkconfig命令可以给每个服务的各个运行级别设置自启动/关闭

1 chkconfig --list|grep xxx:筛选查看xxx服务

查看所有服务

复制代码
 1 [root@wcl ~]# chkconfig --list
 2 注:该输出结果只显示 SysV 服务,并不包含
 3 原生 systemd 服务。SysV 配置数据
 4 可能被原生 systemd 配置覆盖。 
 5 
 6    要列出 systemd 服务,请执行 'systemctl list-unit-files' 7    查看在具体 target 启用的服务请执行
 8    'systemctl list-dependencies [target]' 9 
10 jexec           0:关    1:开    2:开    3:开    4:开    5:开    6:关
11 mysqld          0:关    1:关    2:开    3:开    4:开    5:开    6:关
12 netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
13 network         0:关    1:关    2:开    3:开    4:开    5:开    6:关
复制代码

查看单个mysqld服务,有两种方式;

​ 方式1:chkconfig --list | grep 服务名

​ 方式2:chkconfig 服务名--list

1 //方式1: chkconfig --list | grep mysqld
2 [root@wcl ~]# chkconfig --list | grep mysqld
3 mysqld          0:关    1:关    2:开    3:开    4:开    5:开    6:关
1 // 方式2:chkconfig mysqld --list
2 [root@wcl ~]# chkconfig mysqld --list
3 mysqld          0:关    1:关    2:开    3:开    4:开    5:开    6:关

关闭mysqld服务运行级别5的服务

chkconfig --level 服务运行级别 服务名 on/off:开启关闭某服务运行级别的服务

1 [root@wcl ~]# chkconfig --level 5 mysqld off      //关闭
2 [root@wcl ~]# chkconfig mysqld --list     //查看验证是否成功关闭  
3 mysqld          0:关    1:关    2:开    3:开    4:开    5:关    6:关

注意:

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。

1   要列出 systemd 服务,请执行 'systemctl list-unit-files'2   查看在具体 target 启用的服务请执行
3   'systemctl list-dependencies [target]'

上面指令中查询的结果出现这段内容,只因为我是在Centos7上面操作的原因。Centos7和之前的老版本差别较大。

细节注意:chkconfig重新设置服务后自启动或关闭,需要重启机器reboot才能生效。

1
2
3
4
5
6
7
8
9
查看sshd的服务运行状态:service sshd status
 
显示当前系统中所有服务的各个运行级别的运行状态:chkconfig --list
 
将sshd服务在运行级别5下设置为不自动启动:chkconfig --level 5 sshd off
 
在所有运行级别下,关闭防火墙:chkconfig iptables off
 
在所有运行级别下,开启防火墙:chkconfig iptables on
复制代码
1 额外补充总结
2 
3 chkconfig --del mysqld:删除服务mysqld
4 
5 chkconfig --add mysqld:添加服务mysqld
6 
7 chkconfig mysqld off:所有运行级别下关闭服务mysqld
8 
9 chkconfig mysqld on:所有运行级别下开启服务mysqld
复制代码

 

转载于:https://blog.csdn.net/qq_28296925/article/details/80165970

  

 

posted @   温家三哥  阅读(610)  评论(0)    收藏  举报
编辑推荐:
· 日常问题排查-空闲一段时间再请求就超时
· Java虚拟机代码是如何一步一步变复杂且难以理解的?
· 领域驱动的事实与谬误 一 DDD 与 MVC
· SQL Server 2025 中的改进
· 当数据爆炸遇上SQL Server:优化策略全链路解析
阅读排行:
· Excel百万数据高性能导出方案!
· 揭秘 AI 工具的系统提示词「GitHub 热点速览」
· DeepWiki:AI驱动、免费且实用的 GitHub 源码阅读与分析神器!
· 日常问题排查-空闲一段时间再请求就超时
· 上周热点回顾(4.28-5.4)
点击右上角即可分享
微信分享提示