3

OpenVPN客户端定制IP和路由

 1 year ago
source link: https://bajie.dev/posts/20221119-openvpn_client/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

OpenVPN客户端定制IP和路由

2022-11-19 1 分钟阅读

公司用的 OpenVPN ,研发部门和普通员工的权限不一样,技术部门需要访问服务器,普通员工则不需要

这样的话就需要根据用户定制路由了,技术部门推送特定的路由,普通员工不推送。

那么做法如下,在 server.conf 做如下设定:

ifconfig-pool-persist /etc/openvpn/conf/ipp.txt
client-config-dir /etc/openvpn/conf/static
shell

第一行是指定记录 ip 地址的文件,作用是如果服务器进程重启了,重启后会读取这个文件,客户端重新拨号后获得的 IP 就不会变,还是重启前的 IP。

第二行是指定了客户端的单独配置

我们假设一个用户是 zhangranrui,那么就去 /etc/openvpn/conf/static 下建立一个 zhangranrui 的文本文件,内容如下:

# 前面是客户端固定IP地址,后面是网关地址
ifconfig-push 172.18.0.10 172.18.0.9

# 推送路由,后面是掩码
push "route 10.10.0.0 255.255.0.0"

# 重定向客户端的所有流量,否则访问服务端内网要像上面一样推送路由
# push "redirect-gateway def1"

# 推送给客户端的DNS解析地址
# push "dhcp-option DNS 223.5.5.5"
# push "dhcp-option DNS 114.114.114.114"
fallback

上面我们固定了IP,也推送了特定路由,那么怎么知道一个客户的 ip 呢,去看 ipp 文件:

cat /etc/openvpn/conf/ipp.txt
zhangranrui,172.18.0.8
shell

注意,上面文件里的 172.18.0.8 不是 ip ,是 网段 地址

172.18.0.9 是网关地址,172.18.0.10才是真正的 IP 地址,所以不要搞错了

也可以针对用户推送一些其他选项。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK