Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iptables 和它的周边们 #199

Open
Bpazy opened this issue Sep 14, 2021 · 1 comment
Open

iptables 和它的周边们 #199

Bpazy opened this issue Sep 14, 2021 · 1 comment

Comments

@Bpazy
Copy link
Owner

Bpazy commented Sep 14, 2021

为什么 netfilter-persistent 不生效?

因为只安装 netfilter-persistent 没用,必须要另外安装 iptables-persistent
而安装 iptables-persistent 时会自动安装 netfilter-persistent

所以如果你想用 netfilter-persistent 命令,则运行以下命令安装即可:

sudo apt install iptables-persistent
@Bpazy
Copy link
Owner Author

Bpazy commented Sep 14, 2021

怎么保存 iptables 规则?

方案 1: netfilter-persistent

sudo apt install iptables-persistent
sudo netfilter-persistent save

此时规则会被保存到 /etc/iptables/rules.v4 中,重启后会自动加载。

方案 2: iptables-save

# 保存 iptables 规则
sudo iptables-save > /etc/iptables/rules
# 读取规则并写入 iptables
sudo iptables-restore < /etc/iptables/rule

手动操作比较麻烦,这里可以利用 /etc/network/interfaces 配置文件,实现网卡加载后自动载入 iptables 配置:编辑 /etc/network/interfaces,最最后添加一些内容:

pre-up iptables-restore < /etc/iptables/rules.v4

但是 ubuntu 20 利用 /etc/network/interfaces 的方法已经玩不转了,所以这个方法并不推荐使用。

方案 3: 自己实现一个 systemd 的 service

开个玩笑,哈哈哈,自己写一套脚本,其实最终就是 netfilter-persistent,直接用方案 1 吧。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant