/etc/hosts.allow和/etc/hosts.deny的作用

/etc/hosts.allow和/etc/hosts.deny的作用

http://hi.baidu.com/qaz6401877/item/8966f76c41b8ba2568105b54


[ init.d]$ ls -l /etc/hosts*
-rw-r--r--. 1 root root 554 Apr 20 14:32 /etc/hosts
-rw-r--r--. 1 root root 370 Jun  7  2013 /etc/hosts.allow
-rw-r--r--. 1 root root 460 Jun  7  2013 /etc/hosts.deny

可以通过配置hosts.allow和hosts.deny来控制访问权限。原文如下:

他们两个的关系为:/etc/hosts.allow 的设定优先于 /etc/hosts.deny

1. 当档案 /etc/hosts.allow 存在时,则先以此档案内之设定为准;
2. 而在 /etc/hosts.allow 没有规定到的事项,将在 /etc/hosts.deny 当中继续设定!
也就是说, /etc/hosts.allow 的设定优先于 /etc/hosts.deny 啰!了解了吗?基本上,只要 hosts.allow 也就够了,因为我们可以将allow 与 deny 都写在同一个档案内,只是这样一来似乎显得有点杂乱无章,因此,通常我们都是:
1. 允许进入的写在 /etc/hosts.allow 当中;
2. 不许进入的则写在 /etc/hosts.deny 当中。
再强调一次,那个 service_name 『必需』跟你的 xinetd 或者是 /etc/rc.d/init.d/* 里面的程序名称要相同。好了,我们以telnet 为例子来说明好了,现在假设一个比较安全的流程来设定,就是:
1. 只允许 140.116.44.0/255.255.255.0 与 140.116.79.0/255.255.255.0 这两个网域,及 140.116.141.99 这个主机可以进入我们的 telnet 服务器;
2. 此外,其它的 IP 全部都挡掉!
这样则首先可以设定 /etc/hosts.allow 这个档案成为: 
[root @test root]# vi /etc/hosts.allow
telnetd: 140.116.44.0/255.255.255.0 : allow
telnetd: 140.116.79.0/255.255.255.0 : allow
telnetd: 140.116.141.99 : allow 
再来,设定 /etc/hosts.deny 成为『全部都挡掉』的状态: 
[root @test root]# vi /etc/hosts.deny
telnetd: ALL : deny 
那个 ALL 代表『全部』的意思!呵呵!很棒吧!那么有没有更安全的设定,例如,当当有其它人扫瞄我的 telnet port 时,我就
将他的 IP 记住!以做为未来的查询与认证之用!那么你可以将 /etc/hosts.deny 这个档案改成这个样子: 
[root @test root]# vi /etc/hosts.deny
telnetd: ALL : spawn (echo Security notice from host `/bin/hostname`; \
echo; /usr/sbin/safe_finger @%h ) | \
/bin/mail -s "%d-%h security" root & \
: twist ( /bin/echo -e "\n\nWARNING connection not allowed. Your attempt has been logged.
\n\n\n警告您尚未允许登入,您的联机将会被纪录,并且作为以后的参考\n\n ". )
 
在上面的例子中,黄色字体字『 root 』,可以写成你的个人账号或者其它 e-mail ,以免很少以 root 身份登入 Linux 主机时,
容易造成不知道的情况,另外,最后几行,亦即 :twist 之后的那几行为同一行。如此一来,当未经允许的计算机尝试登入你的主机时,对方的屏幕上就会显示上面的最后一行,并且将他的 IP 寄到 root (或者是你自己的信箱)那里去!(注:某些没有安装 tcp_wrappers 的套件之 distribution 中,由于没有 safe_finger 等程序,所以无法执行相关的功能,这点还请多加注意呢!)

 

etc/hosts.allow和/etc/hosts.deny

这两个文件是tcpd服务器的配置文件,tcpd服务器可以控制外部IP对本机服务的访问。这两个配置文件的格式如下:

#服务进程名:主机列表:当规则匹配时可选的命令操作 server_name:hosts-list[:command] 

/etc/hosts.allow控制可以访问本机的IP地址,/etc/hosts.deny控制禁止访问本机的IP。如果两个文件的配置有冲突,以/etc/hosts.deny为准。下面是一个/etc/hosts.allow的示例:

ALL:127.0.0.1 #允许本机访问本机所有服务进程 smbd:192.168.0.0/255.255.255.0 #允许192.168.0.网段的IP访问smbd服务 

192.168.6.100代表一个主机,192.168.6.代表整个网段。同理,ringkee.com代表一台主机,.ringkee.com代表ringkee.com域内的所有主机。

ALL关键字匹配所有情况,EXCEPT匹配除了某些项之外的情况,PARANOID匹配你想控制的IP地址和它的域名不匹配时(域名伪装)的情况。KNOWN关健字匹配一台名字和地址(通过各种名字解析服务)已知的主机。LOCAL匹配任何一个不包含"."字符的主机名。我们应该尽量使用IP地址,因为主机名或域名会出现名称解析出错的问题,从而造成匹配失效。

tcpdchk程序可用于检查这两个配置文档是否有错,因为tcpd依赖许多配置文档(/etc/services,/etc/inetd.conf,/etc/hosts.allow和/etc/hosts.deny),并且要求这些文档中的相关信息必须一致。有了tcpdchk,就可帮我们自动检查。还有一个有用的工具叫tcpdmatch,它可以模拟一个进入的连接,测试tcpd的行为。

通过spawn选项,我们还可以根据匹配情况执行各种命令。如发邮件或记录日志等。下面是一个示例:

ALL:ALL : spawn (/bin/echo Security Alter from %a on %d on `date`| \ tee -a /var/log/Security_alter | mail 

    使用:

    修改/etc/hosts.allow文件

    #

    # hosts.allow This file describes the names of the hosts which are

    # allowed to use the local INET services, as decided

    # by the '/usr/sbin/tcpd' server.

    #

    sshd:210.13.218.*:allow

    sshd:222.77.15.*:allow

    以上写法表示允许210和222两个ip段连接sshd服务(这必然需要hosts.deny这个文件配合使用),当然:allow完全可以省略的。

    当然如果管理员集中在一个IP那么这样写是比较省事的

    all:218.24.129.110//他表示接受110这个ip的所有请求!

    /etc/hosts.deny文件,此文件是拒绝服务列表,文件内容如下:

    #

    # hosts.deny This file describes the names of the hosts which are

    # *not* allowed to use the local INET services, as decided

    # by the '/usr/sbin/tcpd' server.

    #

    # The portmap line is redundant, but it is left to remind you that

    # the new secure portmap uses hosts.deny and hosts.allow. In particular

    # you should know that NFS uses portmap!

    sshd:all:deny

    注意看:sshd:all:deny表示拒绝了所有sshd远程连接。:deny可以省略。

    所以:当hosts.allow和 host.deny相冲突时,以hosts.allow设置为准。

    注意修改完后:

    service xinetd restart

    才能让刚才的更改生效。


  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值