Linux之服务管理

 

目录

一、service 管理指令

二、查看服务名

三、服务的运行级别(Runlevel)

四、linux开机的流程

五、chkconfig指令

 

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


一、service 管理指令

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

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

 

示例

  1. 查看当前防火墙的状况,关闭防火墙和重启防火墙。
//... ... 查看当前防火墙的状况  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开机的流程

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:关

chkconfig --del mysqld:删除服务mysqld

chkconfig --add mysqld:添加服务mysqld

chkconfig mysqld off:所有运行级别下关闭服务mysqld

chkconfig mysqld on:所有运行级别下开启服务mysqld

 

  • 查看单个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:关

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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值