iptables

TL;DR

                             ┏╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍┓
 ┌───────────────┐               Network      table: filter            ┗━━━━━━━┳━━━━━━━┛
  chain: INPUT  │◀────┐              └───────┬───────┘                                           ┌───────────────────┐
                        table: nat          │local process│          chain: PREROUTING                         └─────────┬─────────┘
                                                                                    ┌─────────────────┐
┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅         ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅      │table: nat        Routing decision      └───── outing decision ─────▶│chain: PREROUTING│
┅┅┅┅┅┅┅┅┅┳┅┅┅┅┅┅┅┅┅          ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅      └────────┬────────┘
                                                                                                                         ┌───────────────┐                                             table: nat               ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅                 chain: OUTPUT     ┌─────▶ outing decision ◀──────────────┘
 └───────┬───────┘          ┅┅┅┅┅┅┅┅┳┅┅┅┅┅┅┅┅
                                                                       ┌───────────────┐       ┌────────────────────┐
  table: filter         chain: POSTROUTING   chain: OUTPUT ├────┘   └──────────┬─────────┘
 └───────────────┘                                                                                     ┏╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍┓
                                 Network                                 ┗━━━━━━━━━━━━━━━┛
iptables -F  # 清空所有的防火墙规则
iptables -X  # 删除用户自定义的空链
iptables -Z  # 清空计数

# 设置默认的规则
iptables -P INPUT DROP # 配置默认的不让进
iptables -P FORWARD DROP # 默认的不允许转发
iptables -P OUTPUT ACCEPT # 默认的可以出去

# 清除已有规则
iptables -F INPUT  # 清空指定链 INPUT 上面的所有规则
iptables -X INPUT  # 删除指定的链,这个链必须没有被其它任何规则引用,而且这条上必须没有任何规则。如果没有指定链名,则会删除该表中所有非内置的链。
iptables -Z INPUT  # 把指定链,或者表中的所有链上的所有计数器清零。

# 配置允许ssh端口连接
iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j ACCEPT
# 22为你的ssh端口, -s 192.168.1.0/24表示允许这个网段的机器来连接,其它网段的ip地址是登陆不了你的机器的。 -j ACCEPT表示接受这样的请求

## 允许本地回环地址可以正常使用
iptables -A INPUT -i lo -j ACCEPT
#本地圆环地址就是那个127.0.0.1,是本机上使用的,它进与出都设置为允许
iptables -A OUTPUT -o lo -j ACCEPT

# 配置白名单
iptables -A INPUT -p all -s 192.168.1.0/24 -j ACCEPT  # 允许机房内网机器可以访问
iptables -A INPUT -p all -s 192.168.140.0/24 -j ACCEPT  # 允许机房内网机器可以访问
iptables -A INPUT -p tcp -s 183.121.3.7 --dport 3380 -j ACCEPT # 允许183.121.3.7访问本机的3380端口

# 开启相应的服务端口
iptables -A INPUT -p tcp --dport 80 -j ACCEPT # 开启80端口,因为web对外都是这个端口
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT # 允许被ping
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # 已经建立的连接得让它进来

# 保存规则到配置文件中
cp /etc/sysconfig/iptables /etc/sysconfig/iptables.bak # 任何改动之前先备份,请保持这一优秀的习惯
iptables-save > /etc/sysconfig/iptables
cat /etc/sysconfig/iptables

# 列出已设置的规则
## > iptables -L [-t 表名] [链名]
### - 四个表名 `raw`,`nat`,`filter`,`mangle`
### - 五个规则链名 `INPUT`、`OUTPUT`、`FORWARD`、`PREROUTING`、`POSTROUTING`
### - filter表包含`INPUT`、`OUTPUT`、`FORWARD`三个规则链
iptables -L -t nat                  # 列出 nat 上面的所有规则 ^ -t 参数指定,必须是 raw, nat,filter,mangle 中的一个
iptables -L -t nat  --line-numbers  # 规则带编号
iptables -L INPUT
iptables -L -nv  # 查看,这个列表看起来更详细
# 将所有iptables以序号标记显示,执行:
iptables -L -n --line-numbers

# 比如要删除INPUT里序号为8的规则,执行:
iptables -D INPUT 8
# 删除已添加的规则
iptables -A INPUT -s 192.168.1.5 -j DROP

