鸟哥的linux私房菜学习笔记《三十七》deamon与服务

  1. 什么是deamon与服务
    简单的说,系统为了某些功能必须要提供一些服务 (不论是系统本身还是网络方面),这个服务就称为 service 。 但是 service 的提供总是需要程序的运行吧!否则如何执行呢?所以达成这个 service 的程序我们就称呼他为 daemon 啰! 举例来说,达成循环型例行性工作调度服务 (service) 的程序为 crond 这个 daemon 。
  2. daemon 的主要分类
    (1)服务的启动、关闭与观察等方式: 所有的服务启动脚本通通放置于 /etc/init.d/ 下面,基本上都是使用 bash shell script 所写成的脚本程序,需要启动、关闭、重新启动、观察状态时, 可以通过如下的方式来处理:
    启动:/etc/init.d/daemon start
    关闭:/etc/init.d/daemon stop
    重新启动:/etc/init.d/daemon restart
    状态观察:/etc/init.d/daemon status
    (2)服务启动
    1 . 独立启动模式 (stand alone):服务独立启动,该服务直接常驻于内存中,提供本机或用户的服务行为,反应速度快。
    2 . 总管程序 (super daemon):由特殊的 xinetd 或 inetd 这两个总管程序提供 socket对应或 port 对应的管理。当没有用户要求某 socket 或 port 时, 所需要的服务是不会被启动的。若有用户要求时, xinetd 总管才会去唤醒相对应的服务程序。当该要求结束时,这个服务也会被结束掉~ 因为通过 xinetd 所总管,因此这个家伙就被称为 super daemon。好处是可以通过 super daemon 来进行服务的时程、连线需求等的控制,缺点是唤醒服务需要一点时间的延迟。
    (3)制定执行等级默认要启动的服务: 若要创建如上提到的SXXdaemon 的话,不需要管理员手动创建链接文件, 通过如下的指令可以来处理默认启动、默认不启动、观察默认启动否的行为:
    默认要启动: chkconfig daemon on
    默认不启动: chkconfig daemon off
    观察默认为启动否: chkconfig –list daemon
    (4)执行等级的切换行为: 当你要从纯命令行 (runlevel 3) 切换到图形界面 (runlevel5), 不需要手动启动、关闭该执行等级的相关服务,只要“ init 5 ”即可切换,init 这个程序会主动去分析 /etc/rc.d/rc[35].d/ 这两个目录内的脚本, 然后启动转换 runlevel 中需要的服务~就完成整体的 runlevel 切换。
    (5)deamon工作形态的类型
    1 . signal-contrl:通过信号管理,被动
    2 . interval-contrl:每隔一段时间就主动去执行某项工作
    (6)deamon的命令规则:通常创建服务名后,在名称后加上一个d
  3. /etc/services:服务与端口的对应

    
    # 查看服务与端口的对应
    
    [root@CentOS ~]# cat /etc/services 
    daytime         13/udp
    qotd            17/tcp          quote
    qotd            17/udp          quote
    msp             18/tcp                          # message send protocol
    msp             18/udp                          # message send protocol
    chargen         19/tcp          ttytst source
    chargen         19/udp          ttytst source
    ftp-data        20/tcp
    ftp-data        20/udp
    
    # 21 is registered to ftp, but also used by fsp
    
    ftp             21/tcp
    ftp             21/udp          fsp fspd
    ssh             22/tcp                          # The Secure Shell (SSH) Protocol
    ssh             22/udp                          # The Secure Shell (SSH) Protocol
    telnet          23/tcp
    telnet          23/udp
    
    # 24 - private mail system
    
    lmtp            24/tcp                          # LMTP Mail Delivery
    lmtp            24/udp                          # LMTP Mail Delivery
    
  4. deamon的启动脚本与启动方式
    (1)为了管理方便,通常发行版都会记录一个deamon启动后取得进程的PID并放在/var/run这个目录下。
    重要的配置文件防止目录:
    /etc/init.d/*:启动脚本放置处
    /etc/sysconfig/*:各服务的初始化环境配置文件
    /etc/xinitd.conf,,,/etc/xintd.d/*:配置文件
    /erc/*:各服务各自的配置文件
    /etc/lib/*:各服务产生的数据库
    /ver/run/*:各服务的程序的PID记录处
    (2)Stand alone(独立守护进程)
    配置文件:/etc/init.d/*

    
    # 查看信息sshd
    
    [root@CentOS ~]# /etc/init.d/sshd 
    
    # 查看sshd这个deamon的状态
    
    [root@CentOS ~]# /etc/init.d/sshd status
    openssh-daemon (pid  2095) is running...
    
    # 重新读取配置文件
    
    [root@CentOS ~]# /etc/init.d/sshd restart
    Stopping sshd:                                             [  OK  ]
    Starting sshd:                                             [  OK  ]
    [root@CentOS ~]# /etc/init.d/sshd status
    openssh-daemon (pid  6517) is running...
    

    script脚本方式启动或者关闭服务

    
    # 重新启动crond这个desmon
    
    [root@CentOS ~]# service crond restart
    Stopping crond:                                            [  OK  ]
    Starting crond:                                            [  OK  ]
    
    # 将系统的所以服务的运行状态列出来:
    
    [root@CentOS ~]# service --status-all
    

    启动以service或者直接到/etc/init.d/下面启动一样。
    详细解释说明查看/sbin/service
    (3)super deamon(超级守护进程)
    常驻内存
    必须要在配置文件设置为启动该deamon才行
    配置文件:/etc/xinit.d/*

    
    # 查看super deamon是否启动:
    
    [root@CentOS ~]# /etc/xinetd.d/*
    bash: /etc/xinetd.d/amanda: Permission denied
    [root@CentOS ~]# grep -i 'disable' /etc/xinetd.d/*
    /etc/xinetd.d/amanda:   disable         = yes
    /etc/xinetd.d/chargen-dgram:    disable     = yes
    /etc/xinetd.d/chargen-stream:   disable     = yes
    
    # disable=yes代表取消启动
    
    
    # disable-no代表启动该服务
    
    
    
    # 修改配置文件成为启动的模样
    
    [root@CentOS ~]# vim /etc/xinetd.d/rsync 
    service rsync
    {
            disable = no 
            flags           = IPv6
    
    
    # 重启xinetd这个服务
    
    [root@CentOS ~]# service xinetd restart
    Stopping xinetd:                                           [  OK  ]
    Starting xinetd:                                           [  OK  ]
    
    
    # 查看启动的端口
    
    [root@CentOS ~]# grep 'rsync' /etc/services 
    rsync           873/tcp                         # rsync
    rsync           873/udp                         # rsync
    airsync         2175/tcp                # Microsoft Desktop AirSync Protocol
    airsync         2175/udp                # Microsoft Desktop AirSync Protocol
    
    
    # 查看启动的是哪个服务:
    
    [root@CentOS ~]# netstat -tnlp | grep 873
    tcp        0      0 :::873                      :::*                        LISTEN      7493/xinetd  
    
    # 得知启动的是xinitd,因为它控制rsync
    
    
  5. 解析super deamon的配置文件,xinetd.conf
    默认的配置文件:xinetd.conf

    
    # 查看一下内容:
    
    [root@CentOS ~]# vim /etc/xinetd.conf 
            cps             = 50 10
            instances       = 50
            per_source      = 10
    
    
    # Address and networking defaults
    
            v6only          = no
     v6only          = no
    
    
    # setup environmental attributes
    
            groups          = yes
            umask           = 002
    }
    
    includedir /etc/xinetd.d
    
    # 上句话表示所有的服务参数文件都被包含在/etc/xinetd.d里面
    
    

    attribute是一些xinetd的管理参数,assign op则是参数的设置方法
    assign op的主要设置形式为:
    =:表示后卖弄的设置参数就是这样
    +=:表示后卖弄的设置为原来的设置里面加入新的参数
    -=:表示后面的参数设置为在原来的参数中舍弃这里输入的参数

  6. rsync设置,内外网映射
    rsync服务:可以进行远程映射。
    rsync可以让两个主机上面的某个目录一模一样,在远程异地备份系统上面挺好用

    [root@CentOS ~]# vim /etc/xinetd.d/rsync 
    service rsync
    {
            disable = no
            flags           = IPv6
            socket_type     = stream
            wait            = no
            user            = root
            server          = /usr/bin/rsync
            server_args     = --daemon
            log_on_failure  += USERID
    }

    由于/etc/services当中规定rsync使用端口号码873,这个端口小于1024,所以理论只能root身份启动这个端口。
    假设要将192.168.1.100设计为对外域,127.0.0.1位内网,且内外网的权限分别为:
    (1)对内网开放较多权限的部分
    这里的设置需要绑定在127.0.0.1这个接口上
    对127.0.0.0/8开放登录权限
    不进行任何连接的限制,包括总连接数量与时间
    但是127.0.0.100及127.0.0.200不允许登录rsync服务
    (2)对外部192.168.1.100网较多限制的设置
    对外设置绑定192.168.1.100这个接口
    这个接口仅开放140.116.0.0/16这个B等级的网段及.edu.cn网段可以登录
    开放的时间为早上1:00~9:00点以及晚上20:00~24:00两个时段
    最多允许10条同时连接的限制

    [root@CentOS ~]# vim /etc/xinetd.d/rsync 
    
    # 先对内网设置
    
    service rsync
    {
            disable = no
            bind            = 127.0.0.1
            only_from       = 127.0.0.0/8
            no_access       = 127.0.0.{100,200}
            instances       = UNLIMITED
            socket_type     = stream
            wait            = no
            user            = root
            server          = /usr/bin/rsync
            server_args     = --daemon
            log_on_failure  += USERID
    }
    
    # 再设置外网
    
    service rsync
    {
            disable = no
            bind            = 192.168.1.100
            only_from       = 140.116.0.0/16
            no_access      += .edu.tw
            access_times    = 01:00-9:00 20:00-23:59
            instances       = 10
            socket_type     = stream
            wait            = no
            user            = root
            server          = /usr/bin/rsync
            server_args     = --daemon
            log_on_failure  += USERID
    }
    
    
    
    # 设置完毕,查看原本873状态
    
    [root@CentOS ~]# netstat -tnlp | grep 873
    tcp        0      0 :::873                      :::*                        LISTEN      7493/xinetd        
    
    # 重启服务
    
    [root@CentOS ~]# /etc/init.d/xinetd restart
    Stopping xinetd:                                           [  OK  ]
    Starting xinetd:                                           [  OK  ]
    [root@CentOS ~]# netstat -tnlp | grep 873
    
  7. 服务的防火墙管理,/etc/hosts.{allow | deny}
    管理某些进程能否接受或者拒绝来自因特网的连接:/etc/hosts.{allow | deny}
    任何以xinetd管理的服务都可以通过/etc/hosts.allow,/etc/hosts.deny来设置防火墙
    修改/etc/xinetd.d/rsync里面no_access,,only_from也可以进行防火墙设置
    其实,/etc/hosts.allow与/etc/hosts.deny也是/usr/sbin/tcpd的配置文件,而这个/usr/sbin/tcpd则是用阿里分析进入系统的TCP网络数据包的一个软件,我们主要使用TCP Wrappers来控管:
    1 . 源IP或与整个域的IP网段
    2 . port

    
    # 测试一下sshd及httpd这两个程序有无TCP Wrappers的功能
    
    [root@CentOS ~]# ldd $( which sshd httpd )
    
    # 查询结果sshd支持但是httpd不支持
    
    
    
    
    # 配置文件语法:
    
    服务  IP或主机名  操作
    [root@CentOS ~]# vim /etc/hosts.deny
    rsync : 127.0.0.100 127.0.0.200 :deny
    
    # 或者:
    
    [root@CentOS ~]# vim /etc/hosts.deny 
    rsync : 127.0.0.100 :deny
    rsync : 127.0.0.200 :deny
    
    # 设置只允许140.116.0.0/16与203.71.39.0/24这两个域,及203.71.38.123这个主机可以进入我们的rsync服务器
    
    [root@CentOS ~]# vim /etc/hosts.allow
    rsync : 140.116.0.0/14 
    rsync : 203.71.39.0/24 
    rsync : 203.71.38.123
    rsync : LOCAL
    [root@CentOS ~]# vim /etc/hosts.deny
    raync : ALL
  8. 查看系统开启服务

    
    # 查看系统开启的所有服务
    
    [root@CentOS ~]# netstat -tulp
    
    # 找出所有有监听网络的服务
    
    [root@CentOS ~]# netstat -lnp
    
    # 查看所有的服务状态
    
    [root@CentOS ~]# service --status-all
  9. linux主机的开机流程
    netstat仅能查到自己目前已经启动的deamon,使用service这个命令或者是/etc/init.d/* start的方法仅能在目前环境下立即启动某个服务
    linux主机的开机流程:
    1 . 打开电源,读取BIOS并进行自检
    2 . 通过BIOS取德第一个可开机设备,读取MBR取得启动装载程序
    3 . 取得内核并加载到内存,且检测系统硬件
    4 . 内核主动调取init进程
    5 . init进程开始执行初始化(/etc/rc.d/rc.sysinit)
    6 . 依据init的设置进行deamonstart(/etc/re.c/rc[0-6].d/*)
    7 . 加载本机设置(/etc/rc.d/rc.local)
  10. chkconfig:管理系统服务默认开机启动与否
    参数:
    –list:仅将目前的各项服务状态栏显示出来
    –level:设置某服务在该level下启动或关闭

    
    # 列出目前系统上面所有被chkconfig管理的服务
    
    [root@CentOS ~]# chkconfig --list | more
    
    # 显示目前在run level 3为启动的服务
    
    [root@CentOS ~]# chkconfig --list | grep '3:on'
    
    # 让atd这个服务在run level为3,4,5时启动
    
    [root@CentOS ~]# chkconfig --level 345 atd on
    
    # chkconfig仅是设置开机时默认启动的服务而已,该服务现在的状态如何是不知道的
    
    
    
    
    # 先查看httpd,再查看默认有无启动,之后chkconfig设置为默认启动
    
    [root@CentOS ~]# /etc/init.d/httpd status
    httpd is stopped
    [root@CentOS ~]# chkconfig --list httpd
    httpd           0:off   1:off   2:off   3:off   4:off   5:off   6:off
    
    # 默认并未启动
    
    
    [root@CentOS ~]# chkconfig httpd on ; chkconfig --list httpd
    httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off
    [root@CentOS ~]# /etc/init.d/httpd status
    httpd is stopped
    
    # 仅是设置开机启动而已,并未使用/etc/init.d/httpd start
    
    
    # 查看rsync是否启动,若要将其关闭怎么处理:
    
    [root@CentOS ~]# /etc/init.d/rsyslog status
    rsyslogd (pid  1655) is running...
    [root@CentOS ~]# netstat -tulp | grep rsync
    [root@CentOS ~]# chkconfig --list rsync
    rsync           on
    [root@CentOS ~]# chkconfig rsync off ; chkconfig --list rsync
    rsync           off
    [root@CentOS ~]# /etc/init.d/xinetd restart ; metstat -tulp | grep rsync
    Stopping xinetd:                                           [  OK  ]
    Starting xinetd:                                           [  OK  ]
    bash: metstat: command not found
    
  11. ntsysv:类图形界面管理模式
    参数:
    –level:后面接不同的run level,例如netsysv –level35
    这里写图片描述
    括号内出现星号代表开机启动
  12. chkconfig:设置自己的系统服务
    参数:
    –add:增加一个服务名称给chkconfig来管理,该服务名称必须在/etc/init.d内
    –del:删除一个给chkconfig管理的服务
    想要自己写一个程序并且想要让进程成为系统服务haorangchkconfig来管理,只要将服务加入init可以管理的script当中,即/etc/init.d/当中。

    
    # 比如我们在/etc/init.d/里面新建一个myvvv文件,该文件仅是一个简单的服务范例,基本没什么用,设置为:myvvv将在run level 3及5启动,,myvvv在/etc/rc.d/rc[35].d当中启动,以80顺序启动,以70顺序结束
    
    [root@CentOS ~]# vim /etc/init.d/myvvv
    
    #!bin/bash
    
    
    # chkconfig: 35 80 70
    
    
    # description:没什么,测试而已。
    
    echo nothing
    
    [root@CentOS ~]# chkconfig --list myvvv
    service myvvv supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add myvvv')
    [root@CentOS ~]# chkconfig --add myvvv ; chkconfig --list myvvv
    myvvv           0:off   1:off   2:off   3:on    4:off   5:on    6:off
    [root@CentOS ~]# chkconfig --del myvvv
    [root@CentOS ~]# rm /etc/init.d/myvvv 
    
  13. 默认开机启动的服务
    使用ntsysv或者vim /etc/init.d/*查看
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值