在一台服务器上同时使用多个 OpenVPN

1,752次阅读
没有评论

您可以在同一台服务器上同时使用多个 OpenVPN 进程,而它们将在不同的端口上工作,并提供彼此不重叠的独立虚拟专用网络。

开箱即用地提供了多个 OpenVPN 实例,但需要额外的配置。

1.OpenVPN服务必须使用不同的端口

所有 OpenVPN 服务都必须使用空闲端口。记住相同的UDP和TCP端口号是不同的端口,也就是说,你可以在OpenVPN的两个实例中使用相同的端口号,前提是其中一个是UDP端口,另一个是TCP端口。

如果两个 OpenVPN 服务使用同一个端口,那么第一个启动的 OpenVPN 实例将正常工作,第二个实例将不会启动,因为错误“TCP/UDP:本地地址上的套接字绑定失败 [AF_INET][undef] :…: 地址已被使用 (errno=98)”,例如:

2021-11-02 09:26:50 us=736094 TCP/UDP: Socket bind failed on local address [AF_INET][undef]:53: Address already in use (errno=98)
2021-11-02 09:26:50 us=736216 Exiting due to fatal error

2、虚拟专用网的地址范围

默认情况下,虚拟专用网络的 IP 地址范围是在服务器配置文件中指定的,例如:

server 10.8.0.0 255.255.255.0

OpenVPN 服务的其他实例必须使用不同的范围。例如,对于第二个服务,您可以指定以下范围:

server 10.8.1.0 255.255.255.0

对于第三个服务,您可以指定以下 IP 地址范围,依此类推:

server 10.8.2.0 255.255.255.0

3.所有虚拟专用网络的流量路由

“在 OpenVPN 服务器上启用流量路由”部分展示了如何为 VPN 流量启用 NAT,以便客户端可以连接到 Internet。上一节中的示例显示了如何为来自 10.8.1.0/24 子网的流量配置路由。由于 OpenVPN 服务的其他实例使用不同的子网(例如,10.8.1.0/24),为了让这些虚拟网络的客户端访问 Internet,您必须为每个子网配置路由网络。

/root/bin/vpn_route.sh 文件的原始内容示例:

#!/bin/sh

# specify the name of the interface, otherwise the script will try to select it automatically
#DEV='eth0'
DEV='ens3'
PRIVATE=10.8.0.0/24
 
if [ -z "$DEV" ]; then
	DEV="$(ip route | grep default | head -n 1 | awk '{print $5}')"
fi
# Turn forwarding packets on from the tunnel interface
# they got to the external interface
sysctl net.ipv4.ip_forward=1
# Make sure iptables do not block redirected traffic:
iptables -I FORWARD -j ACCEPT
# Network Address Translation (NAT) for packets coming from the tunnel
# When forwarding is enabled, packets are forwarded by default.
# with the original address unchanged, that is, in our case 10.8.0.*
# such packages are either deleted at the ISP gateway, or even if they are
# go to the destination, the answer never finds a way back.
# These private addresses are not routed to the Internet.
#
# The solution is Network Address Translation (NAT) of outgoing traffic,
# i.e. replace private 10.8.0.* address with the public IP address of the VPN server.
# This will allow responses to reach the VPN server,
# and there they will be sent back to the tunnel.
iptables -t nat -I POSTROUTING -s $PRIVATE -o $DEV -j MASQUERADE

文件 /root/bin/vpn_route.sh 为子网 10.8.1.0/2410.8.2.0/24 和添加了 NAT 10.8.3.0/24

#!/bin/sh
 
DEV='ens3'
PRIVATE=10.8.0.0/24
 
if [ -z "$DEV" ]; then
	DEV="$(ip route | grep default | head -n 1 | awk '{print $5}')"
fi

sysctl net.ipv4.ip_forward=1

iptables -I FORWARD -j ACCEPT

iptables -t nat -I POSTROUTING -s $PRIVATE -o $DEV -j MASQUERADE

iptables -t nat -I POSTROUTING -s 10.8.1.0/24 -o $DEV -j MASQUERADE

iptables -t nat -I POSTROUTING -s 10.8.2.0/24 -o $DEV -j MASQUERADE

要检查 NAT 设置,请运行命令

iptables -L -t nat

4.配置本地静态地址

如果您使用了“如何为 OpenVPN 客户端创建静态 IP”部分并将“ifconfig-push 10.8.0.10 255.255.255.0”之类的条目添加到文件“/etc/openvpn/ccd” /client1”,那么您需要为第二个和后续的 OpenVPN 网络使用不同的客户端名称,因为它们使用不同的 VPN IP 范围。

