1. 程式人生 > >centos 安全配置應用之hosts.allow和hosts.deny

centos 安全配置應用之hosts.allow和hosts.deny

一、概述

這兩個檔案是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
才能讓剛才的更改生效。