Linux 禁止某個IP地址訪問的幾種方法

來源:互聯網
上載者:User

一、概述

這兩個檔案是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可以省略。

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

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.