Chinaunix首页 | 论坛 | 博客
  • 博客访问: 158301
  • 博文数量: 126
  • 博客积分: 1073
  • 博客等级: 上尉
  • 技术积分: 1175
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-12 00:32
文章分类

全部博文(126)

文章存档

2018年(56)

2013年(1)

2012年(8)

2011年(61)

分类: LINUX

2011-01-24 21:04:19

关于linux下面系统服务的管理的讨论,

 

Linux系统服务可以分为三类:

1、由init控制的服务

2、由System V启动脚本启动的服务

3、由xinetd管理的服务

 

下面我们来介绍这三种系统服务:

一.Init控制的服务

Init的配置文件在/etc/inittab这个文件中。

 

二.System V服务

经常使用几个配置文件,大多数服务启动一个或多个进程。都是独立启动服务。

/etc/init.d/下面的脚本都是system V服务。

[root@localhost ~]# cd /etc/init.d/

[root@localhost init.d]# ls 

acpid                 functions        kudzu               nscd                    setroubleshoot

anacron             gpm               libvirtd              ntpd                    single

apmd                haldaemon     lvm2-monitor     pand                   smartd

atd                    halt                mcstrans            pcscd                 sshd

auditd               hidd               mdmonitor         portmap              syslog

autofs                hplip              mdmpd             psacct                 vmware-tools

avahi-daemon   ip6tables        messagebus       rawdevices          vncserver

avahi-dnsconfd  ipmi              microcode_ctl   rdisc                    vsftpd

bluetooth           ipmievd         multipathd         readahead_early  wdaemon

conman             iptables         named               readahead_later   winbind

cpuspeed          irda               netconsole         restorecond         wpa_supplicant

crond               irqbalance      netfs                  rhnsd                   xend

cups                 iscsi              netplugd             rpcgssd               xendomains

cups-config-daemon  iscsid   network             rpcidmapd           xfs

dnsmasq         kdump      NetworkManager   rpcsvcgssd           xinetd

dund               killall              nfs                    saslauthd               ypbind

firstboot          krb524          nfslock              sendmail               yum-updatesd

[root@localhost init.d]#

在这个里面的所有脚本都是system V的服务,

如果想运行这些服务,可以使用两种方式,

#/etc/init.d/vsftpd  restart

[root@localhost ~]#

[root@localhost ~]# /etc/init.d/vsftpd restart

Shutting down vsftpd:                                         [  OK  ]

Starting vsftpd for vsftpd:                                   [  OK  ]

[root@localhost ~]#

直接使用脚本的方式来运行,还有一种方式,

#service  vsftpd  restart

[root@localhost ~]#

[root@localhost ~]# service vsftpd restart

Shutting down vsftpd:                                        [  OK  ]

Starting vsftpd for vsftpd:                                   [  OK  ]

[root@localhost ~]#

使用service命令来调用脚本。

 

如何控制系统服务呢?

我们一般使用chkconfig工具来管理,

我们可以使用chkconfig工具来定义那个服务运行在那个级别上面。

#chkconfig  --list

这条命令会显示系统中所有的系统服务,输出很多。后面可以跟查询的服务。

# Chkconfig  --list  vsftpd

[root@localhost ~]#

[root@localhost ~]# chkconfig --list vsftpd

vsftpd          0:off   1:off   2:on    3:on    4:on    5:on    6:off

[root@localhost ~]#

通过chkconfig工具可以查询到vsftpd这个服务在下次启动的时候在2345这四个级别上面是启动的。

如何定义系统服务在下次启动的时候在那个级别启动呢?

现在我们定义vsftpd服务在下次启动的时候在35级别启动。

# chkconfig --level 35 vsftpd on

[root@localhost ~]#

[root@localhost ~]# chkconfig --level 35 vsftpd on

[root@localhost ~]#

[root@localhost ~]# chkconfig --list vsftpd

vsftpd          0:off   1:off   2:off   3:on    4:off   5:on    6:off

[root@localhost ~]#

现在我们的vsftpd服务在下次启动的时候就会在35的级别启动。

如果我们不加任何级别,直接将vsftpd服务给off

#chkconfig vsftpd off

[root@localhost ~]#

[root@localhost ~]# chkconfig vsftpd off

[root@localhost ~]#

[root@localhost ~]# chkconfig --list vsftpd

vsftpd          0:off   1:off   2:off   3:off   4:off   5:off   6:off

[root@localhost ~]#

现在在所有的级别上面,vsftpd服务就off了。

vsftpd服务直接给on呢。

