Linux知识点(十三)---服务管理

1. 服务(service)管理:

1. 介绍:

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

2.service管理指令
1. service 服务名 [start|stop|restart|reload| status]
2. 在CentOS7.0后 很多服务不再使用service,而是 systemctl
3. service 指令管理的服务在/etc/init.d查看

在这里插入图片描述

service管理指令案例
请使用service 指令,查看,关闭,启动 network
注意:在虚拟系统演示,因为网络连接会关闭

[root@localhost100 ~]# ls -l /etc/init.d/
总用量 40
-rw-r--r--. 1 root root 18281 5月  22 2020 functions
-rwxr-xr-x. 1 root root  4569 5月  22 2020 netconsole
-rwxr-xr-x. 1 root root  7928 5月  22 2020 network
-rw-r--r--. 1 root root  1160 10月  2 2020 README
[root@localhost100 ~]# service network status
已配置设备:
lo ens33
当前活跃设备:
lo ens33 virbr0
[root@localhost100 ~]# service network stop
Stopping network (via systemctl):                          [  确定  ]
[root@localhost100 ~]# service network start
Starting network (via systemctl):                          [  确定  ]
[root@localhost100 ~]# 
2. 查看服务名

方式1:使用setup->系统服务 就可以看到全部
在这里插入图片描述
在这里插入图片描述

光标移动到*按空格键,自动启动改为手动启动
方式2:/etc/init.d 看到 service 指令管理的服务 Is -l /etc/init.d

[root@localhost100 ~]# ls -l /etc/init.d
lrwxrwxrwx. 1 root root 11 3月  10 12:09 /etc/init.d -> rc.d/init.d
[root@localhost100 ~]# 

2. 服务的运行级别(runlevel):

Linux系统有7种运行级别(runlevel):常用的是级别3和5
运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动
运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆
运行级别2:多用户状态(没有NFS),不支持网络
**运行级别3**:完全的多用户状态(有NFS),无界面,登陆后进入控制台命令行模式
运行级别4:系统未使用,保留
**运行级别5**:X11控制台,登陆后进入图形GUI模式
运行级别6:系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动

开机的流程说明:
在这里插入图片描述

CentOS7后运行级别说明在/etc/initab
进行了简化I如下:

multi-user.target: analogous to runlevel 3 //多用户状态有网络服务
graphical.target: analogous to runlevel 5  //图形界面

查看当前的运行级别:
To view current default target, run:

命令:systemctl get-default

设置默认的运行级别:
To set a default target, run:

命令:systemctl set-default + 级别名
例:systemctl set-default graphical.target
[root@localhost ~]# systemctl get-default
graphical.target
[root@localhost ~]# systemctl set-default multi-user.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
[root@localhost ~]# systemctl get-default
multi-user.target

3. chkconfig指令

1. 介绍
  1. 通过chkconfig命令可以给服务的各个运行级别设置自 启动/关闭
  2. chkconfig 指令管理的服务在/etc/init.d查看
  3. 注意:Centos7.0后,很多服务使用systemctl管理
2. chkconfig基本语法
1. 列出其管理的所有服务
chkconfig --list
[root@localhost100 ~]# chkconfig --list

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

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

netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
network         0:关    1:关    2:开    3:开    4:开    5:开    6:关
[root@localhost100 ~]# 
2. 查看指定服务
(1)chkconfig --list | grep xxx
[root@localhost100 ~]# chkconfig --list | grep network

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

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

network         0:关    1:关    2:开    3:开    4:开    5:开    6:关
[root@localhost100 ~]# 
(2)chkconfig 服务名 --list
[root@localhost100 ~]# chkconfig network --list

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

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

network         0:关    1:关    2:开    3:开    4:开    5:开    6:关
[root@localhost100 ~]# 
3. 给服务的各个运行级别设置自 启动/关闭
chkconfig --level 运行级别 服务名 on/off

例:把network在3运行级别,关闭自启动
在这里插入图片描述

[root@localhost100 ~]# chkconfig --level 3 network off
[root@localhost100 ~]# chkconfig network --list

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

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

network         0:关    1:关    2:开    3:关    4:开    5:开    6:关
[root@localhost100 ~]# 

在这里插入图片描述

[root@localhost100 ~]# chkconfig --level 3 network on
[root@localhost100 ~]# chkconfig network --list

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

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

network         0:关    1:关    2:开    3:开    4:开    5:开    6:关
[root@localhost100 ~]# 
4. 使用细节

chkconfig重新设置服务后自启动或关闭,需要重启机器reboot生效

4. systemct管理指令

1. 基本语法:
systemctl [start|stop|restart|status] 服务名

systemctl指令管理的服务在/usr/lib/systemd/system查看
在这里插入图片描述

2. systemctl设置服务的自启动状态
1. 查看服务开机启动状态,systemctl list-unit-files [|grep服务名] grep可以进行过滤
[root@localhost100 ~]# systemctl list-unit-files | grep fire
firewalld.service                             enabled 

[root@localhost100 ~]# systemctl list-unit-files

在这里插入图片描述

2. 设置服务开机启动:systemctl enable 服务名
3. 关闭服务开机启动:systemctl disable 服务名

注:设置服务开机启动和关闭服务开机启动默认设置运行级别3和运行级别5

