1. 程式人生 > >Linux上使用iptables設定防火牆

Linux上使用iptables設定防火牆

因為老師留作業要對Netfilter核心模組進行擴充套件程式設計,因此在此之前先學習了一下iptables的用法,筆記一下,忘了就來看看。

iptables和netfilter

先說一下iptables和netfilter的關係:

netfilter在核心空間的程式碼根據table中的rules,完成對packet的分析和處置。但是這些table中的具體的防火牆rules,還是必須由系統管理員親自編寫。核心中的netfilter只是提供了一個機制,它並不知道該怎樣利用這個機制,寫出合適的rules,來實現一個網路防火牆。

那麼,系統管理員編寫的rules,怎樣進入位於核心空間中的netfilter維護的table中去呢?這個任務是由iptables這個工具來完成的。

說白了就是netfilter是位於核心裡的,iptables是位於使用者空間的管理工具。有了iptables,使用者空間就可以和核心中的netfilter進行交流,維護table中的防火牆rules了。

iptables的一丁點兒基本知識

表 (tables)

iptables 包含 5 張表(tables):
- raw 用於配置資料包,raw 中的資料包不會被系統跟蹤。
- filter 是用於存放所有與防火牆相關操作的預設表。
- nat 用於 網路地址轉換(例如:埠轉發)。
- mangle 用於對特定資料包的修改(參考 損壞資料包)。
- security 用於 強制訪問控制 網路規則(例如: SELinux – 詳細資訊參考 該文章)。
大部分情況僅需要使用 filter 和 nat。其他表用於更復雜的情況——包括多路由和路由判定。

鏈 (chains)

我們暫時主要了解一下filter的過濾好了。

表由鏈組成,鏈是一些按順序排列的規則的列表。預設的 filter 表包含 INPUT, OUTPUT 和 FORWARD 3條內建的鏈,這3條鏈用於資料包過濾。

規則 (rules)

資料包的過濾基於規則。規則由一個目標(資料包包匹配所有條件後的動作)和很多匹配(導致該規則可以應用的資料包所滿足的條件)指定。一個規則的典型匹配事項是資料包進入的埠(例如:eth0 或者 eth1)、資料包的型別(ICMP, TCP, 或者 UDP)和資料包的目的埠。

配置filter表的具體操作

在我的Arch上,執行iptables的話需要root許可權,畢竟是對核心功能的操作。

檢視當前iptables的設定

pArch# iptables -nvL
Chain INPUT (policy ACCEPT 1 packets, 52 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 1 packets, 52 bytes)
 pkts bytes target     prot opt in     out     source               destination 

以上引數的意義:

-L, --list [chain]
              List all rules in the selected chain.  If no chain is selected, all chains are listed. 

-n, --numeric
              Numeric output.  IP addresses and port numbers will be printed in numeric format.  By default, the program will  try to display them as host names, network names, or services (whenever applicable).

-v, --verbose
              Verbose output.  This option makes the list command show the interface name, the rule options (if any), and the  TOS masks.   The  packet  and  byte  counters  are  also listed, with the suffix 'K', 'M' or 'G' for 1000, 1,000,000 and 1,000,000,000 multipliers respectively.

上面的三條鏈INPUT/FORWARD/OUTPUT的policy都是ACCEPTED,表明還沒有配置規則。沒有資料包被阻止,均被接受。

清除原有規則

pArch# iptables -F  #清除預設表filter中的所有規則鏈的規則
pArch# iptables -X  #清除預設表filter中使用者自定鏈中的規則

如果已經有了過濾規則,可以通過以上命令清空,然後再一步步設定。

預設規則
比如將INPUT(所有接收的包)預設都丟掉:

pArch# iptables -P INPUT DROP
pArch# iptables -nvL         
Chain INPUT (policy DROP 26 packets, 3074 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 22 packets, 1626 bytes)
 pkts bytes target     prot opt in     out     source               destination      

其中引數:

-P, --policy chain target
              Set  the  policy  for  the chain to the given target. Only built-in (non-user-defined) chains can have policies, and neither built-in nor user-defined chains can be policy targets.

可以看到,INPUT的policy已經變成DROP了,而立刻就有妄圖發到我的電腦的26 packets, 3074 bytes被丟棄了。

注:如果你是遠端SSH登陸的話,當你輸入第一個命令回車的時候就應該掉了,因為設定完預設丟棄所有包之後,沒有設定任何可以接收的包。掉線了怎麼辦呢,只能去本機操作了=.=!

新增規則
由於剛剛我們預設INPUT為DROP,則所有進入計算機的包都會被防火牆丟掉,本機收不到任何包。也就是說此時如果進行ping www.baidu.com的話是不會成功的,因為icmp的echo request報文雖然發出去了,但是echo response報文被drop了。

為了能ping通,我們需要設定icmp報文為可接受狀態:

