Linux下防火牆iptables學習筆記

來源:互聯網
上載者:User


一、iptables的開啟和關閉

1. 重啟系統生效


[Sun@webserver2 ~]$ sudo chkconfig iptables on     開啟
[Sun@webserver2 ~]$ sudo chkconfig iptables off    關閉

2.即時生效,重啟系統後失效


[Sun@webserver2 ~]$ sudo service iptables start    開啟
[Sun@webserver2 ~]$ sudo service iptables stop     關閉

二、查看iptables配置

[Sun@webserver2 ~]$ sudo iptables -L -n

三、開放指定的連接埠

每次修改後重啟iptables服務生效

1.允許本地迴環介面(即運行本機訪問本機)
1
[Sun@webserver2 ~]$ sudo iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT

2.允許訪問80連接埠(web伺服器常用連接埠)


[Sun@webserver2 ~]$ sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT

3.允許訪問22連接埠(ssh連接埠)


[Sun@webserver2 ~]$ sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

4.允許訪問3306連接埠(MySQL連接埠)


[Sun@webserver2 ~]$ sudo iptables -A INPUT -p tcp --dport 3306 -j ACCEPT

5.允許訪問21和20連接埠(FTP伺服器連接埠)


[Sun@webserver2 ~]$ sudo iptables -A INPUT -p tcp --dport 20 -j ACCEPT
[Sun@webserver2 ~]$ sudo iptables -A INPUT -p tcp --dport 21 -j ACCEPT

6.允許所有本機向外的訪問

[Sun@webserver2 ~]$ sudo iptables -A OUTPUT -j ACCEPT

四、屏蔽IP

每次修改後重啟iptables服務生效

1.屏蔽單個IP


[Sun@webserver2 ~]$ sudo iptables -I INPUT -s 123.45.6.7 -j DROP

2.屏蔽從123.0.0.1到123.255.255.254網段

[Sun@webserver2 ~]$ sudo iptables -I INPUT -s 123.0.0.0/8 -j DROP

3.屏蔽從123.1.0.1到123.1.255.254網段

[Sun@webserver2 ~]$ sudo iptables -I INPUT -s 123.1.0.0/16 -j DROP

4.屏蔽從123.1.2.1到123.1.2.254網段


[Sun@webserver2 ~]$ sudo iptables -I INPUT -s 123.1.2.0/24 -j DROP

五、刪除已添加的iptables規則
每次修改後重啟iptables服務生效

1.將所有iptables以序號標記顯示

[Sun@webserver2 ~]$ sudo iptables -L -n --line-numbers

2.執行刪除序號為1的

1
[Sun@webserver2 ~]$ sudo iptables -D INPUT 1
 

聯繫我們

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