2023年11月19日星期日

ubuntu22作为openvpn客户端,设置防火墙给其他主机使用,并链接上外网

1.在openvpn已经链接上的情况下。

设置openvpn的主机的sysctl.conf文件,使得ip可以转发。设置如下:

net.ipv4.ip_forward = 1

net.ipv6.conf.all.forwarding = 1

net.ipv6.conf.default.forwarding = 1

然后sysctl -p生效

iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

就是接收从10.8.0.0子网来的信息。否则在客户端(ubuntu22)无法ping通外网。

保持设置,并永久生效:

iptables-save > /etc/iptables/rules.v4

2、在ubuntu22本地

sysctl.conf文件:

net.ipv4.ip_forward = 1

net.ipv6.conf.all.forwarding=1

net.ipv6.conf.default.forwarding=1

然后sysctl -p生效

iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o tun0 -j MASQUERADE

如果有另外一个网络,例如10.8.0.0/24,那么需要增加一条命令:

iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o tun0 -j MASQUERADE

接收来自192.168.1.0子网的流量。

保持设置,并永久生效:

iptables-save > /etc/iptables/rules.v4


这时候,设置192.168.1.0中的子网主机,只要将网关设置为ubuntu22的地址如192.168.1.2,则可以ping通外网了。


最后抓包:

tcpdump -i ens160 dst host 142.251.130.4


对于ipv6 需要设置,在服务器上需要:
sudo ip6tables -t nat -A POSTROUTING -s fd00:0:0:1::/64 -o eth0 -j MASQUERADE

这条 ip6tables 命令的作用是 对从 OpenVPN 客户端(tun0)流出的 IPv6 流量进行 NAT(地址伪装),使其看起来像是来自服务器的 eth0 网卡。

下面这两行好像是不需要的:

sudo ip6tables -A FORWARD -i tun0 -j ACCEPT

sudo ip6tables -A FORWARD -o tun0 -j ACCEPT

保存:

ip6tables-save > /etc/iptables/rules.v6

在client上,需要在client.conf上增加:
route-ipv6 2000::/3




没有评论:

发表评论

安装 PaddleOCR的方法

  安装飞桨版本:https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/develop/install/pip/windows-pip.html 命令:  python -m pip...