zoukankan      html  css  js  c++  java
  • Linux学习笔记(19) Linux服务管理

    1. 服务的分类

    Linux服务可分为RPM包默认安装的服务和源码包安装的服务。前者可细分为独立的服务(直接作用于内存中)和基于xinetd服务。xinetd本身是独立的服务,其唯一的功能是管理其他服务。优点是管理的服务不占用内存,缺点是响应缓慢。

    (1) 启动和自启动

    服务启动就是在当前系统中让服务运行,并提供其功能。服务的自启动是指让服务在系统开机或重启之后,随着系统的启动而自动启动服务。

    (2) 查询已安装的服务

    RPM包安装的服务可通过chkconfig --list命令查看,其作用是查看服务自启动状态。该命令可以看到所有RPM包安装的服务,但看不到源码包安装的服务

    [root@localhost sh]# chkconfig --list
    abrt-ccpp       0:off   1:off   2:off   3:on    4:off   5:on    6:off
    abrtd           0:off   1:off   2:off   3:on    4:off   5:on    6:off
    ......

     通过查看服务安装位置(一般在/usr/local目录下),可以查看源码包安装的服务。与RPM包安装的服务的区别在于,源码包安装在指定位置,而RPM包安装在默认位置下

    2.  独立服务的管理

    RPM包安装的默认位置一般为:

    /etc/init.d/-----启动脚本位置

    /etc/sysconfig/-----初始化环境配置文件的位置

    /etc/-----配置文件的位置

    /etc/xinetd.con-----xinetd配置文件

    /etc/xinetd.d/-----基于xinetd服务的启动脚本

    /var/lib/-----服务产生的数据放在这里

    /var/log/-----日志

    (1) 独立服务的启动方法有两种:

    (1)/etc/init.d/独立服务名 start|stop|restart|status
    #查询apache的状态
    [root@localhost sh]# /etc/init.d/httpd status
    httpd (pid  1514) is running...
    
    (2)service 独立服务名 start|stop|restart|status
    #重启apache
    [root@localhost sh]# service httpd restart
    Stopping httpd: [  OK  ]
    Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
    [  OK  ]

    注:service命令是红帽专有命令,其他的unix系统中的服务操作可使用第一种方法执行。其次,service --status-all命令可以列出所有的RPM包安装服务的状态。

    (2) 独立服务的自启动

    可以通过三种方法来实现,推荐使用第二种方法。

    ① chkconfig [--level 运行级别] [独立服务名] [on|off]

    例:设置apache自启动

    [root@localhost sh]# chkconfig --list | grep httpd
    httpd           0:off   1:off   2:off   3:off   4:off   5:off   6:off
    [root@localhost sh]# chkconfig --level 2345 httpd on
    [root@localhost sh]# chkconfig --list | grep httpd
    httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off

    修改/etc/rc.local文件,开机时输入账号之前,将会检查该文件,从而实现自启动。

    例:在该文件中设置apache服务

    [root@localhost sh]# vim /etc/rc.local 
    #!/bin/sh
    
    # 每次启动时,修改该文件的时间,因此可以查询系统的启动时间
    touch /var/lock/subsys/local
    
    # apache自启动
    /etc/rc.d/init.d/httpd start

    ③ 使用ntsysv命令管理自启动(红帽专有)

    ntsysv 1.3.49.3 - (C) 2000-2001 Red Hat, Inc.
    
                   ┌──────────────────┤ Services ├──────────────────┐
                   │                                                │
                   │ What services should be automatically started? │
                   │                                                │
                   │            [*] abrt-ccpp         ↑             │
                   │            [*] abrtd             ▮             │
                   │            [*] acpid             ▒             │
                   │            [*] atd               ▒             │
                   │            [*] auditd            ▒             │
                   │            [*] autofs            ▒             │
                   │            [*] blk-availability  ▒             │
                   │            [*] certmonger        ↓             │
                   │                                                │
                   │        ┌────┐               ┌────────┐         │
                   │        │ Ok │               │ Cancel │         │
                   │        └────┘               └────────┘         │
                   │                                                │
                   │                                                │
                   └────────────────────────────────────────────────┘

    该命令的好处是不仅可以设置独立服务,也可以管理xinetd服务。

    3. 基于xinetd服务的管理

    除了不安全的telnet仍在使用xinetd服务进行管理,基于xinetd服务的使用越来越少,因此了解即可。

    (1) 安装xinetd和telnet

    [root@localhost sh]# yum -y install xinetd
    [root@localhost sh]# yum -y install telnet-server
    ...
    [root@localhost sh]# chkconfig --list
    ......
    xinetd          0:off   1:off   2:off   3:on    4:on    5:on    6:off
    
    xinetd based services:
            chargen-dgram:  off
        rsync:                  off
            telnet:                 off
            ......   

    当用户访问rsync服务时,将先访问xinetd服务,然后xinetd服务调用rsync服务,rsync服务响应xinetd服务,再由xinetd响应客户端。

    (2) xinetd服务的启动

    启动telnet。将/etc/xinetd.d/telnet 中的"didable=yes"改为"disable=no"即可,随后重启xinetd服务。查看23端口存在时,telnet服务就启动了。

    [root@localhost sh]# vim /etc/xinetd.d/telnet 
    # default: on
    # description: The telnet server serves telnet sessions; it uses 
    #       unencrypted username/password pairs for authentication.
    service telnet     #服务的名称为telnet
    {
            disable = no                   #服务不启动
            flags           = REUSE     # 标志位REUSE,设定TCP/IP socket重用    
            socket_type     = stream    #使用TCP协议数据包
            wait            = no    #允许多个连接同时连接
            user            = root    #启动服务的用户为root
            server          = /usr/sbin/in.telnetd    #服务的请程序
            log_on_failure  += USERID    #登录失败后,记录用户的ID
    }
    
    [root@localhost sh]# service xinetd restart
    Stopping xinetd: [  OK  ]
    Starting xinetd: [  OK  ]
    [root@localhost sh]# netstat -tlun | grep 23
    tcp        0      0 :::23                       :::*                        LISTEN  

    (3) xinetd服务的自启动

    注意:xinetd服务的启动和自启动是通用的,即执行了启动,意味着自启动也执行了。

    # (1) chkconfig telnet on|off
    [root@localhost sh]# chkconfig telnet off
    [root@localhost sh]# netstat -tlun | grep 23
    
    # (2) ntsysv

    4. 源码包安装服务的管理

    (1) 源码包安装服务的启动

    使用绝对路径,调用启动脚本来启动。不同的源码包的启动脚本不同。可以查看源码包的安装说明,来查看启动脚本的方法

    例:启动Apache服务的方法:

    /usr/local/apache2/bin/apachectl start|stop

    再次使用RPM包安装的apache时,将显示80端口冲突额错误。

    (2) 源码包服务的自启动

    vi /etc/rc.d/rc.local

    /usr/local/apache2/bin/apachectl start

      (3) 让源码包服务被服务管理命令识别

      让源码包的apache服务能被service命令管理启动

    ln –s /usr/local/apache2/bin/apachectl /etc/init.d/apache  

      让源码包的apache服务能被chkconfig与ntsysv命令管理自启动

    vi /etc/init.d/apache
    
      #指定httpd脚本可以被chkconfig命令管理,格式为chkconfig: 运行级别 启动顺序 关闭顺序
    
      # 不能与现有的脚本的启动顺序与关闭顺序冲突
    
      # chkconfig:35 86 76
    
      # 说明,内容随意
    
      # description: source package apache    

      然后执行:chkconfig –add apache

      注意:不建议进行这样的操作,容易混淆

     

  • 相关阅读:
    09.移动先行之谁主沉浮----控件之轮流轰炸——高级控件
    08.移动先行之谁主沉浮----控件之轮流轰炸——常用控件
    备份和还原 第三篇:master 数据库的备份和还原
    备份和还原 第二篇:数据库还原
    备份和还原 第一篇:开始数据库备份
    分页实现:Offset-Fetch
    RowVersion 用法
    TSQL 分组集(Grouping Sets)
    MongoDB 存储引擎:WiredTiger和In-Memory
    MongoDB 安全和访问权限控制
  • 原文地址:https://www.cnblogs.com/mengrennwpu/p/4732538.html
Copyright © 2011-2022 走看看