分享

etc/hosts.allow和/etc/hosts.deny详解

 torony 2015-12-18
/etc/hosts.allow和/etc/hosts.deny两个文件是控制远程访问设置的,通过他可以允许或者拒绝某个ip或者ip段的客户访问linux的某项服务。 比如SSH服务,我们通常只对管理员开放,那我们就可以禁用不必要的IP,而只开放管理员可能使用到的IP段

使用:
修改/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完全可以省略的。www.
当然如果管理员集中在一个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设置为准。
注意修改完后:www.111Cn.net
service xinetd restart
才能让刚才的更改生效。
/etc/hosts.allow(允许)和/etc/hosts.deny(禁止)这两个文件是tcpd服务器的配置文件
tcpd服务器可以控制外部IP对本机服务的访问
linux 系统会先检查/etc/hosts.deny规则,再检查/etc/hosts.allow规则,如果有冲突 按/etc/hosts.allow规则处理

比如:
1.禁止所有ip访问linux 的ssh功能
可以在/etc/hosts.deny添加一行 sshd:all:deny

2.禁止某一个ip(192.168.11.112)访问ssh功能
可以在/etc/hosts.deny添加一行sshd:192.168.11.112

3.如果在/etc/hosts.deny和/etc/hosts.allow同时 有sshd:192.168.11.112 规则,则192.168.11.112可以访问主机的ssh服务
总结:通过这种方法可以控制部分非授权访问,但不是一劳永逸的方法!我们在看服务日志的时候或许会看到很多扫描记录,不是还是直接针对root用户的,这时控制你的访问列表就非常有作用了!

//===============================================


对于能过xinetd程序启动的网络服务,比如ftp telnet,我们就可以修改/etc/hosts.allow和/etc/hosts.deny的配制,来许可或者拒绝哪些IP、主机、用户可以访问。

比如我们在
 /etc/hosts.allow中加入
all:218.24.129.
这样就会允许来自
218.24.129.*域的所有的客户来访问。这只是举个例子,实际上,系统默认状态 下,都是能用这些网络服的

如果我们在
 /etc/hosts.deny中加入,就限制了来自218.24.129.*域的所有的所有的IP。
all:218.24.129.

如果我们在
 /etc/hosts.deny中加入
all:218.24.129.134,这样就限制了所有在218.24.129.134中的所有的用户的访问。

当hosts.allow和 host.deny相冲突时,以hosts.allow设置优化。

设置好后,要重新启动
# /etc/rc.d/init.d/xinetd restart
# /etc/rc.d/init.d/network restart

 

不是任何服务程序都能使用TCP_wrappers的,例如使用命令ldd /usr/sbin/sshd,如果输出中有libwrap,则说明可以使用TCP_wrappers, 即该服务可以使用/etc/hosts.allow和/etc/hosts.deny,如果输出没有libwrap则不可使用

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多