linux防火牆iptables允許指定連接埠通過

來源:互聯網
上載者:User


1、允許通過某一連接埠

vi /etc/sysconfig/iptables

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT(允許80連接埠通過防火牆)

/etc/init.d/iptables restart 

#最後重啟防火牆使配置生效

只允許特定ip訪問某連接埠?參考下面命令,只允許46.166.150.22訪問原生80連接埠。如果要設定其他ip或連接埠,改改即可。

iptables -I INPUT -p TCP --dport 80 -j DROP
iptables -I INPUT -s 46.166.150.22 -p TCP --dport 80 -j ACCEPT

在root使用者下執行上面2行命令後,重啟iptables, service iptables restart

查看iptables是否生效:

[root@www.ctohome.com]# iptables -L
Chain INPUT (policy ACCEPT)
target           prot opt source               destination        
ACCEPT     tcp  --  46.166.150.22    anywhere            tcp dpt:http
DROP         tcp  --  anywhere             anywhere            tcp dpt:http

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination        

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination       

 上面命令是針對整個伺服器(全部ip)禁止80連接埠,如果只是需要禁止伺服器上某個ip地址的80連接埠,怎麼辦?

下面的命令是只允許來自174.140.3.190的ip訪問伺服器上216.99.1.216的80連接埠

iptables -A FORWARD -s 174.140.3.190 -d 216.99.1.216 -p tcp -m tcp --dport 80 -j ACCEPT
iptables -A FORWARD -d 216.99.1.216 -p tcp -m tcp --dport 80 -j DROP

如果您不熟悉linux的ssh命令,那麼可以在webmin/virtualmin面板中設定,達到相同效果。參考:webmin面板怎樣設定允許特定ip訪問80連接埠,禁止80連接埠


更多iptables參考命令如下:

1.先備份iptables

# cp /etc/sysconfig/iptables /var/tmp

需要開80連接埠,指定IP和區域網路

下面三行的意思:

先關閉所有的80連接埠

開啟ip段192.168.1.0/24端的80口

開啟ip段211.123.16.123/24端ip段的80口

# iptables -I INPUT -p tcp --dport 80 -j DROP
# iptables -I INPUT -s 192.168.1.0/24 -p tcp --dport 80 -j ACCEPT
# iptables -I INPUT -s 211.123.16.123/24 -p tcp --dport 80 -j ACCEPT

以上是臨時設定。

2.然後儲存iptables

# service iptables save

3.重啟防火牆

#service iptables restart

===============以下是轉載================================================

以下是連接埠,先全部封再開某些的IP

iptables -I INPUT -p tcp --dport 9889 -j DROP
iptables -I INPUT -s 192.168.1.0/24 -p tcp --dport 9889 -j ACCEPT
如果用了NAT轉寄記得配合以下才會生效

iptables -I FORWARD -p tcp --dport 80 -j DROP
iptables -I FORWARD -s 192.168.1.0/24 -p tcp --dport 80 -j ACCEPT

 

 

常用的IPTABLES規則如下:
只能收發郵件,別的都關閉
iptables -I Filter -m mac --mac-source 00:0F:EA:25:51:37 -j DROP
iptables -I Filter -m mac --mac-source 00:0F:EA:25:51:37 -p udp --dport 53 -j ACCEPT
iptables -I Filter -m mac --mac-source 00:0F:EA:25:51:37 -p tcp --dport 25 -j ACCEPT
iptables -I Filter -m mac --mac-source 00:0F:EA:25:51:37 -p tcp --dport 110 -j ACCEPT


IPSEC NAT 策略
iptables -I PFWanPriv -d 192.168.100.2 -j ACCEPT
iptables -t nat -A PREROUTING -p tcp --dport 80 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.100.2:80

iptables -t nat -A PREROUTING -p tcp --dport 1723 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.100.2:1723

iptables -t nat -A PREROUTING -p udp --dport 1723 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.100.2:1723

iptables -t nat -A PREROUTING -p udp --dport 500 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.100.2:500

iptables -t nat -A PREROUTING -p udp --dport 4500 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.100.2:4500


FTP伺服器的NAT
iptables -I PFWanPriv -p tcp --dport 21 -d 192.168.1.22 -j ACCEPT
iptables -t nat -A PREROUTING -p tcp --dport 21 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.1.22:21


只允許訪問指定網址
iptables -A Filter -p udp --dport 53 -j ACCEPT
iptables -A Filter -p tcp --dport 53 -j ACCEPT
iptables -A Filter -d www.ctohome.com -j ACCEPT
iptables -A Filter -d www.guowaivps.com -j ACCEPT
iptables -A Filter -j DROP


開放一個IP的一些連接埠,其它都封閉
iptables -A Filter -p tcp --dport 80 -s 192.168.1.22 -d www.pconline.com.cn -j ACCEPT
iptables -A Filter -p tcp --dport 25 -s 192.168.1.22 -j ACCEPT
iptables -A Filter -p tcp --dport 109 -s 192.168.1.22 -j ACCEPT
iptables -A Filter -p tcp --dport 110 -s 192.168.1.22 -j ACCEPT
iptables -A Filter -p tcp --dport 53 -j ACCEPT
iptables -A Filter -p udp --dport 53 -j ACCEPT
iptables -A Filter -j DROP


