国产宅男网站在线|亚洲A级性爱免费视频|亚洲中精品级在线|午夜福利AA毛

  • <dd id="gf5jf"><th id="gf5jf"></th></dd>

    <cite id="gf5jf"><label id="gf5jf"></label></cite>
  • <div id="gf5jf"><listing id="gf5jf"></listing></div>
    學(xué)習(xí)啦 > 學(xué)習(xí)電腦 > 操作系統(tǒng) > Linux教程 > Linux禁止某個(gè)IP地址訪問的方法

    Linux禁止某個(gè)IP地址訪問的方法

    時(shí)間: 志藝942 分享

    Linux禁止某個(gè)IP地址訪問的方法

      我們要實(shí)現(xiàn)Linux 禁止某個(gè)IP地址訪問其實(shí)非常的簡(jiǎn)單,最常用的辦法就是使用iptalbes來(lái)操作,接下來(lái)是小編為大家收集的Linux禁止某個(gè)IP地址訪問的方法,歡迎大家閱讀:

      Linux禁止某個(gè)IP地址訪問的方法

      一、概述

      這兩個(gè)文件是tcpd服務(wù)器的配置文件,tcpd服務(wù)器可以控制外部IP對(duì)本機(jī)服務(wù)的訪問。這兩個(gè)配置文件的格式如下:

      #服務(wù)進(jìn)程名:主機(jī)列表:當(dāng)規(guī)則匹配時(shí)可選的命令操作

      server_name:hosts-list[:command]

      /etc/hosts.allow控制可以訪問本機(jī)的IP地址,/etc/hosts.deny控制禁止訪問本機(jī)的IP。如果兩個(gè)文件的配置有沖突,以/etc/hosts.deny為準(zhǔn)。

      /etc/hosts.allow和/etc/hosts.deny兩個(gè)文件是控制遠(yuǎn)程訪問設(shè)置的,通過(guò)他可以允許或者拒絕某個(gè)ip或者ip段的客戶訪問linux的某項(xiàng)服務(wù)。

      比如SSH服務(wù),我們通常只對(duì)管理員開放,那我們就可以禁用不必要的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這個(gè)ip的所有請(qǐng)求!

      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.網(wǎng)段的IP訪問smbd服務(wù)

      #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兩個(gè)ip段連接sshd服務(wù)(這必然需要hosts.deny這個(gè)文件配合使用),當(dāng)然:allow完全可以省略的。

      ALL要害字匹配所有情況,EXCEPT匹配除了某些項(xiàng)之外的情況,PARANOID匹配你想控制的IP地址和它的域名不匹配時(shí)(域名偽裝)的情況。

      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遠(yuǎn)程連接。:deny可以省略。

      3、啟動(dòng)服務(wù)

      注意修改完后:

      #service xinetd restart

      才能讓剛才的更改生效。

      需求:需要用hosts.deny限制用戶通過(guò)ssh登錄

      在/etc/hosts.deny中加入

      sshd: all

      在/etc/hosts.allow中加入

      sshd:all #拒絕所有的ip鏈接ssh服務(wù)

      在其他服務(wù)器上嘗試鏈接該服務(wù)器,卻發(fā)現(xiàn)還是正常鏈接

      繼續(xù)找問題,又從網(wǎng)上得知, /etc/hosts.allow 與 /etc/hosts.deny 只對(duì)調(diào)用了 tcp_wrappers 的才起作用。若是源代碼編譯的,看看編譯時(shí)是否尋找了 libwrap.so

      在起效果機(jī)器下,執(zhí)行如下命令:

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

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

      在不起效果機(jī)器下,卻找不到libwrap.so

      在生效的機(jī)器上執(zhí)行:

      rpm -qf /lib64/libwrap.so.0 結(jié)果如下:

      tcp_wrappers-7.6-40.7.el5

      在不生效的機(jī)器上

      yum install -y tcp_wrappers

      安裝后,用ldd /usr/sbin/sshd | grep libwrap.so 還是沒有內(nèi)容

      在不生效機(jī)器上,繼續(xù)

      yum list |grep openssh 結(jié)果:

      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

      于是,執(zhí)行:

      yum update -y openssh

      再次執(zhí)行:

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

      有結(jié)果顯示了。

      別的服務(wù)器鏈接該服務(wù)器,也會(huì)報(bào)下面的錯(cuò)誤

      ssh_exchange_identification: Connection closed by remote host

      另一種,也是大家常用的iptalbes來(lái)限制IP訪問網(wǎng)站

      只允許指定的一個(gè)IP訪問服務(wù)器

      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

      如果你之前的防火墻設(shè)置了永久關(guān)閉,則需要解除

      chkconfig --list 查看啟動(dòng)服務(wù),找到要關(guān)閉服務(wù)名

      chkconfig --level 235 服務(wù)名 off 【在等級(jí)3和5為開機(jī)運(yùn)行服務(wù)】

      系統(tǒng)運(yùn)行級(jí)別有0—6,就在/etc/inittab中的0-6

      等級(jí)0表示:表示關(guān)機(jī)

      等級(jí)1表示:?jiǎn)斡脩裟J?/p>

      等級(jí)2表示:無(wú)網(wǎng)絡(luò)連接的多用戶命令行模式

      等級(jí)3表示:有網(wǎng)絡(luò)連接的多用戶命令行模式

      等級(jí)4表示:不可用

      等級(jí)5表示:帶圖形界面的多用戶模式

      等級(jí)6表示:重新啟動(dòng)2011/10/26

      ================ 以下為摘錄 ====================

      又有人攻擊服務(wù)器了,沒有辦法又的去防,這里簡(jiǎn)單介紹一種限制指定IP訪問的辦法。

      單個(gè)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

      封整個(gè)段的命令是

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

      封幾個(gè)段的命令是

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

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

      服務(wù)器啟動(dòng)自運(yùn)行

      有三個(gè)方法:

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

      2、vi /etc/sysconfig/iptables可以把你當(dāng)前的iptables規(guī)則放到/etc/sysconfig/iptables中,系統(tǒng)啟動(dòng)iptables時(shí)自動(dòng)執(zhí)行。

      3、service iptables save 也可以把你當(dāng)前的iptables規(guī)則放/etc/sysconfig/iptables中,系統(tǒng)啟動(dòng)iptables時(shí)自動(dòng)執(zhí)行。

      后兩種更好此,一般iptables服務(wù)會(huì)在network服務(wù)之前啟來(lái),更安全

      解封:

      iptables -L INPUT

      iptables -L --line-numbers 然后iptables -D INPUT 序號(hào)

      iptables 限制ip訪問

      通過(guò)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禁止某個(gè)IP地址訪問的方法”還想看:

    1.linux防火墻如何阻止ip訪問

    2.win7系統(tǒng)如何屏蔽某個(gè)ip鏈接

    3.如何禁止修改ip地址

    4.Cisco路由如何設(shè)置命令阻止訪問特定網(wǎng)站

    3089662