inetd程序在系統中是作爲一個服務進程出現,它監聽許多端口並且在得到客戶請求時啓動這個端口的服務程序。
早期系統中使用的inetd被稱作超級服務器,其實現控制對主機網絡連接。當一個請求到達由inetd管理的服務端口,inetd將該請求轉發給名爲 tcpd的程序。tcpd根據配置文件host.{allow,deny}來判斷是否允許服務該請求,如果請求被允許剛相應的服務器程序(如:ftpd、 telnet)將被啓動。這個機制也被稱爲TCP_Wrapper。
xinetd(eXended Internet services Daemon)提供類似於inetd+tcp_wrapper的功能,但是更加強大和安全。已經逐漸取代了inetd,並且提供了訪問控制、加強的日誌和資源管理功能,成了Linux系統的Internet標準超級守護進程。很多系統服務都用到了xinetd如:FTP、IMAP、POP和telnet等。/etc/services中所有的服務通過他們的端口來訪問服務器的時候,先由xinetd來處理,在喚起服務請求之前,xinetd先檢驗請求者是否滿足配置文件中指定的訪問控制規則,當前的訪問是否超過了指定的同時訪問數目,還有配置文件中指定的其他規則等,檢查通過,xinetd將這個請求交付到相應的服務去處理,自己就進入sleep狀態,等待下一個請求的處理。
以telnet爲例,每當有telnet的連接請求時,tcpd即會截獲請求,先讀取系統管理員所設置的訪問控制文件,合乎要求,則會把這次連接原封不動的轉給真正的telnet進程,由telnet完成後續工作;如果這次連接發起的ip不符合訪問控制文件中的設置,則會中斷連接請求,拒絕提供telnet服務。
#ldd $(which Command) | grep wrap "查看是否支持TCP Wrapper的服務"
[root@rhel6 ~]# ldd `which vsftpd` | grep wrap
libwrap.so.0 => /lib64/libwrap.so.0 (0x00007f58c2cdd000) "有返回值則表示支持TCP_Wrapper"
[root@rhel6 ~]# ldd `which sshd` | grep wrap
libwrap.so.0 => /lib64/libwrap.so.0 (0x00007fdbbc848000)
· 配置文件
TCP_Wrappers的主要配置文件爲:/etc/hosts.allow和/etc/hosts.deny。
/usr/sbin/tcpd進程首先檢查/etc/hosts.allow,如果請求訪問的主機名或IP包含在此文件中,則允許訪問。
如果請求訪問的主機名或IP不包含在/etc/hosts.allow中,那麼tcpd進程就檢查/etc/hosts.deny。如果請求訪問的主機名或IP包含在hosts.deny文件中,則訪問就被拒絕;
如果都沒有,默認許可
· 訪問控制規則的格式
訪問控制需要加在/etc/hosts.allow和/etc/hosts.deny裏,規則格式如下:
<daemon list>:<client list>[:<option>:<option>:...]
daemon list 服務進程名列表,如telnet的服務進程名爲in.telnetd
client list 訪問控制的客戶端列表,可以寫域名、主機名或網段,如.example.com或者192.168.1.
option 可選選項,這裏可以是某些命令,也可以是指定的日誌文件
- [root@rhel6 ~]# cat /etc/hosts.allow
- #
- # hosts.allow This file contains access rules which are used to
- # allow or deny connections to network services that
- # either use the tcp_wrappers library or that have been
- # started through a tcp_wrappers-enabled xinetd.
- #
- # See 'man 5 hosts_options' and 'man 5 hosts_access'
- # for information on rule syntax.
- # See 'man tcpd' for information on tcp_wrappers
- in.telnetd:.xfcy.org
- vsftpd:192.168.0.
- sshd:192.168.0.0/255.255.255.0
-
- [root@rhel6 ~]# cat /etc/hosts.deny
- #
- # hosts.deny This file contains access rules which are used to
- # deny connections to network services that either use
- # the tcp_wrappers library or that have been
- # started through a tcp_wrappers-enabled xinetd.
- #
- # The rules in this file can also be set up in
- # /etc/hosts.allow with a 'deny' option instead.
- #
- # See 'man 5 hosts_options' and 'man 5 hosts_access'
- # for information on rule syntax.
- # See 'man tcpd' for information on tcp_wrappers
- #
- ALL:ALL
-
- /etc/hosts.deny裏的ALL:ALL表示,除非在/etc/hosts.allow裏明確允許訪問,否則一律拒絕
- /etc/hosts.allow裏第一行in.telnetd:.xfcy.org表示,只有xfcy.org這個域裏的主機允許訪問telnet服務,注意xfcy.org前面的那個點(.)
- /etc/hosts.allow裏第二行表示,只有192.168.0這個網段的用戶允許訪問FTP服務,注意0後面的點(.)
- /etc/hosts.allow裏第三行表示,只有192.168.0這個網段的用戶允許訪問SSH服務,注意這裏不能寫爲192.168.0.0/24