多個連接埠
iptables -A Filter -p tcp -m multiport --destination-port 22,53,80,110 -s 192.168.20.3 -j REJECT


連續連接埠
iptables -A Filter -p tcp -m multiport --source-port 22,53,80,110 -s 192.168.20.3 -j REJECT iptables -A Filter -p tcp --source-port 2:80 -s 192.168.20.3 -j REJECT


指定時間上網
iptables -A Filter -s 10.10.10.253 -m time --timestart 6:00 --timestop 11:00 --days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j DROP
iptables -A Filter -m time --timestart 12:00 --timestop 13:00 --days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j ACCEPT
iptables -A Filter -m time --timestart 17:30 --timestop 8:30 --days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j ACCEPT

禁止多個連接埠服務
iptables -A Filter -m multiport -p tcp --dport 21,23,80 -j ACCEPT


將WAN 口NAT到PC
iptables -t nat -A PREROUTING -i $INTERNET_IF -d $INTERNET_ADDR -j DNAT --to-destination 192.168.0.1


將WAN口8000連接埠NAT到192。168。100。200的80連接埠
iptables -t nat -A PREROUTING -p tcp --dport 8000 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.1.22:80


MAIL伺服器要轉的連接埠
iptables -t nat -A PREROUTING -p tcp --dport 110 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.1.22:110
iptables -t nat -A PREROUTING -p tcp --dport 25 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.1.22:25


只允許PING 202。96。134。133,別的服務都禁止
iptables -A Filter -p icmp -s 192.168.1.22 -d 202.96.134.133 -j ACCEPT
iptables -A Filter -j DROP

禁用BT配置
iptables –A Filter –p tcp –dport 6000:20000 –j DROP

禁用QQ防火牆配置
iptables -A Filter -p udp --dport ! 53 -j DROP
iptables -A Filter -d 218.17.209.0/24 -j DROP
iptables -A Filter -d 218.18.95.0/24 -j DROP
iptables -A Filter -d 219.133.40.177 -j DROP

基於MAC,只能收發郵件,其它都拒絕
iptables -I Filter -m mac --mac-source 00:0A:EB:97:79:A1 -j DROP
iptables -I Filter -m mac --mac-source 00:0A:EB:97:79:A1 -p tcp --dport 25 -j ACCEPT
iptables -I Filter -m mac --mac-source 00:0A:EB:97:79:A1 -p tcp --dport 110 -j ACCEPT

禁用MSN配置
iptables -A Filter -p udp --dport 9 -j DROP
iptables -A Filter -p tcp --dport 1863 -j DROP
iptables -A Filter -p tcp --dport 80 -d 207.68.178.238 -j DROP
iptables -A Filter -p tcp --dport 80 -d 207.46.110.0/24 -j DROP

只允許PING 202。96。134。133 其它公網IP都不許PING
iptables -A Filter -p icmp -s 192.168.1.22 -d 202.96.134.133 -j ACCEPT
iptables -A Filter -p icmp -j DROP

禁止某個MAC地址訪問internet:
iptables -I Filter -m mac --mac-source 00:20:18:8F:72:F8 -j DROP

禁止某個IP地址的PING:
iptables –A Filter –p icmp –s 192.168.0.1 –j DROP

禁止某個IP地址服務:
iptables –A Filter -p tcp -s 192.168.0.1 --dport 80 -j DROP
iptables –A Filter -p udp -s 192.168.0.1 --dport 53 -j DROP

只允許某些服務,其他都拒絕(2條規則)
iptables -A Filter -p tcp -s 192.168.0.1 --dport 1000 -j ACCEPT
iptables -A Filter -j DROP

禁止某個IP地址的某個連接埠服務
iptables -A Filter -p tcp -s 10.10.10.253 --dport 80 -j ACCEPT
iptables -A Filter -p tcp -s 10.10.10.253 --dport 80 -j DROP

禁止某個MAC地址的某個連接埠服務

iptables -I Filter -p tcp -m mac --mac-source 00:20:18:8F:72:F8 --dport 80 -j DROP

禁止某個MAC地址訪問internet:
iptables -I Filter -m mac --mac-source 00:11:22:33:44:55 -j DROP

禁止某個IP地址的PING:
iptables –A Filter –p icmp –s 192.168.0.1 –j DROP

下面轉截


啟動iptables
service iptables start
iptables –list //*查看iptables規則集*//
下面是沒有定義規劃時iptables的樣子:
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination

如何開啟/關閉指定連接埠
例如:
開啟81連接埠:
iptables -I INPUT -i eth0 -p tcp –dport 81 -j ACCEPT
iptables -I OUTPUT -o eth0 -p tcp –sport 81 -j ACCEPT
關閉81連接埠:
iptables -I INPUT -i eth0 -p tcp –dport 81 -j DROP
iptables -I OUTPUT -o eth0 -p tcp –sport 81 -j DROP
然後儲存
/etc/rc.d/init.d/iptables save

eth0為網卡名稱,可以輸入ifconfig來查看網卡資訊,注意填寫正確的網卡名稱。

可以使用lsof命令來查看某一連接埠是否開放.查看連接埠可以這樣來使用.
我就以81連接埠為例:
lsof -i:81
如果有顯示說明已經開放了,如果沒有顯示說明沒有開放。

聯繫我們

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