# 开放指定的端口
iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT               #允许本地回环接口(即运行本机访问本机)
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT    #允许已建立的或相关连的通行
iptables -A OUTPUT -j ACCEPT                                        #允许所有本机向外的访问
iptables -A INPUT -p tcp --dport 22 -j ACCEPT                       #允许访问22端口
iptables -A INPUT -p tcp --dport 80 -j ACCEPT                       #允许访问80端口
iptables -A INPUT -p tcp --dport 21 -j ACCEPT                       #允许ftp服务的21端口
iptables -A INPUT -p tcp --dport 20 -j ACCEPT                       #允许FTP服务的20端口

iptables -A INPUT -j reject                                         #禁止其他未允许的规则访问
iptables -A FORWARD -j REJECT                                       #禁止其他未允许的规则访问
# 屏蔽IP
iptables -A INPUT -p tcp -m tcp -s 192.168.0.8 -j DROP  # 屏蔽恶意主机(比如,192.168.0.8
iptables -I INPUT -s 123.45.6.7 -j DROP                 # 屏蔽单个IP的命令
iptables -I INPUT -s 123.0.0.0/8 -j DROP                # 封整个段即从123.0.0.1到123.255.255.254的命令
iptables -I INPUT -s 124.45.0.0/16 -j DROP              # 封IP段即从123.45.0.1到123.45.255.254的命令
iptables -I INPUT -s 123.45.6.0/24 -j DROP              # 封IP段即从123.45.6.1到123.45.6.254的命令是

# 指定数据包出去的网络接口
## 只对 OUTPUT,FORWARD,POSTROUTING 三个链起作用。
iptables -A FORWARD -o eth0

# 查看已添加的规则
iptables -L -n -v
Chain INPUT (policy DROP 48106 packets, 2690K bytes)
 pkts bytes target     prot opt in     out     source               destination
 5075  589K ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
 191K   90M ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:22
1499K  133M ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:80
4364K 6351M ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
 6256  327K ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0

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

Chain OUTPUT (policy ACCEPT 3382K packets, 1819M bytes)
 pkts bytes target     prot opt in     out     source               destination
 5075  589K ACCEPT     all  --  *      lo      0.0.0.0/0            0.0.0.0/0

# 启动网络转发规则
## 公网`210.14.67.7`让内网`192.168.188.0/24`上网
iptables -t nat -A POSTROUTING -s 192.168.188.0/24 -j SNAT --to-source 210.14.67.127

# 端口映射
## 本机的2222端口映射到内网 虚拟机的22端口
iptables -t nat -A PREROUTING -d 210.14.67.127 -p tcp --dport 2222  -j DNAT --to-dest 192.168.188.115:22

# 字符串匹配
## 比如,我们要过滤所有TCP连接中的字符串`test`,一旦出现它我们就终止这个连接,我们可以这么做:
iptables -A INPUT -p tcp -m string --algo kmp --string "test" -j REJECT --reject-with tcp-reset
iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
REJECT     tcp  --  anywhere             anywhere            STRING match "test" ALGO name kmp TO 65535 reject-with tcp-reset

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

# 阻止Windows蠕虫的攻击
iptables -I INPUT -j DROP -p tcp -s 0.0.0.0/0 -m string --algo kmp --string "cmd.exe"

# 防止SYN洪水攻击
iptables -A INPUT -p tcp --syn -m limit --limit 5/second -j ACCEPT

# 添加SECMARK记录
iptables -t mangle -A INPUT -p tcp --src 192.168.1.2 --dport 443 -j SECMARK --selctx system_u:object_r:myauth_packet_t
# 向从 192.168.1.2:443 以TCP方式发出到本机的包添加MAC安全上下文 system_u:object_r:myauth_packet_t

# 允许内网访问28000~30000之间的端口
iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 28000:30000 -j ACCEPT

iptables-man

附录

iptables常用主选项

iptables常用主选项

配置示例

