Linux系统中,如果你需要禁止特定ip地址访问来保证系统的安全,只需通过操作iptalbes来实现,下面小编就给大家介绍下Linux如何禁止某个ip地址访问,感兴趣的朋友可以来了解下。

  一、概述

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

  #服务进程名:主机列表:当规则匹配时可选的命令操作

  server_name:hosts-list[:command]

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

  /etc/hosts.allow和/etc/hosts.deny两个文件是控制远程访问设置的,通过他可以允许或者拒绝某个ip或者ip段的客户访问linux的某项服务。

  比如SSH服务,我们通常只对管理员开放,那我们就可以禁用不必要的IP,而只开放管理员可能使用到的IP段。

  二、配置

  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地址和它的域名不匹配时(域名伪装)的情况。

  2、修改/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

  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:deny表示拒绝了所有sshd远程连接。:deny可以省略。

上一页123下一页共3页

  3、启动服务

  注意修改完后:

  #service xinetd restart

  才能让刚才的更改生效。

  需求:需要用hosts.deny限制用户通过ssh登录

  在/etc/hosts.deny中加入

  sshd: all

  在/etc/hosts.allow中加入

  sshd:all #拒绝所有的ip链接ssh服务

  在其他服务器上尝试链接该服务器,却发现还是正常链接

  继续找问题,又从网上得知, /etc/hosts.allow 与 /etc/hosts.deny 只对调用了 tcp_wrappers 的才起作用。若是源代码编译的,看看编译时是否寻找了 libwrap.so

  在起效果机器下,执行如下命令:

  [root@zt ~]# ldd /usr/sbin/sshd | grep libwrap.so

  libwrap.so.0 =》 /lib64/libwrap.so.0 (0x00002ba28edcc000)

  在不起效果机器下,却找不到libwrap.so

  在生效的机器上执行:

  rpm -qf /lib64/libwrap.so.0 结果如下:

  tcp_wrappers-7.6-40.7.el5

  在不生效的机器上

  yum install -y tcp_wrappers

  安装后,用ldd /usr/sbin/sshd | grep libwrap.so 还是没有内容

  在不生效机器上,继续

  yum list |grep openssh 结果:

  openssh.x86_64 5.3p2-24.el5 installed

  openssh-clients.x86_64 5.3p2-24.el5 installed

  openssh-server.x86_64 5.3p2-24.el5 installed

  openssh.x86_64 5.3p2-41.el5_5.1 updates

  openssh-askpass.x86_64 5.3p2-41.el5_5.1 updates

  openssh-clients.x86_64 5.3p2-41.el5_5.1 updates

  openssh-server.x86_64 5.3p2-41.el5_5.1 updates

  于是,执行:

  yum update -y openssh

  再次执行:

  ldd /usr/sbin/sshd | grep libwrap.so

  有结果显示了。

  别的服务器链接该服务器,也会报下面的错误

  ssh_exchange_identification: Connection closed by remote host

  另一种,也是大家常用的iptalbes来限制IP访问网站

  只允许指定的一个IP访问服务器

  vi /etc/sysconfig/iptables

  *filter

  :INPUT ACCEPT [0:0]

  :FORWARD ACCEPT [0:0]

  :OUTPUT ACCEPT [0:0]

  -A INPUT -s 165.232.121.17 -j ACCEPT

  -A INPUT -j DROP

  COMMIT 

  如果你之前的防火墙设置了永久关闭,则需要解除

  chkconfig --list 查看启动服务,找到要关闭服务名

  chkconfig --level 235 服务名 off 【在等级3和5为开机运行服务】

  系统运行级别有0—6,就在/etc/inittab中的0-6

  等级0表示:表示关机

  等级1表示:单用户模式

  等级2表示:无网络连接的多用户命令行模式

  等级3表示:有网络连接的多用户命令行模式

  等级4表示:不可用

  等级5表示:带图形界面的多用户模式

  等级6表示:重新启动2011/10/26

  ================ 以下为摘录 ====================

  又有人攻击服务器了,没有办法又的去防,这里简单介绍一种限制指定IP访问的办法。

  单个IP的命令是

  iptables -I INPUT -s 59.151.119.180 -j DROP

  封IP段的命令是

  iptables -I INPUT -s 211.1.0.0/16 -j DROP

  iptables -I INPUT -s 211.2.0.0/16 -j DROP

  iptables -I INPUT -s 211.3.0.0/16 -j DROP

  封整个段的命令是

  iptables -I INPUT -s 211.0.0.0/8 -j DROP

  封几个段的命令是

  iptables -I INPUT -s 61.37.80.0/24 -j DROP

  iptables -I INPUT -s 61.37.81.0/24 -j DROP

  服务器启动自运行

  有三个方法:

  1、把它加到/etc/rc.local中

  2、vi /etc/sysconfig/iptables可以把你当前的iptables规则放到/etc/sysconfig/iptables中,系统启动iptables时自动执行。

  3、service iptables save 也可以把你当前的iptables规则放/etc/sysconfig/iptables中,系统启动iptables时自动执行。

  后两种更好此,一般iptables服务会在network服务之前启来,更安全

  解封:

  iptables -L INPUT

  iptables -L --line-numbers 然后iptables -D INPUT 序号

  iptables 限制ip访问

  通过iptables限制9889端口的访问(只允许192.168.1.201、192.168.1.202、192.168.1.203),其他ip都禁止访问

  iptables -I INPUT -p tcp --dport 9889 -j DROP

  iptables -I INPUT -s 192.168.1.201 -p tcp --dport 9889 -j ACCEPT

  iptables -I INPUT -s 192.168.1.202 -p tcp --dport 9889 -j ACCEPT

  iptables -I INPUT -s 192.168.1.203 -p tcp --dport 9889 -j ACCEPT

  注意命令的顺序不能反了。

  上面就是Linux禁止特定ip地址访问的方法介绍了,如果你不想要哪个ip地址访问,可尝试使用本文介绍的方法。

点赞(117)

评论列表共有 0 条评论

立即
投稿
返回
顶部