0%

debian系统openvpn组网双向互访

openvpn搭建,双向内网段互访,TCP组网,杜绝UDP中断。

注意!!!

openvpn需要tun,如采用lxc方式,需要自行把tun挂到lxc内。

1
2
lxc.cgroup2.devices.allow: c 10:200 rwm
lxc.mount.entry: /dev/net/tun dev/net/tun none bind,create=file

一键脚本做openvpn配置

采用 一键脚本 部署,脚本开源,14K Start 安全可靠。

部署前确定能访问GitHub。

1
2
3
curl -O https://raw.githubusercontent.com/angristan/openvpn-install/master/openvpn-install.sh
chmod +x openvpn-install.sh
./openvpn-install.sh

服务端配置

1
2
3
4
5
6
7
8
9
10
Welcome to the OpenVPN installer!
The git repository is available at: https://github.com/angristan/openvpn-install

I need to ask you a few questions before starting the setup.
You can leave the default options and just press enter if you are ok with them.

I need to know the IPv4 address of the network interface you want OpenVPN listening to.
Unless your server is behind NAT, it should be your public IPv4 address.
IP address: 192.168.2.118
此处填IPV4公网IP或者域名
1
2
3
4
5
6
Checking for IPv6 connectivity...

Your host does not appear to have IPv6 connectivity.

Do you want to enable IPv6 support (NAT)? [y/n]: n
IPV6支持,有IPV4的话,可不开启
1
2
3
4
5
6
What port do you want OpenVPN to listen to?
1) Default: 1194
2) Custom
3) Random [49152-65535]
Port choice [1-3]: 3
设置端口,不建议默认端口,可自定义或者随机
1
2
3
4
5
6
What protocol do you want OpenVPN to use?
UDP is faster. Unless it is not available, you shouldn't use TCP.
1) UDP
2) TCP
Protocol [1-2]: 2
选择UDP还是TCP连接,国内环境建议TCP,不用担心被阻断
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
What DNS resolvers do you want to use with the VPN?
1) Current system resolvers (from /etc/resolv.conf)
2) Self-hosted DNS Resolver (Unbound)
3) Cloudflare (Anycast: worldwide)
4) Quad9 (Anycast: worldwide)
5) Quad9 uncensored (Anycast: worldwide)
6) FDN (France)
7) DNS.WATCH (Germany)
8) OpenDNS (Anycast: worldwide)
9) Google (Anycast: worldwide)
10) Yandex Basic (Russia)
11) AdGuard DNS (Anycast: worldwide)
12) NextDNS (Anycast: worldwide)
13) Custom
DNS [1-12]: 13
设置下发到客户端的dns,可以13自定义
1
2
3
Do you want to use compression? It is not recommended since the VORACLE attack makes use of it.
Enable compression? [y/n]: n
是否开启压缩,人家说不安全,那就默认不开完事
1
2
3
4
5
6
7
Do you want to customize encryption settings?
Unless you know what you're doing, you should stick with the default parameters provided by the script.
Note that whatever you choose, all the choices presented in the script are safe. (Unlike OpenVPN's defaults)
See https://github.com/angristan/openvpn-install#security-and-encryption to learn more.

Customize encryption settings? [y/n]: n
是否自定义加密设置,基本不需要,脚本默认的加密选项已经很高了
1
2
3
4
Okay, that was all I needed. We are ready to setup your OpenVPN server now.
You will be able to generate a client at the end of the installation.
Press any key to continue...
服务端配置完事了,回车等待安装完成即可!确保网络畅通

客户端配置

添加第一个客户端

服务端配置完成脚本会自动开始创建第一个客户端

1
2
3
4
Tell me a name for the client.
The name must consist of alphanumeric character. It may also include an underscore or a dash.
Client name:test
输入第一个客户端的名称
1
2
3
4
5
6
Do you want to protect the configuration file with a password?
(e.g. encrypt the private key with a password)
1) Add a passwordless client
2) Use a password for the client
Select an option [1-2]: 1
是否密码加密配置文件,基本不需要吧?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Notice
------
Keypair and certificate request completed. Your files are:
req: /etc/openvpn/easy-rsa/pki/reqs/test.req
key: /etc/openvpn/easy-rsa/pki/private/test.key
Using configuration from /etc/openvpn/easy-rsa/pki/0039a394/temp.89eaf293
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName :ASN.1 12:'test'
Certificate is to be certified until Jan 18 02:02:55 2035 GMT (3650 days)

