iptables设置

为了环境的整洁,将Flask部署在了lxc中,但在外部无法直接访问到lxc-container内部,因此需要添加iptables规则,以使得外部可以访问:

添加规则

假定lxc-container的ip为10.0.3.86,开放的端口为8888;而宿主机ip为192.168.142.133,对外开放的端口为3389,则对应的iptables规则为:

iptables -t nat -I PREROUTING -p tcp -d 192.168.142.133 --dport 3389 -j DNAT --to 10.0.3.86:8888

查看规则

使用如下命令查看添加的规则:

$ iptables -t nat -L --line-number
Chain PREROUTING (policy ACCEPT)
num  target     prot opt source               destination         
1    DNAT       tcp  --  anywhere             ubuntu               tcp dpt:3389 to:10.0.3.86:8888

Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
num  target     prot opt source               destination         
1    MASQUERADE  all  --  10.0.3.0/24         !10.0.3.0/24

可以看到链PREROUTING中策略ACCEPT中的第一条规则即为刚才添加的规则。

删除规则

根据上边的结论,添加的规则编号为1,则删除规则如下:

iptables -t nat -D PREROUTING 1

此时刚才添加的规则被删除。

你可能感兴趣的:(iptables设置)