一个简单实用的iptables设置脚本

本文介绍如何使用iptables命令来配置防火墙规则,包括删除现有规则、设置默认规则以及开放特定端口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

echo "------------------ iptables firewall seting ------------------";
echo "old settings are:"
iptables -L -n

echo "seting now..."
iptables -F     # delete filters in input,output,forward
iptables -X     # delete filter in user's chain

iptables -P INPUT DROP  # INPUT 's default to DROP
iptables -P FORWARD DROP # FORWARD 's default to DROP
iptables -P OUTPUT ACCEPT # OUTPUT 's default to ACCEPT

# this host as server
iptables -A INPUT -p tcp --dport 22 -j ACCEPT   # sshd service
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT # mysqld service
iptables -A INPUT -p tcp --dport 80 -j ACCEPT   # httpd service

# this host as client
iptables -A INPUT -p icmp -j ACCEPT             # ping
iptables -A INPUT -p udp --sport 53 -j ACCEPT   # dns
iptables -A INPUT -p tcp --sport 80 -j ACCEPT   # web

echo "setting over, new settings are:"
iptables -L -n

echo "save new settings to /etc/sysconfig/iptables."
service iptables save

下面是一个简单iptables脚本,可以阻止所有入站流量,允许所有出站流量: ``` #!/bin/bash # 清空所有规则 iptables -F # 设置默认策略 iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT ACCEPT # 允许回环接口(localhost) iptables -A INPUT -i lo -j ACCEPT iptables -A OUTPUT -o lo -j ACCEPT # 允许已经建立的连接 iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # 允许ssh连接 iptables -A INPUT -p tcp --dport 22 -j ACCEPT # 允许ping iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT # 允许HTTP和HTTPS连接 iptables -A INPUT -p tcp --dport 80 -j ACCEPT iptables -A INPUT -p tcp --dport 443 -j ACCEPT # 防止DOS攻击 iptables -A INPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT # 允许DNS查询 iptables -A INPUT -p udp --dport 53 -j ACCEPT iptables -A INPUT -p tcp --dport 53 -j ACCEPT # 允许NTP时间同步 iptables -A INPUT -p udp --dport 123 -j ACCEPT # 允许SMTP邮件 iptables -A INPUT -p tcp --dport 25 -j ACCEPT # 允许POP3邮件 iptables -A INPUT -p tcp --dport 110 -j ACCEPT # 允许IMAP邮件 iptables -A INPUT -p tcp --dport 143 -j ACCEPT # 允许FTP连接 iptables -A INPUT -p tcp --dport 20:21 -j ACCEPT # 允许MySQL连接 iptables -A INPUT -p tcp --dport 3306 -j ACCEPT # 允许SSH连接 iptables -A INPUT -p tcp --dport 22 -j ACCEPT # 允许Git连接 iptables -A INPUT -p tcp --dport 9418 -j ACCEPT # 允许SVN连接 iptables -A INPUT -p tcp --dport 3690 -j ACCEPT # 允许Redis连接 iptables -A INPUT -p tcp --dport 6379 -j ACCEPT # 允许Memcached连接 iptables -A INPUT -p tcp --dport 11211 -j ACCEPT # 允许MongoDB连接 iptables -A INPUT -p tcp --dport 27017 -j ACCEPT # 允许Elasticsearch连接 iptables -A INPUT -p tcp --dport 9200 -j ACCEPT iptables -A INPUT -p tcp --dport 9300 -j ACCEPT # 允许Zookeeper连接 iptables -A INPUT -p tcp --dport 2181 -j ACCEPT # 允许Kafka连接 iptables -A INPUT -p tcp --dport 9092 -j ACCEPT # 允许Kibana连接 iptables -A INPUT -p tcp --dport 5601 -j ACCEPT # 允许RabbitMQ连接 iptables -A INPUT -p tcp --dport 5672 -j ACCEPT # 允许Zabbix连接 iptables -A INPUT -p tcp --dport 10050 -j ACCEPT # 允许Prometheus连接 iptables -A INPUT -p tcp --dport 9090 -j ACCEPT # 允许Grafana连接 iptables -A INPUT -p tcp --dport 3000 -j ACCEPT # 允许Jenkins连接 iptables -A INPUT -p tcp --dport 8080 -j ACCEPT # 允许Docker连接 iptables -A INPUT -p tcp --dport 2375 -j ACCEPT iptables -A INPUT -p tcp --dport 2376 -j ACCEPT # 保存规则 iptables-save > /etc/sysconfig/iptables ``` 这个脚本假定你的系统默认是没有任何防火墙规则的。如果你已经有一些防火墙规则,那么需要根据实际情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值