1. 程式人生 > >基礎運維:iptables防火墻入門到掌握

基礎運維:iptables防火墻入門到掌握

解釋 may tput new centos7.x eric flush img get

一、簡介

IPTABLES 是與最新的 3.5 版本 Linux 內核集成的 IP 信息包過濾系統。如果 Linux 系統連接到因特網或 LAN、服務器或連接 LAN 和因特網的代理服務器, 則該系統有利於在 Linux 系統上更好地控制 IP 信息包過濾和防火墻配置。
防火墻在做數據包過濾決定時,有一套遵循和組成的規則,這些規則存儲在專用的數據包過濾表中,而這些表集成在 Linux 內核中。在數據包過濾表中,規則被分組放在我們所謂的鏈(chain)中。而netfilter/iptables IP 數據包過濾系統是一款功能強大的工具,可用於添加、編輯和移除規則。

雖然 netfilter/iptables IP 信息包過濾系統被稱為單個實體,但它實際上由兩個組件netfilter 和 iptables 組成。

netfilter 組件也稱為內核空間(kernelspace),是內核的一部分,由一些信息包過濾表組成,這些表包含內核用來控制信息包過濾處理的規則集。

iptables 組件是一種工具,也稱為用戶空間(userspace),它使插入、修改和除去信息包過濾表中的規則變得容易。除非您正在使用 Red Hat Linux 7.1 或更高版本,否則需要下載該工具並安裝使用它。

二、IPtables結構

Linux下的iptables表結構
技術分享圖片

三、基礎iptables

Linux centos7.X中iptables地位變低了,改成firewall。但日常使用的都是iptables。

1.安裝iptables防火墻服務


[root@leo ~]# yum install -y iptables

2.查找系統關於iptables防火墻的文件或者目錄

[root@leo ~]# find / -name iptables*    ( * 代表任意長度字符串)
/etc/sysconfig/iptables-config      #iptables防火墻的配置文件
/etc/selinux/targeted/active/modules/100/iptables       #selinux下的防護規則目錄
/usr/bin/iptables-xml       #iptables的二進制xml文件
/usr/sbin/iptables-restore      #iptables的管理命令,重置或恢復iptables規則命令
/usr/sbin/iptables-save     #iptables的管理命令,保存iptables規則命令
/usr/sbin/iptables          #iptables管理命令,設置iptables規則命令
/usr/share/doc/iptables-1.4.21      #iptables的目錄(帶版本)
/usr/share/man/man1/iptables-xml.1.gz   #對應上面文件或命令的壓縮軟件包
/usr/share/man/man8/iptables-extensions.8.gz    #對應上面文件或命令的壓縮軟件包
/usr/share/man/man8/iptables-restore.8.gz       #對應上面文件或命令的壓縮軟件包
/usr/share/man/man8/iptables-save.8.gz      #對應上面文件或命令的壓縮軟件包
/usr/share/man/man8/iptables.8.gz       #對應上面文件或命令的壓縮軟件包
[root@leo ~]# 

3.減少實驗障礙操作(非生產環境)

[root@leo ~]# iptables -F       #清除iptables規則鏈中已有的條目
[root@leo ~]# iptables -X       #清除iptables自定義的規則鏈
[root@leo ~]# iptables -Z       #清除iptables規則鏈中的數據包計算器和字節計算器(歸零)

溫馨提醒:該操作能在實驗中少犯錯,降低入門的門檻,在運維進階中會詳細說明各個規則及生產用處。

4.設置iptables規則鏈

4.1 官方設置說明:

[root@leo ~]# iptables --help
iptables v1.4.21

Usage: iptables -[ACD] chain rule-specification [options]
       iptables -I chain [rulenum] rule-specification [options]
       iptables -R chain rulenum rule-specification [options]
       iptables -D chain rulenum [options]
       iptables -[LS] [chain [rulenum]] [options]
       iptables -[FZ] [chain] [options]
       iptables -[NX] chain
       iptables -E old-chain-name new-chain-name
       iptables -P chain target [options]
       iptables -h (print this help information)

