学海无涯
go,go,go

通过/etc/hosts.allow和/etc/hosts.deny文件控制登录的服务器

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

一、配置/etc/hosts.allow文件
书写格式(改成自自需要的IP或IP段)
ssh允许单个ip                        sshd:192.168.220.1
ssh允许ip段                          sshd:192.168.220.
telnet允许单个ip                     in.telnetd:192.168.220.1
telnet允许ip段                       in.telnetd:192.168.221.
允许这个ip的http访问                 http: 47.106.31.0/24
允许这个ip的https访问                https: 121.43.18.0/24
表示接受110这个ip的所有请求!        all:218.24.129.110
允许192.168.0.网段的IP访问smbd服务   smbd:192.168.0.0/255.255.255.0
以ssh允许192.168.220.1和telnet允许192.168.220网段为例,具体在/etc/hosts.allow加入内容如下:

编缉/etc/hosts.allow文件,配置允许的ip配置列子1
vi /etc/hosts.allow 
修改/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的所有请求!

配置列子2
/etc/hosts.allow控制可以访问本机的IP地址,/etc/hosts.deny控制禁止访问本机的IP。如果两个文件的配置有冲突,以/etc/hosts.deny为准。
1、修改/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
all:218.24.129.110 #表示接受110这个ip的所有请求!
in.telnetd:140.116.44.0/255.255.255.0
in.telnetd:140.116.79.0/255.255.255.0
in.telnetd:140.116.141.99
in.telnetd:LOCAL
smbd:192.168.0.0/255.255.255.0 #允许192.168.0.网段的IP访问smbd服务

#sendmail:192.168.1.0/255.255.255.0
#pop3d:192.168.1.0/255.255.255.0
#swat:192.168.1.0/255.255.255.0
pptpd:all EXCEPT 192.168.0.0/255.255.255.0
httpd:all
vsftpd:all
以上写法表示允许210和222两个ip段连接sshd服务(这必然需要hosts.deny这个文件配合使用),当然:allow完全可以省略的。
ALL要害字匹配所有情况,EXCEPT匹配除了某些项之外的情况,PARANOID匹配你想控制的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

in.telnet:ALL

ALL:ALL EXCEPT 192.168.0.1/255.255.255.0,192.168.1.21,\
202.10.5.0/255.255.255.0

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

3、启动服务
注意修改完后:
#service xinetd restart
才能让刚才的更改生效。


三、几个列子和注意点
hosts.allow与hosts.deny
两个文件均在/etc/目录下
优先级为先检查hosts.deny,再检查hosts.allow,后者设定可越过前者限制,

例如:
1.限制所有的ssh,
除非从218.64.87.0——127上来。
hosts.deny:
in.sshd:ALL
hosts.allow:
in.sshd:218.64.87.0/255.255.255.128

2.封掉218.64.87.0——127的telnet
hosts.deny
in.sshd:218.64.87.0/255.255.255.128

3.限制所有人的TCP连接,除非从218.64.87.0——127访问
hosts.deny
ALL:ALL
hosts.allow
ALL:218.64.87.0/255.255.255.128

4.限制218.64.87.0——127对所有服务的访问
hosts.deny
ALL:218.64.87.0/255.255.255.128

其中冒号前面是TCP daemon的服务进程名称,通常系统
进程在/etc/inetd.conf中指定,比如in.ftpd,in.telnetd,in.sshd

其中IP地址范围的写法有若干中,主要的三种是:
1.网络地址——子网掩码方式:
218.64.87.0/255.255.255.0
2.网络地址方式(我自己这样叫,呵呵)
218.64.(即以218.64打头的IP地址)
3.缩略子网掩码方式,既数一数二进制子网掩码前面有多少个“1”比如:
218.64.87.0/255.255.255.0《====》218.64.87.0/24

sshd:192.168.1.100:allow只允许ip为100的连接进来
sshd:all:deny 拒绝所有ip连接
赞(0) 打赏
未经允许不得转载:YYQ运维技术博客_运维的工作学习之路 » 通过/etc/hosts.allow和/etc/hosts.deny文件控制登录的服务器
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

运维devops

联系我们关于本博客

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