5.启动并添加第二个及后续的OpenVPN服务启动

请注意,对于 systemctl,您可以使用如下命令

openvpn-server@<configuration>.service

其中 是位于 /etc/openvpn/server/ 文件夹中的配置文件,但没有 .conf 扩展名

例如OpenVPN服务第二个实例的第二个配置文件放在/etc/openvpn/server/server-tcp.conf文件中,那么启动服务的程序如下:

sudo systemctl start openvpn-server@server-tcp.service

使用 server-tcp.conf 配置文件检查服务状态:

systemctl status openvpn-server@server-tcp.service

使用 server-tcp.conf 配置文件查看 OpenVPN 服务错误:

journalctl -xeu openvpn-server@server-tcp.service

将服务添加到启动:

sudo systemctl enable openvpn-server@server-tcp.service

另请参阅:

  • 如何使用带有 TCP 协议的 OpenVPN
  • 如何设置 OpenVPN 服务器和客户端
  • OpenVPN 高级使用示例
正文完
可以使用微信扫码关注公众号(ID:xzluomor)
post-qrcode
 0
评论(没有评论)

文心AIGC

2024 年 6 月
 12
3456789
10111213141516
17181920212223
24252627282930
文心AIGC
文心AIGC
人工智能ChatGPT,AIGC指利用人工智能技术来生成内容,其中包括文字、语音、代码、图像、视频、机器人动作等等。被认为是继PGC、UGC之后的新型内容创作方式。AIGC作为元宇宙的新方向,近几年迭代速度呈现指数级爆发,谷歌、Meta、百度等平台型巨头持续布局
文章搜索
热门文章
潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026 Jay 2025-12-22 09...
“昆山杯”第二十七届清华大学创业大赛决赛举行

“昆山杯”第二十七届清华大学创业大赛决赛举行

“昆山杯”第二十七届清华大学创业大赛决赛举行 一水 2025-12-22 17:04:24 来源:量子位 本届...
MiniMax海螺视频团队首次开源:Tokenizer也具备明确的Scaling Law

MiniMax海螺视频团队首次开源:Tokenizer也具备明确的Scaling Law

MiniMax海螺视频团队首次开源:Tokenizer也具备明确的Scaling Law 一水 2025-12...
天下苦SaaS已久,企业级AI得靠「结果」说话

天下苦SaaS已久,企业级AI得靠「结果」说话

天下苦SaaS已久,企业级AI得靠「结果」说话 Jay 2025-12-22 13:46:04 来源:量子位 ...
最新评论
ufabet ufabet มีเกมให้เลือกเล่นมากมาย: เกมเดิมพันหลากหลาย ครบทุกค่ายดัง
tornado crypto mixer tornado crypto mixer Discover the power of privacy with TornadoCash! Learn how this decentralized mixer ensures your transactions remain confidential.
ดูบอลสด ดูบอลสด Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
Obrazy Sztuka Nowoczesna Obrazy Sztuka Nowoczesna Thank you for this wonderful contribution to the topic. Your ability to explain complex ideas simply is admirable.
ufabet ufabet Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
ufabet ufabet You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!
ufabet ufabet Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
热评文章
摩尔线程的野心,不藏了

摩尔线程的野心,不藏了

摩尔线程的野心,不藏了 量子位的朋友们 2025-12-22 10:11:58 来源:量子位 上市后的仅15天...
摩尔线程的野心,不藏了

摩尔线程的野心,不藏了

摩尔线程的野心,不藏了 量子位的朋友们 2025-12-22 10:11:58 来源:量子位 上市后的仅15天...
AI体育教练来了!中国团队打造SportsGPT,完成从数值评估到专业指导的智能转身

AI体育教练来了!中国团队打造SportsGPT,完成从数值评估到专业指导的智能转身

AI体育教练来了!中国团队打造SportsGPT,完成从数值评估到专业指导的智能转身 量子位的朋友们 2025...
AI体育教练来了!中国团队打造SportsGPT,完成从数值评估到专业指导的智能转身

AI体育教练来了!中国团队打造SportsGPT,完成从数值评估到专业指导的智能转身

AI体育教练来了!中国团队打造SportsGPT,完成从数值评估到专业指导的智能转身 量子位的朋友们 2025...
真正面向大模型的AI Infra,必须同时懂模型、系统、产业|商汤大装置宣善明@MEET2026

真正面向大模型的AI Infra,必须同时懂模型、系统、产业|商汤大装置宣善明@MEET2026

真正面向大模型的AI Infra,必须同时懂模型、系统、产业|商汤大装置宣善明@MEET2026 量子位的朋友...