Commands:
Either long or short options are allowed.
  --append  -A chain        Append to chain
  --check   -C chain        Check for the existence of a rule
  --delete  -D chain        Delete matching rule from chain
  --delete  -D chain rulenum
                Delete rule rulenum (1 = first) from chain
  --insert  -I chain [rulenum]
                Insert in chain as rulenum (default 1=first)
  --replace -R chain rulenum
                Replace rule rulenum (1 = first) in chain
  --list    -L [chain [rulenum]]
                List the rules in a chain or all chains
  --list-rules -S [chain [rulenum]]
                Print the rules in a chain or all chains
  --flush   -F [chain]      Delete all rules in  chain or all chains
  --zero    -Z [chain [rulenum]]
                Zero counters in chain or all chains
  --new     -N chain        Create a new user-defined chain
  --delete-chain
            -X [chain]      Delete a user-defined chain
  --policy  -P chain target
                Change policy on chain to target
  --rename-chain
            -E old-chain new-chain
                Change chain name, (moving any references)
Options:
    --ipv4  -4      Nothing (line is ignored by ip6tables-restore)
    --ipv6  -6      Error (line is ignored by iptables-restore)
[!] --protocol  -p proto    protocol: by number or name, eg. `tcp‘
[!] --source    -s address[/mask][...]
                source specification
[!] --destination -d address[/mask][...]
                destination specification
[!] --in-interface -i input name[+]
                network interface name ([+] for wildcard)
 --jump -j target
                target for rule (may load target extension)
  --goto      -g chain
                              jump to chain with no return
  --match   -m match
                extended match (may load extension)
  --numeric -n      numeric output of addresses and ports
[!] --out-interface -o output name[+]
                network interface name ([+] for wildcard)
  --table   -t table    table to manipulate (default: `filter‘)
  --verbose -v      verbose mode
  --wait    -w [seconds]    maximum wait to acquire xtables lock before give up
  --wait-interval -W [usecs]    wait time to try to acquire xtables lock
                default is 1 second
  --line-numbers        print line numbers when listing
  --exact   -x      expand numbers (display exact values)
[!] --fragment  -f      match second or further fragments only
  --modprobe=<command>      try to insert modules using this command
  --set-counters PKTS BYTES set the counter during insert/append
[!] --version   -V      print package version.

4.2 通俗(常用)設置說明:

iptables(選項)(參數)
-t<表>:指定要操縱的表;
-A:向規則鏈中添加條目;
-D:從規則鏈中刪除條目;
-i:向規則鏈中插入條目;
-R:替換規則鏈中的條目;
-L:顯示規則鏈中已有的條目;
-F:清楚規則鏈中已有的條目;
-Z:清空規則鏈中的數據包計算器和字節計數器;
-N:創建新的用戶自定義規則鏈;
-P:定義規則鏈中的默認目標;
-h:顯示幫助信息;
-p:指定要匹配的數據包協議類型;
-s:指定要匹配的數據包源ip地址;
-j<目標>:指定要跳轉的目標;
-i<網絡接口>:指定數據包進入本機的網絡接口;
-o<網絡接口>:指定數據包要離開本機所使用的網絡接口。

5.規則鏈設置

5.1 設置說明

iptables -t 表名 <-A/I/D/R> 規則鏈名 [規則號] <-i/o 網卡名> -p 協議名 <-s 源IP/源子網> --sport 源端口 <-d 目標IP/目標子網> --dport 目標端口 -j 動作

舉個例子:放行22端口,暴露在互聯網中,任意地址都能通過22端口連接到服務器
解釋:只需要在入站規則設置就好(入站就是進入服務器,出站就是從服務器出去)

[root@leo ~]# iptables -L
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         
[root@leo ~]# iptables -I INPUT -p tcp --dport 22  -j ACCEPT
[root@leo ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

5.2 顯示規則詳情

[root@leo ~]# iptables -nvL
Chain INPUT (policy ACCEPT 3 packets, 234 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  128  9372 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22

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

Chain OUTPUT (policy ACCEPT 90 packets, 14552 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@leo ~]# 

6.進階iptables防火墻規則
這一部分有點復雜,放到運維進階中,有興趣可以關註動態,按照全棧運維路線更新。

基礎運維:iptables防火墻入門到掌握