pArch# iptables -A INPUT -p icmp -j ACCEPT
pArch# iptables -nvL --line-numbers       
Chain INPUT (policy DROP 227 packets, 36248 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1       18  1869 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0     

其中:

-A, --append chain rule-specification
              Append one or more rules to the end of the selected chain.  When the source and/or destination names resolve to more than one address, a rule will be added for each possible address combination.

[!] -p, --protocol protocol
              The  protocol  of the rule or of the packet to check.  The specified protocol can be one of tcp, udp, udplite, icmp, icmpv6,esp, ah, sctp, mh or the special keyword "all", or it can be a numeric value, representing one of these  pro‐tocols or a different one.  

-j, --jump target
              This specifies the target of the rule; i.e., what to do if the packet matches it.  

所以iptables -A INPUT -p icmp -j ACCEPT的意思就是給INPUT鏈新增一條規則,對於所有的icmp協議,都ACCEPT。

這樣的話ping就可以執行了。

小例項

我們都知道訪問伺服器的80埠可以上網,所以我們使用iptables對自己的防火牆進行設定:除了上網的報文,其餘所有出去的報文均被攔截。

設定之後的效果:

pArch# iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT
pArch# iptables -nvL --line-numbers
Chain INPUT (policy ACCEPT 1 packets, 52 bytes)
num   pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy DROP 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1     171K   11M ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80

其中--dport代表destination port,目的埠號;--sport代表source port,源埠號。

上面的設定就是說我的電腦對訪問外伺服器的80埠的tcp協議報文是放行的,即對請求網頁的報文是放行的,再說白點兒就是允許上網。

然後當我在瀏覽器中上百度的時候卻發現無論如何都上不去。這不科學!應該是可以上網的啊,我已經把通往外網80埠的資料包預設放行了啊!

後來找了半天原因,才發現百度的網址是https://www.baidu.com/,是https而不是http,而https所對應的埠是443(443/TCP HTTPS - HTTP over TLS/SSL(加密傳輸)),不是80(80/TCP HTTP(超文字傳輸協議)- 用於傳輸網頁)。

然後把443埠也設為ACCEPT,iptables -A OUTPUT -p tcp --dport 443 -j ACCEPT,再上百度果然就行了。

配置並執行 iptables

iptables 是一個 Systemd 服務,因此可以這樣啟動:

systemctl start iptables

但是,除非有 /etc/iptables/iptables.rules 檔案,否則服務不會啟動,Arch iptables 包不包含預設的 iptables.rules 檔案。因此,第一次啟動服務時使用以下命令:

touch /etc/iptables/iptables.rules
systemctl start iptables

或者

cp /etc/iptables/empty.rules /etc/iptables/iptables.rules
systemctl start iptables

和其他服務一樣,如果希望啟動時自動載入 iptables,必須啟用該服務:

systemctl enable iptables

配置檔案

通過命令列新增規則,配置檔案不會自動改變,所以必須手動儲存:

iptables-save > /etc/iptables/iptables.rules

修改配置檔案後,需要重新載入服務:

systemctl reload iptables

或者通過 iptables 直接載入:

iptables-restore < /etc/iptables/iptables.rules

相關推薦

Linux使用iptables設定防火牆

因為老師留作業要對Netfilter核心模組進行擴充套件程式設計,因此在此之前先學習了一下iptables的用法,筆記一下,忘了就來看看。 iptables和netfilter 先說一下iptables和netfilter的關係: netfilte

UbuntuUFW設定防火牆

轉自:https://www.digitalocean.com/community/users/hazelnut 介紹 UFW或Uncomplicated Firewall是iptables一個介面,旨在簡化配置防火牆的過程。 雖然iptables是一個可靠而靈活的工具,但

linuxiptables配置防火牆

netfilter的工作原理 我們以系統預設的表為“filter”為例進行講解。該表中包含了INPUT、 FORWARD和OUTPUT 3個鏈。每一條鏈中可以有一條或數條規則,每一條規則都是這樣定義的“如果資料包頭符合這樣的條件,就這樣處理這個資料包”。當一個數據包到達一個鏈時,系統就會

linux如何設定網路,出現connect: network is unreachable 的問題。

發現有網友問有關ping命令出現connect: network is unreachable 的問題。 這通常是因為沒正確設定ip地址。 解決方法: 在確保完善網絡卡驅動,以及確保將網絡卡驅動編譯進核心後,檢查 ls /etc/sysconfig/network-script/ifcfg-eth0

Linux安裝設定mysql 5.7.24

一,準備 1,先檢視Linux是32位還是64位 getconf LONG_BIT 如果返回的是32,那麼就是32位 如果返回的是64,那麼就是64位 2,如果伺服器不能聯網,就先去官網下載好壓縮包,然後上傳到伺服器 下載地址:https://dev.mysql.com/downloads/mys

Linuxiptables設定詳細

無論如何,iptables是一個需要特別謹慎設定的東西,萬一伺服器不在你身邊,而你貿然設定導致無法SSH,那就等著被老闆罵吧,呵呵。。。以下內容是為了防止這種情況發生而寫的,當然很初級,不過一般伺服器也夠用了: 1.首先介紹一下指令和相關配置檔案 啟動指令:service

linux怎麼設定mysql的最大連線數

一、mysql最大連線數的設定 1:檢視當前mysql支援的最大連線數 mysql> show variables like 'max_connections'; +-----------------+-------+ | Variable_name | Va

Ubuntu based GNU/Linux 防火牆 (ufw) 基本設定

Linux 上設定防火牆iptables 有些功能其實用不到,例如 nat, forward等等的, 而且語法有點複雜,另一套防火牆 ufw (Uncomplicated Firewall ),相對於 iptables 來說,ufw 是真的簡單很多了! ufw其實只是一個 iptables 的前端設定程式,

LinuxIPTABLES防火牆設定

安裝linux後(防火牆是開啟狀態),需要檢查防火牆埠 1.iptables防火牆啟動和停止 啟動iptables防火牆時命令列輸入 #service iptables start [[email protected] ~]# service iptab

VPSiptables防火牆的基本設定

http://www.huzs.net/?p=975 看到有弄IPTABLES沒搞出來的,把我的經驗貼上來,大家分享下! http://www.linuxsir.org/bbs/thread315767.html Debian 配置防火牆 iptables

linux系統中的防火牆iptables與firewalld)——iptables

