生产环境中常用的iptables脚本

iptables是用于监控进/出服务器流量的一个工具,iptables使用一个叫做table的结构,而这些tables包含了一系列规则(set of rules),我们称这些规则为chain,chain会过滤进/出服务器的数据包(data packets)。

创建 iptables.sh 脚本

[root@Jaking ~]# vim iptables.sh#!/bin/bash #清空 filter 表和 nat 表
iptables -F
iptables -t nat -F

#关掉 firewalld
systemctl stop firewalld &>/dev/null
systemctl disable firewalld &>/dev/null

#以下两行允许某些调用 localhost 的应用访问
iptables -A INPUT -i lo -j ACCEPT #规则1
iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT #规则2#以下一行允许从其他地方 ping
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT #规则3#以下一行允许从其他主机、网络设备发送 MTU 调整的报文#在一些情况下,例如通过 IPSec 科学 隧道时,主机的 MTU 需要动态减小
iptables -A INPUT -p icmp --icmp-type fragmentation-needed -j ACCEPT #规则4#以下两行分别允许所有来源访问 TCP 80,443 端口
iptables -A INPUT -p tcp --dport 80 -j ACCEPT #规则5
iptables -A INPUT -p tcp --dport 443 -j ACCEPT #规则6#以下一行允许所有来源访问 UDP 80,443 端口
iptables -A INPUT -p udp -m multiport --dports 80,443 -j ACCEPT #规则7#以下一行允许 192.168.1.63 来源的 IP 访问 TCP 22 端口(OpenSSH)
iptables -A INPUT -p tcp -s 192.168.1.63 --dport 22 -j ACCEPT #规则8#以下一行允许 192.168.1.3(发起SSH连接的系统对应网卡的IP) 来源的 IP 访问 TCP 22 端口(OpenSSH)#如果是在远程终端跑本脚本,最好开启以下一行以防被踢掉#另一种更加简便的方式:iptables -I INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp -s 192.168.1.3 --dport 22 -j ACCEPT #规则9#以下一行允许 192.168.1.26 来源的 IP 访问 UDP 161 端口(SNMP)
iptables -A INPUT -p udp -s 192.168.1.26 --dport 161 -j ACCEPT #规则10#配置 NAT#启用内核路由转发功能echo 1 > /proc/sys/net/ipv4/ip_forward
echo "net.ipv4.ip_forward = 1" > /etc/sysctl.conf
sysctl -p &>/dev/null

#配置源地址转换 SNAT#将 192.168.2.0/24 转换成 192.168.1.63
iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -j SNAT --to 192.168.1.63 #规则11#配置目的地址转换 DNAT#将 192.168.1.63 的 80 端口请求转发到 192.168.2.2 的 80 端口
iptables -t nat -A PREROUTING -d 192.168.1.63 -p tcp --dport 80 -j DNAT --to 192.168.2.2:80 #规则12#以下一行禁止所有其他的进入流量
iptables -A INPUT -j DROP #规则13#以下一行允许本机响应规则编号为 1-12 的数据包发出
iptables -A OUTPUT -m state --state ESTABLISHED -j ACCEPT #规则14#以下一行禁止本机主动发出外部连接
iptables -A OUTPUT -j DROP #规则15#以下一行禁止本机转发数据包
iptables -A FORWARD -j DROP #规则16#固化 iptables
iptables-save > /etc/sysconfig/iptables

[root@Jaking ~]# chmod 755 iptables.sh

测试

