Notes: Stand alone daemons and Super daemon

来源:互联网 发布:淘宝商城女装卫衣套装 编辑:程序博客网 时间:2024/05/16 11:43

之前讲到,Linux系统中有一些进程,一直在后台休眠,等待一些事件将其激活。我们将这些进程成为daemons。它们之所以一直在休眠是因为它们需要为系统或者网络应用提供服务(Service)。所以,从某种意义来说,service和daemon说的是同一种东西。


这里稍微理一下,网络后台服务进程和daemons的关系。其实是一个子集的关系,凡是占用端口的进程就是网络服务进程,它们都需要一直潜伏在后台。所以它们都是daemons。但daemons不仅仅是网络服务的进程,还有像atd和cron这些提供系统服务的进程。一般而言,daemons有自己的命名规则,在ps -aux中,名字一般以d结尾。


Daemons分为Stand-alone daemons和Super daemon。前者可以直接使用etc/init.d/[daemon's name] [start|stop|restart|condrestart] 来直接启动、停止、重启、条件重启这个服务。条件重启的意思是:服务当前已经是运行的话,它可以重启这个服务,但是如果服务没有运行,condrestart是无法启动这个服务的。后者则以一个特殊的stand alone daemon作为管理者,一个称为super daemon而名字叫做xinetd的daemon。通过修改这个daemon的配置文件(/etc/xinetd.d/[name of service]),把其中的disable = no后,再通过/etc/init.d/xinetd restart,就可以启动这些由super daemon管理的service。


总体而言,Super daemon的管理如下图:




与上述两种daemons有关的文件夹如下:

1)/etc/init.d/* 启动

2) /etc/sysconfig/*  初始化的配置文件

3)  /etc/xinetd.conf 和 /etc/xinetd.d/* 前者是super daemon默认的全局设置,如果个别的daemon在后者中有设置,则会以后者为准。

4) /etc/* 是stand alone进程的配置文件。如crontab的cron.allow和cron.deny

5)/var/lib 存的是一些数据库的daemon的数据库。如mysql这个daemon就把数据库存在/var/lib/mysql下。

6) /var/run 会存下daemon的PID文件。以.pid结尾的文件可以直接用cat打开查看PID。


Stand alone daemons


前面说过启动是用/etc/init.d/*来启动,同样的,查看状态也能用/etc/init.d/* status来查看是否打开。对于,standalone的进程,也可以用service这个命令查看或者启动。语法是:

service [service name] (start/stop/restart/condrestart)service --status-all

但是,记住,对于super daemon下的进程,无法通过这样确定服务是否打开。需要通过netstat -lp | grep 'name of process'去辨认。如果无法通过名字,则通过端口号


Super Daemon管理的daemons可以启动防火墙机制:


所谓的防火墙机制无非就是限制某些IP与这些进程沟通,或者说,只限某IP段(域)的IP和这些进程沟通。


1)可以通过某个进程在/etc/xinet.d/*下的配置文件进行IP端的配置。举个例子:


# use a setup file for a daemon of super daemon to set the firewallvim /etc/xinetd.d/rsyncservice{    disable = no    bind = 127.0.0.0.1 #bind the service at this IP address    only_from = 127.0.0.0.0/8 #only let this interval of IP log in    no_access = 127.0.0.{100,200} # never let these two IP login    ......}


2) 使用/etc/host.deny and /etc/host.allow总体允许某些或者禁止某些IP地址访问


不过,这个起作用的前提是想限制的进程的依赖lib上有libwrap.so。如何查看某个进程的依赖关系?用ldd(library dependency discovery)查看即可:


ldd ${which sshd}/usr/sbin/sshd:     libwrap.so.0  => /usr/lib64/libwrap.so.0      libpam.so.0 => ...

 如上例子,sshd具有libwrap.so,所以/etc/host.allow or host.deny是对其起作用的。


这是因为host.allow和host.deny是一个叫tcpd的配置文件(tcp wrapper),所以需要对libwrap有一些才能使用。


如果allow和deny都没有对一个IP进行说明,就是所有的规则对其不适用,默认是允许访问的,所以通常做法是:


vim /etc/hosts.allowrsync: 203.71.39.0/255.255.255.0rsync: LOCALvim /etc/host.denyrsyn: All

这样就确保只有allow的IP可以和进程交流了。


使用chkconfig管理系统开机启动daemons:


这里稍微重温一下系统开机的过程(http://blog.csdn.net/firehotest/article/details/52349468):

1)BIOS检查设备响应、配置新设备(如果有),查询CMOS的设备清单,确定系统启动源;

2)如果启动源是硬盘,则找到整个硬盘的第一个扇区(boot sector),大小为512Bytes,其中装有446Bytes的MBR和64Bytes的分区表。读取MBR,取得系统启动选择程序(如果有两个系统),如果选择本系统的话,直接运行本系统的内核加载程序加载内核,选择另外的系统则交给另外一个系统分区的bootsector处理。如果只有一个系统,则直接加载内核。

3)内核调用init(PID为1)进程

4)init开始系统初始化(/etc/rc.d/rc.sysinit)

5)根据init中的chkconfig的设置进行系统daemons启动(/etc/rc.d/rc[0,6].d/*)


为什么会有0-6呢?因为Linux对应着有0-6的run level。我们的bash是run level3而X windows用的是run level 5。


了解了这些,就可以知道chkconfig的用法了:


chkconfig --list [service name]# see all the daemons which are automatically started NetworkManager 0:off 1:off 2:off ... 6:off......

那些数字对应着run level,后面代表着该run level下是否自动启动这个daemon。


chkconfig --level [0,1,2,3,4,5,6] [service name] [on/off]# set up the daemon automatically start or close in particular levelsExample:(if you did not specify --level parameter, it will think it is on for level 3 4 5 automatically)chkconfig --level 345 atd on 



0 0