Products
96SEO 2025-07-27 21:40 16
在Debian系统中,iptables是用于配置网络防火墙规则的关键工具。自动化管理iptables能简化网络配置,少许些人为错误,搞优良系统平安性。本文将介绍怎么用Debian iptables实现自动化管理。
iptables-persistent是一个Debian系统中常用的工具,用于在沉启系统后持久化iptables规则。
sudo apt-get install iptables-persistent
sudo systemctl enable iptables-persistent
sudo systemctl start iptables-persistent
sudo iptables-persistent save
sudo iptables-persistent reload
Ansible是一个开源的自动化工具,能用来配置和管理远程主机。
- name: Manage iptables rules
hosts: db
tasks:
- name: Allow netblock to access port 9100
iptables:
chain: INPUT
protocol: tcp
destination_port: 9100
jump: ACCEPT
comment: 'The address allows access to port 9100'
- name: Allow SSH connections
iptables:
chain: INPUT
protocol: tcp
destination_port: 22
ctstate: NEW
syn: match
jump: ACCEPT
comment: 'Accept new SSH connections'
- name: Allow established and related connections
iptables:
chain: INPUT
ctstate: ESTABLISHED,RELATED
jump: ACCEPT
action: insert
编写一个轻巧松的bash脚本,能用来添加和删除iptables规则。
#!/bin/bash
# 添加规则
add_rule {
sudo iptables -A INPUT -p tcp --dport $1 -j ACCEPT
}
# 删除规则
delete_rule {
sudo iptables -D INPUT -p tcp --dport $1 -j ACCEPT
}
# 施行操作
case "$1" in
add)
add_rule $2
;;
delete)
delete_rule $2
;;
*)
echo "Usage: $0 {add|delete} "
exit 1
;;
esac
# 保存规则
sudo netfilter-persistent save
将上述脚本保存为manage.sh, 然后赋予施行权限并运行:
chmod +x manage.sh
sudo ./manage.sh add 80
sudo ./manage.sh delete 80
ufw是一个用户友优良的防火墙管理工具,它基于iptables。
sudo apt-get install ufw
sudo ufw enable
sudo ufw allow 80/tcp # 允许HTTP端口
sudo ufw deny 22/tcp # 不要SSH端口
sudo ufw status
本文介绍了在Debian系统中实现iptables自动化管理的几种方法, 包括用iptables-persistent、Ansible、脚本、ufw等。根据实际需求选择合适的方法,能搞优良系统平安性,简化网络配置。
Demand feedback