[root@Jaking ~]# ./iptables.sh
[root@Jaking ~]#
[root@Jaking ~]#
[root@Jaking ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination        
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  localhost            localhost          
ACCEPT     icmp --  anywhere             anywhere             icmp echo-request
ACCEPT     icmp --  anywhere             anywhere             icmp fragmentation-needed
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https
ACCEPT     udp  --  anywhere             anywhere             multiport dports http,https
ACCEPT     tcp  --  192.168.1.63         anywhere             tcp dpt:ssh
ACCEPT     tcp  --  192.168.1.3          anywhere             tcp dpt:ssh
ACCEPT     udp  --  192.168.1.26         anywhere             udp dpt:snmp
DROP       all  --  anywhere             anywhere            

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination        
DROP       all  --  anywhere             anywhere            

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination        
ACCEPT     all  --  anywhere             anywhere             state ESTABLISHED
DROP       all  --  anywhere             anywhere            
[root@Jaking ~]# iptables -L --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination        
1    ACCEPT     all  --  anywhere             anywhere            
2    ACCEPT     all  --  localhost            localhost          
3    ACCEPT     icmp --  anywhere             anywhere             icmp echo-request
4    ACCEPT     icmp --  anywhere             anywhere             icmp fragmentation-needed
5    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
6    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https
7    ACCEPT     udp  --  anywhere             anywhere             multiport dports http,https
8    ACCEPT     tcp  --  192.168.1.63         anywhere             tcp dpt:ssh
9    ACCEPT     tcp  --  192.168.1.3          anywhere             tcp dpt:ssh
10   ACCEPT     udp  --  192.168.1.26         anywhere             udp dpt:snmp
11   DROP       all  --  anywhere             anywhere            

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination        
1    DROP       all  --  anywhere             anywhere            

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination        
1    ACCEPT     all  --  anywhere             anywhere             state ESTABLISHED
2    DROP       all  --  anywhere             anywhere            
[root@Jaking ~]# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination        
DNAT       tcp  --  anywhere             192.168.1.63         tcp dpt:http to:192.168.2.2:80

Chain INPUT (policy ACCEPT)
target     prot opt source               destination        

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination        

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination        
SNAT       all  --  192.168.2.0/24       anywhere             to:192.168.1.63
[root@Jaking ~]# iptables -t nat -L --line-number
Chain PREROUTING (policy ACCEPT)
num  target     prot opt source               destination        
1    DNAT       tcp  --  anywhere             192.168.1.63         tcp dpt:http to:192.168.2.2:80

Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination        

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination        

Chain POSTROUTING (policy ACCEPT)
num  target     prot opt source               destination        
1    SNAT       all  --  192.168.2.0/24       anywhere             to:192.168.1.63

iptables 的清空和恢复

[root@Jaking ~]# iptables -F
[root@Jaking ~]# 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@Jaking ~]# iptables -t nat -F
[root@Jaking ~]# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination        

Chain INPUT (policy ACCEPT)
target     prot opt source               destination        

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination        

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination
[root@Jaking ~]# iptables-restore  [root@Jaking ~]# iptables -L Chain INPUT (policy ACCEPT) target     prot opt source               destination         ACCEPT     all  --  anywhere             anywhere             ACCEPT     all  --  localhost            localhost           ACCEPT     icmp --  anywhere             anywhere             icmp echo-request ACCEPT     icmp --  anywhere             anywhere             icmp fragmentation-needed ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https ACCEPT     udp  --  anywhere             anywhere             multiport dports http,https ACCEPT     tcp  --  192.168.1.63         anywhere             tcp dpt:ssh ACCEPT     tcp  --  192.168.1.3          anywhere             tcp dpt:ssh ACCEPT     udp  --  192.168.1.26         anywhere             udp dpt:snmp DROP       all  --  anywhere             anywhere             Chain FORWARD (policy ACCEPT) target     prot opt source               destination         DROP       all  --  anywhere             anywhere             Chain OUTPUT (policy ACCEPT) target     prot opt source               destination         ACCEPT     all  --  anywhere             anywhere             state ESTABLISHED DROP       all  --  anywhere             anywhere             [root@Jaking ~]# iptables -t nat -L Chain PREROUTING (policy ACCEPT) target     prot opt source               destination         DNAT       tcp  --  anywhere             192.168.1.63         tcp dpt:http to:192.168.2.2:80 Chain INPUT (policy ACCEPT) target     prot opt source               destination         Chain OUTPUT (policy ACCEPT) target     prot opt source               destination         Chain POSTROUTING (policy ACCEPT) target     prot opt source               destination         SNAT       all  --  192.168.2.0/24       anywhere             to:192.168.1.63 

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/220586.html<

(0)
运维的头像运维
上一篇2025-04-14 15:36
下一篇 2025-04-14 15:38

相关推荐

  • 个人主题怎么制作?

    制作个人主题是一个将个人风格、兴趣或专业领域转化为视觉化或结构化内容的过程,无论是用于个人博客、作品集、社交媒体账号还是品牌形象,核心都是围绕“个人特色”展开,以下从定位、内容规划、视觉设计、技术实现四个维度,详细拆解制作个人主题的完整流程,明确主题定位:找到个人特色的核心主题定位是所有工作的起点,需要先回答……

    2025-11-20
    0
  • 社群营销管理关键是什么?

    社群营销的核心在于通过建立有温度、有价值、有归属感的社群,实现用户留存、转化和品牌传播,其管理需贯穿“目标定位-内容运营-用户互动-数据驱动-风险控制”全流程,以下从五个维度展开详细说明:明确社群定位与目标社群管理的首要任务是精准定位,需明确社群的核心价值(如行业交流、产品使用指导、兴趣分享等)、目标用户画像……

    2025-11-20
    0
  • 香港公司网站备案需要什么材料?

    香港公司进行网站备案是一个涉及多部门协调、流程相对严谨的过程,尤其需兼顾中国内地与香港两地的监管要求,由于香港公司注册地与中国内地不同,其网站若主要服务内地用户或使用内地服务器,需根据服务器位置、网站内容性质等,选择对应的备案路径(如工信部ICP备案或公安备案),以下从备案主体资格、流程步骤、材料准备、注意事项……

    2025-11-20
    0
  • 如何企业上云推广

    企业上云已成为数字化转型的核心战略,但推广过程中需结合行业特性、企业痛点与市场需求,构建系统性、多维度的推广体系,以下从市场定位、策略设计、执行落地及效果优化四个维度,详细拆解企业上云推广的实践路径,精准定位:明确目标企业与核心价值企业上云并非“一刀切”的方案,需先锁定目标客户群体,提炼差异化价值主张,客户分层……

    2025-11-20
    0
  • PS设计搜索框的实用技巧有哪些?

    在PS中设计一个美观且功能性的搜索框需要结合创意构思、视觉设计和用户体验考量,以下从设计思路、制作步骤、细节优化及交互预览等方面详细说明,帮助打造符合需求的搜索框,设计前的规划明确使用场景:根据网站或APP的整体风格确定搜索框的调性,例如极简风适合细线条和纯色,科技感适合渐变和发光效果,电商类则可能需要突出搜索……

    2025-11-20
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注