Ubuntu中保存iptables防火墙规则的例子
系统大全为您提供
ubuntu下保存iptables规则并开机自动加载的方法:
Saving iptables 保存设置
机器重启后,iptables中的配置信息会被清空。您可以将这些配置保存下来,让iptables在启动时自动加载,省得每次都得重新输入。iptables-save和iptables-restore 是用来保存和恢复设置的。
Configuration onstartup 开机自动加载配置
先将防火墙规则保存到/etc/iptables.up.rules文件中
# iptables-save > /etc/iptables.up.rules #需要sudo su - root切换用户后执行,直接sudo cmd是不行的
Then modify the/etc/network/interfacesscript to apply the rulesautomatically (the bottom line is added)
然后修改脚本/etc/network/interfaces,使系统能自动应用这些规则(最后一行是我们手工添加的)。
auto eth0
iface eth0 inet dhcp
pre-up iptables-restore < /etc/iptables.up.rules
当网络接口关闭后,您可以让iptables使用一套不同的规则集。
auto eth0
iface eth0 inet dhcp
pre-up iptables-restore < /etc/iptables.up.rules
post-down iptables-restore < /etc/iptables.down.rules
Tips 技巧
如果你经常手动编辑iptables
大多数人并不需要经常改变他们的防火墙规则,因此只要根据前面的介绍,建立起防火墙规则就可以了。但是如果您要经常修改防火墙规则,以使其更加完善,那么您可能希望系统在每次重启前将防火墙的设置保存下来。为此您可以在/etc/network/interfaces文件中添加一行:
pre-up iptables-restore < /etc/iptables.up.rules
post-down iptables-save > /etc/iptables.up.rules
The line "post-down iptables-save > /etc/iptables.up.rules"will save the rules to be used on the next boot.
"post-down iptables-save > /etc/iptables.up.rules"会将设置保存下来,以便下次启动时使用。
Usingiptables-save/restore to test rules 使用iptables-save/restore测试规则
使用iptables-save和iptables-restore可以很方便地修改和测试防火墙规则。首先运行iptables-save将规则保存到一个文件,然后用编辑器编辑该文件。
# iptables-save > /etc/iptables.test.rules
# gedit /etc/iptables.test.rules
如果您根据前面的例子建立了防火墙规则,iptables-save将产生一个类似于如下内容的文件:
# Generated by iptables-save v1.3.1 on Sun Apr 2306:19:53 2006
*filter
:INPUT ACCEPT [368:102354]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [92952:20764374]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m limit --limit 5/min -j LOG --log-prefix"iptables denied: " --log-level 7
-A INPUT -j Drop
COMMIT
# Completed on Sun Apr 23 06:19:53 2006
文件内容其实就是各种iptables命令,只不过把命令名iptables省略了。您可以随意对这个文件进行编辑,然后保存。接着使用以下命令测试修改后的规则:
# iptables-restore < /etc/iptables.test.rules
之前您如果没有在/etc/network/interfaces文件中添加iptables-save命令,那么测试之后,别忘了把您所作的修改保存起来。
# iptables-save > /etc/iptables.up.rules
补充:iptables防火墙规则的备份与恢复
首先,保存iptables规则到一个文件
# sudo iptables-save > /etc/iptables.up.rules
然后,修改 /etc/network/interfaces 脚本自动应用这些规则(末行是添加的)
sudo echo "pre-up iptables-restore < /etc/iptables.up.rules" >> /etc/network/interfaces
也可以准备一组规则来自动应用它:
首先,保存iptables规则到一个文件
# sudo iptables-save > /etc/iptables.up.rules
然后,修改 /etc/network/interfaces 脚本自动应用这些规则(末行是添加的)
sudo echo "pre-up iptables-restore < /etc/iptables.up.rules" >> /etc/network/interfaces
也可以准备一组规则来自动应用它:
sudo echo " pre-up iptables-restore < /etc/iptables.up.rules" >>/etc/network/interfaces
sudo echo "post-down iptables-restore < /etc/iptables.down.rules " >> /etc/network/interfaces
上述方法在 Ubuntu 12.04 已测试过,生效。
sudo echo " pre-up iptables-restore < /etc/iptables.up.rules" >>/etc/network/interfaces
sudo echo "post-down iptables-restore < /etc/iptables.down.rules " >> /etc/network/interfaces
上述方法在 Ubuntu 12.04 已测试过,生效。
以上就是系统大全给大家介绍的如何使的方法都有一定的了解了吧,好了,如果大家还想了解更多的资讯,那就赶紧点击系统大全官网吧。
本文来自系统大全http://www.win7cn.com/如需转载请注明!推荐:win7纯净版
系统下载排行榜71011xp
番茄花园 Win7 64位 快速稳定版
2【纯净之家】Windows7 64位 最新纯净版
3【老机专用】Windows7 32位 精简旗舰版
4【纯净之家】Windows7 SP1 64位 全补丁旗舰版
5JUJUMAO Win7 64位 最新纯净版
6【纯净之家】Windows7 32位 最新纯净版
7【纯净之家】Windows7 SP1 64位 办公旗舰版
8【雨林木风】Windows7 64位 装机旗舰版
9【电脑公司】Windows7 64位 免费旗舰版
10【国庆献礼】JUJUMAO Win7 SP1 64位旗舰克隆纯净版
【纯净之家】Windows10 22H2 64位 专业工作站版
2【纯净之家】Windows10 22H2 64位 游戏优化版
3【纯净之家】Windows10 企业版 LTSC 2021
4【纯净之家】Windows10企业版LTSC2021 纯净版
5【深度技术】Windows10 64位 专业精简版
6联想Lenovo Windows10 22H2 64位专业版
7JUJUMAO Win8.1 Up3 X64 极速精简版v2019.11
8【纯净之家】Windows10 22H2 64位 专业精简版
9【雨林木风】Windows10 64位 专业精简版
10【纯净之家】Windows10 22H2 64位 专业办公版
【纯净之家】Windows11 23H2 64位 游戏优化版
2【纯净之家】Windows11 23H2 64位 专业工作站版
3【纯净之家】Windows11 23H2 64位 纯净专业版
4风林火山Windows11下载中文版
5【纯净之家】Windows11 23H2 64位 最新企业版
6【纯净之家】Windows11 23H2 64位 中文家庭版
7JUJUMAO Win11 24H2 64位 专业版镜像
8深度精简 Win11 ESD 全新优化纯净版V2024
9【纯净之家】Windows11 23H2 64位专业精简版
10【纯净之家】Windows11 23H2 64位 纯净家庭版
深度技术 GHOST XP SP3 电脑专用版 V2017.03
2深度技术 GGHOST XP SP3 电脑专用版 V2017.02
3Win7系统下载 PCOS技术Ghost WinXP SP3 2017 夏季装机版
4萝卜家园 GHOST XP SP3 万能装机版 V2017.03
5番茄花园 GHOST XP SP3 极速体验版 V2017.03
6雨林木风 GHOST XP SP3 官方旗舰版 V2017.03
7电脑公司 GHOST XP SP3 经典旗舰版 V2017.03
8雨林木风GHOST XP SP3完美纯净版【V201710】已激活
9萝卜家园 GHOST XP SP3 完美装机版 V2016.10
10雨林木风 GHOST XP SP3 精英装机版 V2017.04
热门教程
装机必备 更多+
重装工具