4. 服务名查询某个服务是否是自启动的:systemctl is-enabled 
[root@localhost100 ~]# systemctl is-enabled firewalld
enabled
[root@localhost100 ~]# 

应用案例:
查看当前防火墙的状况,关闭防火墙和重启防火墙。
找到防火墙服务的名称:

[root@localhost100 ~]# ls -l /usr/lib/systemd/system | grep fire
-rw-r--r--. 1 root root  657 10月  1 2020 firewalld.service
[root@localhost100 ~]# 

查看当前防火墙的状况

[root@localhost100 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since 四 2022-04-21 10:13:11 CST; 7h ago
     Docs: man:firewalld(1)
 Main PID: 822 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─822 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

4月 21 10:13:09 localhost100 systemd[1]: Starting firewalld - dynamic firewall daemon...
4月 21 10:13:11 localhost100 systemd[1]: Started firewalld - dynamic firewall daemon.
4月 21 10:13:11 localhost100 firewalld[822]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure... now.
Hint: Some lines were ellipsized, use -l to show in full.

关闭防火墙

[root@localhost100 ~]# systemctl stop firewalld
[root@localhost100 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since 四 2022-04-21 17:31:52 CST; 6s ago
     Docs: man:firewalld(1)
  Process: 822 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 822 (code=exited, status=0/SUCCESS)

4月 21 10:13:09 localhost100 systemd[1]: Starting firewalld - dynamic firewall daemon...
4月 21 10:13:11 localhost100 systemd[1]: Started firewalld - dynamic firewall daemon.
4月 21 10:13:11 localhost100 firewalld[822]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure... now.
4月 21 17:31:51 localhost100 systemd[1]: Stopping firewalld - dynamic firewall daemon...
4月 21 17:31:52 localhost100 systemd[1]: Stopped firewalld - dynamic firewall daemon.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost100 ~]# 

启动防火墙

[root@localhost100 ~]# systemctl start firewalld
[root@localhost100 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since 四 2022-04-21 17:34:00 CST; 2s ago
     Docs: man:firewalld(1)
 Main PID: 12500 (firewalld)
    Tasks: 2
   CGroup: /system.slice/firewalld.service
           └─12500 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

4月 21 17:34:00 localhost100 firewalld[12500]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter...in?).
4月 21 17:34:01 localhost100 firewalld[12500]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter...in?).
4月 21 17:34:01 localhost100 firewalld[12500]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter...in?).
4月 21 17:34:01 localhost100 firewalld[12500]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter...name.
4月 21 17:34:01 localhost100 firewalld[12500]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter...name.
4月 21 17:34:01 localhost100 firewalld[12500]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter...in?).
4月 21 17:34:01 localhost100 firewalld[12500]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter...in?).
4月 21 17:34:01 localhost100 firewalld[12500]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter...in?).
4月 21 17:34:01 localhost100 firewalld[12500]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter...in?).
4月 21 17:34:01 localhost100 firewalld[12500]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter...in?).
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost100 ~]# 

在这里插入图片描述

3. 细节讨论:
  1. 或者启用防火墙后,立即生效。telnet测试 某个端口即可
[root@localhost100 ~]# netstat -anp | more

在这里插入图片描述

在这里插入图片描述

[root@localhost100 ~]# systemctl stop firewalld
[root@localhost100 ~]# 

在这里插入图片描述
在这里插入图片描述

  1. 这种方式只是临时生效(systemctl stop firewalld),当重启系统后,还是回归以前对服务的设置。
    即使采用systemctl stop firewalld这种方式关闭防火墙,但是一旦重启防火墙还会重新启动
[root@localhost100 ~]# systemctl is-enabled firewalld
enabled
[root@localhost100 ~]# 
[root@localhost100 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost100 ~]# systemctl is-enabled firewalld
disabled
[root@localhost100 ~]# 
  1. 如果希望设置某个服务自启动或关闭永久生效,要使用systemctl [enable、disable] 服务名

4. 打开或者关闭指定端口

在真正的生产环境,往往需要将防火墙打开,但问题来了,如果我们把防火墙打开,那么外部请求数据包就不能跟服务器监听端口通讯。这时,需要打开指定的端口。比如80、22、8080等

firewall指令
打开端口:firewall-cmd --permanent --add-port=端口号/协议
关闭端口:firewall-cmd --permanent --remove-port=端口号/协议
重新载入,才能生效:firewall-cmd --reload
查询端口是否开放:firewall-cmd --query-port=端口/协议

应用案例:

  1. 启用防火墙,测试 111端口是否能telnet
    在这里插入图片描述
[root@localhost100 ~]# firewall-cmd --permanent --add-port=111/tcp
success
[root@localhost100 ~]# firewall-cmd --reload
success
[root@localhost100 ~]# firewall-cmd --query-port=111/tcp
yes
[root@localhost100 ~]# 

在这里插入图片描述

在这里插入图片描述

  1. 关闭端口
[root@localhost100 ~]# firewall-cmd --permanent --remove-port=111/tcp
success
[root@localhost100 ~]# firewall-cmd --reload
success
[root@localhost100 ~]# firewall-cmd --query-port=111/tcp
no
[root@localhost100 ~]# 
lhost100 ~]# firewall-cmd --query-port=111/tcp
no
[root@localhost100 ~]# 

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EOPG

你的鼓励是我创造的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值