#chkconfig  vsftpd  on

[root@localhost ~]#

[root@localhost ~]# chkconfig vsftpd on

[root@localhost ~]#

[root@localhost ~]# chkconfig --list vsftpd

vsftpd          0:off   1:off   2:on    3:on    4:on    5:on    6:off

[root@localhost ~]#

那么就会在2345级别上面启动。因为在016级别启动没有意义。

这条命令也是我们最长用到的。

我们可以添加服务或者删除服务在chkconfig的管理程序里面。

删除一个服务在chkconfig的管理程序里面,

#chkconig  --del  vsftpd

[root@localhost ~]#

[root@localhost ~]# chkconfig --del vsftpd

[root@localhost ~]#

[root@localhost ~]# chkconfig --list vsftpd

service vsftpd supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add vsftpd')

[root@localhost ~]#

Vsftpd这个服务就从chkconfig的管理程序里面删除了,通过chkconfig  --list命令也查询不到了。

添加一个服务到chkconfig的管理程序里面。

#chkconfig  --add  vsftpd

[root@localhost ~]#

[root@localhost ~]# chkconfig --add vsftpd

[root@localhost ~]#

[root@localhost ~]# chkconfig --list vsftpd

vsftpd          0:off   1:off   2:off   3:off   4:off   5:off   6:off

[root@localhost ~]#

OKvsftpd服务就被成功的添加到了chkconfig的管理程序里面。

注意:service vsftpd restart这条命令是使vsftpd服务在当前生效,下次启动不生效。而chkconfig工具开启的服务是在下次生效。

Chkconfig  vsftpd  on这条命令一定要敲。

 

关于/etc/sysconfig下面的文件,

/etc/sysconfig这个目录我们并不陌生了,这个目录下面的所有文件都是全局配置文件。我们很多的服务都在/etc/sysconfig下面有全局的配置文件。

 

三.Xinetd管理的服务。

我们由xinetd管理的服务都在/etc/xinetd/下面。

[root@localhost ~]#

[root@localhost ~]# cd /etc/xinetd.d/

[root@localhost xinetd.d]# ls

chargen-dgram   discard-stream  gssftp       rsync     time-stream

chargen-stream  echo-dgram      klogin       tcpmux-server

daytime-dgram   echo-stream     krb5-telnet  telnet

daytime-stream  eklogin         kshell       tftp

discard-dgram   ekrb5-telnet    rmcp         time-dgram

[root@localhost xinetd.d]#

这个目录下面的服务都是由Xinetd来管理的。

可以看到,在这个目录下面有个telnet。也就是说telnet是由xinetd来管理的。

现在我们打开这个文件来看看。

# default: on

# description: The telnet server serves telnet sessions; it uses \

#       unencrypted username/password pairs for authentication.

service telnet

{

        flags                  = REUSE

        socket_type      = stream

        wait                  = no

        user                  = root

        server               = /usr/sbin/in.telnetd

        log_on_failure  += USERID

        disable              = yes

}

telnet的配置文件里面有个disable = yes,是不是关闭这个服务呢,yes

现在我们将它改成no,不关闭。

那么我们如何重新启动这个服务呢。

使用service  telnetd  restart来试试,

[root@localhost ~]#

[root@localhost ~]# service telnetd restart

telnetd: unrecognized service

[root@localhost ~]#

好像不行,因为telnet不是system V的服务,不可能会调用/etc/init.d下面的脚本。

telnet服务是属于xinetd服务的,我们重启下xinetd服务,试试。

[root@localhost ~]#

[root@localhost ~]# service xinetd restart

Stopping xinetd:                                           [  OK  ]

Starting xinetd:                                             [  OK  ]

[root@localhost ~]#

OKxinetd服务启动成功。

因为xinetd服务也是属于system V服务,所以可以用service来启动。

我们知道telnet的端口号是23,我们来查询这个端口有没有监听。

[root@localhost ~]#

[root@localhost ~]# netstat -tulnp | grep 23

tcp        0      0 0.0.0.0:23                  0.0.0.0:*                   LISTEN      14854/xinetd       

[root@localhost ~]#

可以看到,telnet服务的确是监听在23号端口的。并且是由xinetd来管理的。

现在我们来测试一下,

[root@localhost ~]#

[root@localhost ~]# telnet 192.168.0.254

Trying 192.168.0.254...

Connected to 192.168.0.254 (192.168.0.254).

Escape character is '^]'.

Red Hat Enterprise Linux Server release 5.4 (Tikanga)