Write out database with 1 new entries
Database updated

Notice
------
Certificate created at:
* /etc/openvpn/easy-rsa/pki/issued/test.crt

Notice
------
Inline file created:
* /etc/openvpn/easy-rsa/pki/inline/test.inline
Client test added.

The configuration file has been written to /root/test.ovpn.
Download the .ovpn file and import it in your OpenVPN client.
部署完成!

部署完成之后会在当前路径多出两个文件openvpn-install.sh、test.ovpn,openvpn-install.sh是一键脚本,test.ovpn是客户端配置文件。

1
2
root@openvpn:~# ls
openvpn-install.sh test.ovpn

另外再添加更多客户端

只需输入 ./openvpn-install.sh

1
2
3
4
5
6
7
8
9
10
11
12
root@openvpn:~# ./openvpn-install.sh
Welcome to OpenVPN-install!
The git repository is available at: https://github.com/angristan/openvpn-install

It looks like OpenVPN is already installed.

What do you want to do?
1) Add a new user
2) Revoke existing user
3) Remove OpenVPN
4) Exit
Select an option [1-4]: 1
  1. 添加新用户
  2. 移除指定用户
  3. 删除整个openvpn
  4. 退出脚本

创建客户端操作和上面一样,操作完成之后路径下会多出客户端配置文件

1
2
root@openvpn:~# ls
openvpn-install.sh test2.ovpn test.ovpn

客户端配置放到所需机器上使用即可!

debian命令行启动openvpn客户端

安装openvpn

1
apt install openvpn -y

放入配置文件

把配置文件放到 /etc/openvpn/ 路径下,并且改名为 .conf 结尾

1
mv test.ovpn /etc/openvpn/test.conf

启动openvpn客户端

命令中的test对应为配置文件的名称

1
2
systemctl start openvpn@test
systemctl enable openvpn@test

另外服务端访问客户端内网需要添加防火墙规则,看下文。

组网配置

openvpn需要iptables防火墙,如没有则安装

1
apt install iptables -y

修改配置使客户端可访问服务端内网

1
2
cd /etc/openvpn/
nano server.conf

这行建议注释掉,意思为把客户端的所有流量全局走vpn

1
push "redirect-gateway def1 bypass-dhcp"

更换为,中间是服务端内网段,这样只有直接访问服务端内网IP才会走vpn

1
push "route 192.168.2.0 255.255.255.0"

有更多网段需求可以以上方式增加更多网段路由。

修改配置使服务端可访问客户端内网

服务端配置文件添加一行,意思这个网段走vpn

1
route 192.168.3.0 255.255.255.0

在/etc/openvpn/ccd路径下创建并编辑客户端名称的文件

1
nano /etc/openvpn/ccd/test # 我的客户端名称为test,所以写test

填入以下内容,修改中间网段为客户端网段

1
iroute 192.168.3.0 255.255.255.0

客户端 添加防火墙规则

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

一个是服务端内网段,一个是vpn网段,修改为自己网段和网卡名。

以上步骤缺一不可,否则服务端无法访问客户端内网段。

客户端防火墙规则持久化

采用iptables-persistent持久化防火墙规则

1
2
3
4
apt install iptables-persistent # 安装 iptables-persistent
systemctl start netfilter-persistent # 启动 iptables-persistent
systemctl enable netfilter-persistent # 允许 iptables-persistent 开机自启
iptables-save > /etc/iptables/rules.v4 # 保存防火墙规则

iptables防火墙可能用到的命令

1
2
3
4
5
6
iptables -t nat -L -n -v
# 查看 nat 表防火墙规则
iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -o eth0 -j MASQUERADE
# 添加IP动态伪装规则
iptables -t nat -D POSTROUTING -o eth0 -s 192.168.2.0/24 -j MASQUERADE
# 删除 nat 表某规则