Linux如何禁止特定ip地址訪問

來源:互聯網
上載者:User

   Linux系統中,如果你需要禁止特定ip地址訪問來保證系統的安全,只需通過操作iptalbes來實現,下面小編就給大家介紹下Linux如何禁止某個ip地址訪問,感興趣的朋友可以來瞭解下。


  一、概述

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

聯繫我們

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