[linux-21] 服务管理

一、介绍

  1. 服务(service)本质就是进程,但是是运行在后台的,通常都会监听某个端口,等待其它程序的请求,比如(mysqld,sshd防火墙等),因此我们又称为守护进程,是Linux中非常重要的知识点。
  2. service管理指令
    (1)service 服务名 [start|stop|restart|reload|status]
    (2)在CentOS7.0后,很多服务不再使用service,而是systemctl
    (3)service指令管理的服务在/etc/init.d查看 在这里插入图片描述
  3. 应用场景
    用service,查看、关闭、启动network
    tips:在虚拟系统演示,因为网络连接会关闭
    service network status
    service network stop
    service network start
  4. 查看服务名
    方式1:输入setup ,enter键选择 在这里插入图片描述
    方式2:/etc/init.d看到service指令管理的服务,部分
    ls -l /etc/init.d

二、服务运行级别(runlevel)

  1. Linux系统有7种运行级别(runlevel):常用的级别是3和5
    运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动
    运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登录
    运行级别2:多用户状态(没有NFS) ,不支持网络
    运行级别3:完全的多用户状态(有NFS),登录后进入控制台命令行模式
    运行级别4:系统未使用,保留
    运行级别5:X11控制台,登录后进入图形GUI模式
    运行级别6:系统正常关闭并重启,默认运行级别不能设为6 ,否则不能正常启动
    开机的流程说明:
    开机–>BIOS–>/boot–>systemd进程1–>运行级别–>运行级对应的服务
  2. CentOS7后运行级别说明
    在/etc/initab进行了简化,如下
    multi-user.target:analogous to runlevel 3
    graphical.target:analogous to runlevel 5 在这里插入图片描述

三、chkconfig指令

  1. 介绍
    (1)通过chkconfig 命令可以给服务的各个运行级别设置自启动/关闭
    (2)chkconfig 指令管理的服务在/etc/init.d查看
    (3)注意: Centos7.0后,很多服务使用systemctl管理
  2. chkconfig语法
    查看服务 chkconfig --list[| grep xxx]
    chkconfig 服务名 --list
    chkconfig --level 5 服务名 on/off
  3. 应用场景
    对network服务进行各种操作,把network在3运行级别,关闭自启动
    chkconfig --level 3 network off
    chkconfig --level 3 network on

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

四、服务(service)管理

systemctl管理指令

  1. 基本语法: systemctl [start | stop I restart | status] 服务名
  2. systemctl 指令管理的服务在/usr/lib/systemd/system查看
    ls -l /usr/lib/systemd/system | grep firewalld.service
    systemctl设置服务的自启动状态
  3. systemctl list-unit-files [I grep服务名] (查看服务开机启动状态,grep可以进行过滤)
  4. systemctl enable 服务名(设置服务开机启动)
  5. systemctl disable 服务名(关闭服务开机启动)
  6. systemctl is-enabled服务名(查询某个服务是否是自启动的)
    如:查看防火墙的状况,关闭防火墙和重启防火墙
    systemctl status firewalld
    systemctl list-unit-files | grep firewalld.service
    systemctl is-enabled firewalld
    systemctl disable firewalld
    systemctl enable firewalld
    打开或者关闭指定端口
    在真正的生产环境,往往需要将防火墙打开,但问题来了,如果我们把防火墙打开,那么外部请求数据包就不在真正的生产环境,往往需要将防火墙打开,但问题来了,如果我们把防火墙打开,那么外部请求数据包就不能跟服务器监听端口通讯。这时,需要打开指定的端口,如80、 22、8080等。
    firewall指令
    1.打开端口: firewall-cmd --permanent --add-port=端口号/协议
    查看端口所用协议:[root@test100 ~]# netstat -anp | more
    2.关闭端口: firewall-cmd --permanent --remove-port=端口号/协议
    3.重新载入,才能生效: firewall-cmd --reload
    4.查询端口是否开放: firewall-cmd --query-port=端口/协议

场景:
1.启用防火墙,测试111端口是否能Telnet
cmd–>telnet 192.168.41.133 111
2.开放111端口
firewall-cmd --permanent --add-port=111/tcp
firewall-cmd --reload
3.再次关闭111端口
firewall-cmd --permanent --remove-port=111/tcp
firewall-cmd --reload
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/sinat_41739591/article/details/123607044