k8s-master01

Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy DROP)
target     prot opt source               destination
DOCKER-USER  all  --  anywhere             anywhere
DOCKER-ISOLATION-STAGE-1  all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain DOCKER (1 references)
target     prot opt source               destination

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target     prot opt source               destination
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-ISOLATION-STAGE-2 (1 references)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-USER (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere

k8s-master02

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

k8s-master03

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

k8s-node06

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
cali-INPUT  all  --  anywhere             anywhere             /* cali:Cz_u1IQiXIMmKD4c */
KUBE-FIREWALL  all  --  anywhere             anywhere
KUBE-NODE-PORT  all  --  anywhere             anywhere             /* kubernetes health check rules */

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
cali-FORWARD  all  --  anywhere             anywhere             /* cali:wUHhoiAYhphO9Mso */
DOCKER-USER  all  --  anywhere             anywhere
DOCKER-ISOLATION-STAGE-1  all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
KUBE-FORWARD  all  --  anywhere             anywhere             /* kubernetes forwarding rules */
ACCEPT     all  --  anywhere             anywhere             /* cali:S93hcgKJrXEqnTfs */ /* Policy explicitly accepted packet. */ mark match 0x10000/0x10000
MARK       all  --  anywhere             anywhere             /* cali:mp77cMpurHhyjLrM */ MARK or 0x10000

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
cali-OUTPUT  all  --  anywhere             anywhere             /* cali:tVnHkvAo15HuiPy0 */
KUBE-FIREWALL  all  --  anywhere             anywhere

Chain KUBE-FORWARD (1 references)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere             mark match 0x4000/0x4000 /* kubernetes forwarding rules */
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED /* kubernetes forwarding conntrack pod source rule */
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED /* kubernetes forwarding conntrack pod destination rule */

Chain KUBE-NODE-PORT (1 references)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere             match-set KUBE-HEALTH-CHECK-NODE-PORT dst /* Kubernetes health check node port */

Chain DOCKER (1 references)
target     prot opt source               destination

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target     prot opt source               destination
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-ISOLATION-STAGE-2 (1 references)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-USER (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere

Chain KUBE-FIREWALL (2 references)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere             mark match 0x8000/0x8000 /* kubernetes firewall for dropping marked packets */
DROP       all  -- !127.0.0.0/8          127.0.0.0/8          ! ctstate RELATED,ESTABLISHED,DNAT /* block incoming localnet connections */
DROP       all  --  anywhere             anywhere             mark match 0x8000/0x8000 /* kubernetes firewall for dropping marked packets */
DROP       all  -- !127.0.0.0/8          127.0.0.0/8          ! ctstate RELATED,ESTABLISHED,DNAT /* block incoming localnet connections */

Chain KUBE-KUBELET-CANARY (0 references)
target     prot opt source               destination

Chain cali-to-wl-dispatch (2 references)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere             /* cali:7KNphB1nNHw80nIO */ /* Unknown interface */

Chain cali-OUTPUT (1 references)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere             /* cali:Mq1_rAdXXH3YkrzW */ mark match 0x10000/0x10000
cali-forward-endpoint-mark  all  --  anywhere             anywhere            [goto]  /* cali:5Z67OUUpTOM7Xa1a */ mark match ! 0x0/0xfff00000
RETURN     all  --  anywhere             anywhere             /* cali:M2Wf0OehNdig8MHR */
ACCEPT     ipencap--  anywhere             anywhere             /* cali:AJBkLho_0Qd8LNr3 */ /* Allow IPIP packets to other Calico hosts */ match-set cali40all-hosts-net dst ADDRTYPE match src-type LOCAL
MARK       all  --  anywhere             anywhere             /* cali:iz2RWXlXJDUfsLpe */ MARK and 0xfff0ffff
cali-to-host-endpoint  all  --  anywhere             anywhere             /* cali:xQqLi8S0sxbiyvjR */ ! ctstate DNAT
ACCEPT     all  --  anywhere             anywhere             /* cali:aSnsxZdmhxm_ilRZ */ /* Host endpoint policy accepted packet. */ mark match 0x10000/0x10000

Chain cali-to-hep-forward (2 references)
target     prot opt source               destination

Chain cali-INPUT (1 references)
target     prot opt source               destination
ACCEPT     ipencap--  anywhere             anywhere             /* cali:PajejrV4aFdkZojI */ /* Allow IPIP packets from Calico hosts */ match-set cali40all-hosts-net src ADDRTYPE match dst-type LOCAL
DROP       ipencap--  anywhere             anywhere             /* cali:_wjq-Yrma8Ly1Svo */ /* Drop IPIP packets from non-Calico hosts */
MARK       all  --  anywhere             anywhere             /* cali:ss8lEMQsXi-s6qYT */ MARK and 0xfffff
cali-forward-check  all  --  anywhere             anywhere             /* cali:PgIW-V0nEjwPhF_8 */
RETURN     all  --  anywhere             anywhere             /* cali:QMJlDwlS0OjHyfMN */ mark match ! 0x0/0xfff00000
cali-wl-to-host  all  --  anywhere             anywhere            [goto]  /* cali:nDRe73txrna-aZjG */
ACCEPT     all  --  anywhere             anywhere             /* cali:iX2AYvqGXaVqwkro */ mark match 0x10000/0x10000
MARK       all  --  anywhere             anywhere             /* cali:bhpnxD5IRtBP8KW0 */ MARK and 0xfff0ffff
cali-from-host-endpoint  all  --  anywhere             anywhere             /* cali:H5_bccAbHV0sooVy */
ACCEPT     all  --  anywhere             anywhere             /* cali:inBL01YlfurT0dbI */ /* Host endpoint policy accepted packet. */ mark match 0x10000/0x10000

Chain cali-from-hep-forward (1 references)
target     prot opt source               destination

Chain cali-cidr-block (1 references)
target     prot opt source               destination

Chain cali-forward-endpoint-mark (1 references)
target     prot opt source               destination
cali-from-endpoint-mark  all  --  anywhere             anywhere             /* cali:O0SmFDrnm7KggWqW */ mark match ! 0x100000/0xfff00000
cali-to-wl-dispatch  all  --  anywhere             anywhere             /* cali:aFl0WFKRxDqj8oA6 */
cali-to-hep-forward  all  --  anywhere             anywhere             /* cali:AZKVrO3i_8cLai5f */
MARK       all  --  anywhere             anywhere             /* cali:96HaP1sFtb-NYoYA */ MARK and 0xfffff
ACCEPT     all  --  anywhere             anywhere             /* cali:VxO6hyNWz62YEtul */ /* Policy explicitly accepted packet. */ mark match 0x10000/0x10000

Chain cali-from-wl-dispatch (2 references)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere             /* cali:zTj6P0TIgYvgz-md */ /* Unknown interface */

Chain cali-forward-check (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere             /* cali:Pbldlb4FaULvpdD8 */ ctstate RELATED,ESTABLISHED
cali-set-endpoint-mark  tcp  --  anywhere             anywhere            [goto]  /* cali:ZD-6UxuUtGW-xtzg */ /* To kubernetes NodePort service */ multiport dports 30000:32767 match-set cali40this-host dst
cali-set-endpoint-mark  udp  --  anywhere             anywhere            [goto]  /* cali:CbPfUajQ2bFVnDq4 */ /* To kubernetes NodePort service */ multiport dports 30000:32767 match-set cali40this-host dst
cali-set-endpoint-mark  all  --  anywhere             anywhere             /* cali:jmhU0ODogX-Zfe5g */ /* To kubernetes service */ ! match-set cali40this-host dst

Chain cali-set-endpoint-mark (3 references)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere             /* cali:MN61lcxFj1yWuYBo */ /* Unknown endpoint */
MARK       all  --  anywhere             anywhere             /* cali:nKOjq8N2yzfmS3jk */ /* Non-Cali endpoint mark */ MARK xset 0x100000/0xfff00000

Chain cali-from-endpoint-mark (1 references)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere             /* cali:9dpftzl-pNycbr37 */ /* Unknown interface */

Chain cali-from-host-endpoint (1 references)
target     prot opt source               destination

Chain cali-wl-to-host (1 references)
target     prot opt source               destination
cali-from-wl-dispatch  all  --  anywhere             anywhere             /* cali:Ee9Sbo10IpVujdIY */
ACCEPT     all  --  anywhere             anywhere             /* cali:nSZbcOoG1xPONxb8 */ /* Configured DefaultEndpointToHostAction */

Chain cali-to-host-endpoint (1 references)
target     prot opt source               destination

Chain cali-FORWARD (1 references)
target     prot opt source               destination
MARK       all  --  anywhere             anywhere             /* cali:vjrMJCRpqwy5oRoX */ MARK and 0xfff1ffff
cali-from-hep-forward  all  --  anywhere             anywhere             /* cali:A_sPAO0mcxbT9mOV */ mark match 0x0/0x10000
cali-from-wl-dispatch  all  --  anywhere             anywhere             /* cali:8ZoYfO5HKXWbB3pk */
cali-to-wl-dispatch  all  --  anywhere             anywhere             /* cali:jdEuaPBe14V2hutn */
cali-to-hep-forward  all  --  anywhere             anywhere             /* cali:12bc6HljsMKsmfr- */
cali-cidr-block  all  --  anywhere             anywhere             /* cali:NOSxoaGx8OIstr1z */