iptables 關閉firewalld開啟iptables 相關概念 IPTABLES 是與最新的 3.5 版本 Linux 核心整合的 IP 資訊包過濾系統。如果 Linux 系統連線到因特網或 LAN、伺服器或連線 LAN 和因特網的代理伺服器, 則該系統有利於在 Lin

linux系統中的防火牆iptables與firewalld)——firewalld

防火牆 防火牆是整個資料包進入主機前的第一道關卡。防火牆主要通過Netfilter與TCPwrappers兩個機制來管理的。 1)Netfilter:資料包過濾機制 2)TCP Wrappers:程式管理機制 關於資料包過濾機制有兩個軟體:firewalld與iptables cento

Linux CentOS7 設定防火牆使apache及mysql可以被外部訪問

1、關閉firewall # systemctl stop firewalld.service 2、禁止firewall開機啟動 # systemctl disable firewalld.service 3、安裝iptables防火牆 # yum install -

LinuxIptables防火牆相關概念和基本操作

iptables概念 一、 iptables的前身叫ipfirewall(核心1.x時代),這是一個作者從freeBSD上移植過來的,能夠工作在核心當中的,對資料包進行檢測的一款簡易訪問控制工具。但是ipfirewall工作功能極其有限(它需要將所有的規則都放進核心當中,這樣規則才能

如何在Debian Linux設定靜態IP地址

目的 目標是在Debian Linux伺服器上配置靜態IP地址。請注意,對於桌面安裝,建議使用GUI工具,例如network-manager。如果您希望通過/etc/network/interfaces桌面上的檔案直接配置網路介面,請確保禁用任何其他可能干擾的網路配置守護程式例如,以下命令將禁用

用Spring的郵件封裝類JavaMailSenderImpl傳送郵件:注意埠號不需要設定(設定了反而linux釋出後傳送失敗)

===》###注意埠號不需要設定:》加不加埠:window都行,linux加了埠反而發不出去! 是的。配置了埠,Windows沒問題。linux就有問題了。仔細檢視錯誤資訊: 發現是 JavaMail API 程式碼問題。排除了是我的程式碼邏輯問題。 而且是埠超時問題。 看到這

使用PuTTY遠端登陸虛擬機器Linux系統相關設定

話說小白在第一次嘗試使用Linux的時候就是在虛擬機器上吧,但是新問題在於,我們學習Linux肯定是有關伺服器的,所以涉及到了遠端登陸Linux系統。其實最重要的就是使用的 putty 的 host name 與 Linux 的 IP 地址一樣就可以了 本文開始: 1.本

Centos7防火牆firewall與iptables設定

iptables配置 yum install iptables -y iptables(選項)(引數)   -P:定義規則鏈中的預設策略;   -t<表>:指定要操縱的表;   -A:向規則鏈中新增條目;   -D:從規則鏈中刪除條目;   -I:向規則鏈中插入條目;   -R:替換

(轉)Linuxiptables做代理伺服器和防火牆詳解

https://blog.csdn.net/harryxlb/article/details/7339307 用Linux的iptables做代理伺服器和防火牆配置詳細介紹     代理/防火牆     1.iptables規則表 &n

(轉)iptables設定Linux全域性代理

https://blog.csdn.net/junmuzi/article/details/51508802 一般用Linux的話都是用全域性代理都是用http_proxy以及https_proxy這兩個環境環境變數來實現的, 但是缺點是這種方式並不是真正的全域性代理, 即使設定了代理的話,對於