Kernel 2.6.18-164.el5xen on an i686

login: user1

Password:

Last login: Mon Mar  8 14:22:29 from 192.168.0.2

[user1@localhost ~]$ ls

[user1@localhost ~]$

同样的,我们也可以使用chkconfig来管理telnet

[root@localhost ~]#

[root@localhost ~]# chkconfig telnet on

[root@localhost ~]# chkconfig --list telnet

telnet          on

[root@localhost ~]# chkconfig telnet off

[root@localhost ~]# chkconfig --list telnet

telnet          off

[root@localhost ~]#

关于system V服务和xinetd管理的服务,

System V        占用资源,反应速度快。

Xinted            占用资源少,反应速度慢。

 

Xinetd的全局配置文件在/etc/xinetd.conf文件,

/etc/xinetd.d/service这个是xinetd服务的配置文件。

xinetd服务自己定义的为准,如果xinetd服务没有定义的条目,就会继承全局配置文件中定义的条目。

首先来看看全局配置文件,

Enabled    =   yes  or  no   

开启或者关闭,

Instances   =   50            

最多可以管理50个请求,

Per_source  =   10           

每个单一的IP仅可以访问10次,

V6only                       

是否启用ipv6

log_on_failure    =  HOST    

当访问我失败,记录主机名。

Log_on_success  =   PID HOST DURATION EXIT

当访问我成功了,就记录这些信息。

Cps  =50 10   

当连接到我的计算机上面的用户超过50,就暂停10S

在来看看xinetd服务的配置文件,

Service   telnet  

服务的名字

Disable  =  yes  or   no 

是否开启telnet

Socket_type   =  stream

类型是TCP

Wait    =  no

是否等待

User    =  root

服务的管理者是root

Log_on_failure  +=  USERID

登录失败,记录用户的uid

 

下面就是关于xinetd服务的访问控制,

语法:

使用only_from 来定义允许访问,

使用no_access来定义禁止访问。

Only_from代表仅允许那些人来访问,

No_access代表不允许那些人访问。

 

关于xinetd服务访问控制的主机模式

192.168.0.10             主机

192.168.0.0/24          网段

Station10.example.com.   主机名

.example.com           域名

现在我们来做试验,

[root@localhost ~]#

[root@localhost ~]# ssh 192.168.0.10

root@192.168.0.10's password:

Permission denied, please try again.

root@192.168.0.10's password:

Last login: Mon Mar  8 15:32:07 2010 from 192.168.0.254

[root@localhost ~]#

[root@localhost ~]# telnet 192.168.0.254

Trying 192.168.0.254...

Connected to 192.168.0.254 (192.168.0.254).

Escape character is '^]'.

Red Hat Enterprise Linux Server release 5.4 (Tikanga)

Kernel 2.6.18-164.el5xen on an i686

login: user1

Password:

Last login: Mon Mar  8 15:21:50 from 192.168.0.10

[user1@localhost ~]$

现在我们是可以telnet192.168.0.254上面的。

现在我们对telnet来做访问控制,

[root@localhost ~]#

[root@localhost ~]# cat /etc/xinetd.d/telnet

# default: on

# description: The telnet server serves telnet sessions; it uses \

#       unencrypted username/password pairs for authentication.

service telnet

{

        disable               = no

        flags                  = REUSE

        socket_type      = stream       

        wait                  = no

        user                  = root

        server               = /usr/sbin/in.telnetd

        log_on_failure  += USERID

        no_access         =  192.168.0.10 

}

[root@localhost ~]#

我们拒绝192.168.0.10进行telnet192.168.0.254

然后重启下xinetd服务,

[root@localhost ~]#

[root@localhost ~]# service xinetd restart

Stopping xinetd:                                          [  OK  ]

Starting xinetd:                                            [  OK  ]

服务启动成功,测试下,

[root@localhost ~]#

[root@localhost ~]# ssh 192.168.0.10

root@192.168.0.10's password:

Last login: Mon Mar  8 15:49:20 2010 from 192.168.0.254

[root@localhost ~]#

[root@localhost ~]# telnet 192.168.0.254

Trying 192.168.0.254...

Connected to 192.168.0.254 (192.168.0.254).

Escape character is '^]'.

Connection closed by foreign host.

[root@localhost ~]#

OK,可以看到,连接就被拒绝了。

192.168.0.20可以telnet192.168.0.254上面吗。试试

[root@localhost ~]#

[root@localhost ~]# ssh 192.168.0.20

root@192.168.0.20's password:

