Simple IPTables Firewall with Whitelist and Blacklist

本文介绍了一个简单的IPTables防火墙脚本,可用于CentOS v6和Ubuntu v12等系统。该脚本包括了白名单和黑名单功能,并允许自定义开放端口。文章还提供了设置步骤和注意事项。

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

The following is a simple IPTables firewall script that can be used for general purposes.  It includes a port list and whitelist/blacklist.  The script was tested on CentOS v6 and Ubuntu v12.

Create the whitelist & blacklist files

These can remain empty until needed.

# touch /usr/local/etc/whitelist.txt
# touch /usr/local/etc/blacklist.txt

Enter one IP or domain per line as needed to permit or deny.  For example, to permit 1.1.1.1 and somedomain.com

# nano /usr/local/etc/whitelist.txt
1.1.1.1
​somedomain.com

Note about DNS domains and iptables.

If your whitlist specifies a domain it is the DNS resolved IP address that is added to the ipables rule.  So any change in the IP address of a domain in a whitelist or blacklist will require the firewall script to be re-run.

Create the firewall script

Located IPtables on your distribution and alter the IPTABLES= line in the script accordingly.

# which iptables
# which iptables-save

For non standard SSH port and to allow or deny other ports alter ALLOWED= line accordingly

# nano /usr/local/etc/firewall.sh
#!/bin/bash
#
## Simple IPTables Firewall with Whitelist & Blacklist
#
## List Locations
#

WHITELIST=/usr/local/etc/whitelist.txt
BLACKLIST=/usr/local/etc/blacklist.txt

#
## Specify ports you wish to use.
## For port listing reference see http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
#

ALLOWED="22 25 53 80 443 465 587 993"

#
## Specify where IP Tables is located
#

IPTABLES=/sbin/iptables
IPTABLES_SAVE=/sbin/iptables-save

#
## Save current iptables running configuration in case we want to revert back
## To restore using our example we would run "/sbin/iptables-restore < /usr/src/iptables.last"
#

$IPTABLES_SAVE > /usr/local/etc/iptables.last

#
## Clear current rules
#
## If current INPUT policy is set to DROP we will be locked out once we flush the rules
## so we must first ensure it is set to ACCEPT.
#
$IPTABLES -P INPUT ACCEPT
echo 'Setting default INPUT policy to ACCEPT'

$IPTABLES -F
echo 'Clearing Tables F'
$IPTABLES -X
echo 'Clearing Tables X'
$IPTABLES -Z
echo 'Clearing Tables Z'

#Always allow localhost.
echo 'Allowing Localhost'
$IPTABLES -A INPUT -s 127.0.0.1 -j ACCEPT

#
## Whitelist
#

for x in `grep -v ^# $WHITELIST | awk '{print $1}'`; do
echo "Permitting $x..."
$IPTABLES -A INPUT -s $x -j ACCEPT
done

#
## Blacklist
#

for x in `grep -v ^# $BLACKLIST | awk '{print $1}'`; do
echo "Denying $x..."
$IPTABLES -A INPUT -s $x -j DROP
done

#
## Permitted Ports
#

for port in $ALLOWED; do
echo "Accepting port TCP $port..."
$IPTABLES -A INPUT -p tcp --dport $port -j ACCEPT
done

for port in $ALLOWED; do
echo "Accepting port UDP $port..."
$IPTABLES -A INPUT -p udp --dport $port -j ACCEPT
done

#
##The following rule ensures that replies are not blocked.
##It also allows for things that may be related but not part of those connections such as ICMP.
#

$IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

#
## NOTE: Test this script first to make sure it works as expected.
## Run "iptables -v -n -L" to ensure the rules are as expected and that your SSH port is correct.
##
## When you are sure this script works properly uncomment the following 2 lines to enforce the rules.
#

# $IPTABLES -A INPUT -p udp -j DROP
# $IPTABLES -A INPUT -p tcp --syn -j DROP

#
## Save the rules so they are persistent on reboot.
#
/etc/init.d/iptables save
Make the script executable and run.
# chmod +x /usr/local/etc/firewall.sh
# /usr/local/etc/firewall.sh点击打开链接
Check rules.
​# iptables -v -n -L

Once you are sure the script is working properly with the proper SSH port allowed you can uncommend the two lines at the bottom of the script and run again to fully enable it.

Ubuntu iptables persistence

If using Ubuntu, install the persistent package.

# apt-get install iptables-persistent 

Change the last line in the firewall script to:

$IPTABLES_SAVE > /etc/iptables/rules.v4
这个错误通常是由于iptables配置文件中存在错误或者iptables服务未正确安装导致的。你可以尝试运行以下命令来检查iptables服务是否已正确安装: ``` sudo systemctl status iptables ``` 如果该命令返回“Unit iptables.service could not be found”或者“iptables.service loaded failed”,则说明iptables服务未正确安装。你可以使用以下命令来安装iptables服务: ``` sudo yum install iptables-services ``` 安装完成后,你可以使用以下命令来启动iptables服务: ``` sudo systemctl start iptables ``` 如果该命令返回“Failed to start iptables.service: Unit iptables.service not found”错误,则你需要手动创建iptables.service文件。你可以使用文本编辑器打开/etc/systemd/system/iptables.service文件,并将以下内容复制到文件中: ``` [Unit] Description=IPv4 firewall with iptables After=syslog.target network.target [Service] Type=oneshot ExecStart=/usr/libexec/iptables/iptables.init start ExecStop=/usr/libexec/iptables/iptables.init stop [Install] WantedBy=multi-user.target ``` 保存文件后,使用以下命令来重新加载systemd服务并启动iptables服务: ``` sudo systemctl daemon-reload sudo systemctl start iptables ``` 如果iptables服务启动仍然失败,请尝试查看系统日志以获取更多信息: ``` sudo tail /var/log/messages ``` 该命令将显示iptables服务启动失败的详细信息,你可以根据提示进行进一步的排查。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值