Linux 設定hosts.allow與hosts.deny屏蔽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.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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.