Last login: Mon Mar  8 15:58:27 2010 from 192.168.0.254

[root@localhost ~]#

[root@localhost ~]# telnet 192.168.0.254

Trying 192.168.0.254...

Connected to 192.168.0.254 (192.168.0.254).

Escape character is '^]'.

Red Hat Enterprise Linux Server release 5.4 (Tikanga)

Kernel 2.6.18-164.el5xen on an i686

login: user1

Password:

Last login: Mon Mar  8 15:32:35 from 192.168.0.10

[user1@localhost ~]$

是可以的,我们只是拒绝了192.168.0.10进行telnet192.168.0.254

所以192.168.0.20是可以的。

如果有这样的情况呢?

Only_from     192.168.0.0/24

No_access     192.168.0.20

那么192.168.0.20最终可以访问吗?

我们来尝试一下。

[root@localhost ~]#

[root@localhost ~]# cat /etc/xinetd.d/telnet

# default: on

# description: The telnet server serves telnet sessions; it uses \

#       unencrypted username/password pairs for authentication.

service telnet

{

        disable               = no

        flags                   = REUSE

        socket_type       = stream       

        wait                   = no

        user                   = root

        server                = /usr/sbin/in.telnetd

        log_on_failure   += USERID

        only_from         = 192.168.0.0/24

        no_access         =  192.168.0.20 

}

[root@localhost ~]#

配置OK,重启下服务,

[root@localhost ~]#

[root@localhost ~]# service xinetd restart

Stopping xinetd:                                          [  OK  ]

Starting xinetd:                                            [  OK  ]

服务启动成功,测试下,

[root@localhost ~]#

[root@localhost ~]# ssh 192.168.0.20

root@192.168.0.20's password:

Last login: Mon Mar  8 16:03:23 2010 from 192.168.0.254

[root@localhost ~]# telnet 192.168.0.254

Trying 192.168.0.254...

Connected to 192.168.0.254 (192.168.0.254).

Escape character is '^]'.

Connection closed by foreign host.

[root@localhost ~]#

可以看到,192.168.0.20最终还是不可以telnet192.168.0.254上面去的。

那么192.168.0.10可以telnet吗?

[root@localhost ~]#

[root@localhost ~]# ssh 192.168.0.10

root@192.168.0.10's password:

Last login: Mon Mar  8 16:05:00 2010 from 192.168.0.254

[root@localhost ~]#

[root@localhost ~]# telnet 192.168.0.254

Trying 192.168.0.254...

Connected to 192.168.0.254 (192.168.0.254).

Escape character is '^]'.

Red Hat Enterprise Linux Server release 5.4 (Tikanga)

Kernel 2.6.18-164.el5xen on an i686

login: user1

Password:

Last login: Mon Mar  8 16:00:42 from 192.168.0.20

[user1@localhost ~]$

OK192.168.0.10是没有问题的,可以telnet192.168.0.254上面去。

我们允许192.168.0.0/24这个网段里面所有的主机,但是拒绝192.168.0.10这台主机。

这个访问控制还可以支持访问时间的控制。

Access_times  = 8:00-12:00

[root@localhost ~]#

[root@localhost ~]# cat /etc/xinetd.d/telnet

# default: on

# description: The telnet server serves telnet sessions; it uses \

#       unencrypted username/password pairs for authentication.

service telnet

{

        disable               = no

        flags                   = REUSE

        socket_type       = stream       

        wait                   = no

        user                   = root

        server                = /usr/sbin/in.telnetd

        log_on_failure   += USERID

        access_times     = 8:00-12:00

}

[root@localhost ~]#

配置就OK,重启下服务,

[root@localhost ~]#

[root@localhost ~]# service xinetd restart

Stopping xinetd:                                          [  OK  ]

Starting xinetd:                                            [  OK  ]

服务启动成功,测试下,

[root@localhost ~]#

[root@localhost ~]# ssh 192.168.0.10

root@192.168.0.10's password:

Last login: Mon Mar  8 16:12:00 2010 from 192.168.0.254

[root@localhost ~]# date

Mon Mar  8 16:12:20 CST 2010

[root@localhost ~]#

[root@localhost ~]# telnet 192.168.0.254

Trying 192.168.0.254...

Connected to 192.168.0.254 (192.168.0.254).

Escape character is '^]'.

Connection closed by foreign host.

[root@localhost ~]#

192.168.0.10是不可以telnet192.168.0.254上面去的,因为访问时间限制了。

 

linux下面的系统服务的讨论就是这么多了。

阅读(713) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~