centos部署openvpn

centos部署openvpn

openvpn服务端

安装依赖组件

sh 复制代码
yum -y install epel-release openvpn easy-rsa net-tools bridge-utils

配置服务端证书

创建ca和证书,使用免密模式

sh 复制代码
cd /usr/share/easy-rsa/3
./easyrsa init-pki
./easyrsa build-ca
./easyrsa build-server-full server1 nopass
./easyrsa build-client-full client1 nopass
./easyrsa gen-dh
openvpn --genkey --secret ./pki/ta.key

复制证书到运行目录下

sh 复制代码
cp -pR /usr/share/easy-rsa/3/pki/{issued,private,ca.crt,dh.pem,ta.key} /etc/openvpn/server/

配置路由转发

修改内核参数

复制代码
vi /etc/sysctl.d/99-sysctl.conf
配置net.ipv4.ip_forward = 1
sysctl --system 

服务端配置说明

配置文件里的路径相对server.conf所在目录

sh 复制代码
cp /usr/share/doc/openvpn-x/sample/sample-config-files/server.conf /etc/openvpn/server/
vi /etc/openvpn/server/server.conf
txt 复制代码
## 为客户端分配的ip段
server 10.8.0.0 255.255.255.0
## 服务端内网网段例172.16.30.0则配置如下
push "route 172.16.30.0 255.255.255.0"
## 允许多客户端使用同一个证书连接
duplicate-cn
## 证书目录
ca ca.crt
cert issued/server1.crt
key private/server1.key
dh dh.pem
## 日志输出目录
status /var/log/openvpn/openvpn-status.log
log    /var/log/openvpn/openvpn.log
## 使用tcp时注释掉以下配置
;explicit-exit-notify 1

服务端启动

sh 复制代码
systemctl start openvpn-server@server
systemctl enable openvpn-server@server
systemctl status openvpn-server@server

配置允许访问同一网段

不配置则客户端只能访问vpn服务器所在ip。需添加路由表配置转发,例10.8.0.0/16是服务端server.conf为客户端分配的网段

sh 复制代码
iptables -t nat -A POSTROUTING -s 10.8.0.0/16 -j MASQUERADE

查看服务端日志

详见服务端server.conf里配置的日志目录

查看已接入的客户端

sh 复制代码
cat /etc/openvpn/server/ipp.txt

客户端使用

客户端证书

拷贝服务端生成的证书文件放到客户端config目录下

复制代码
/etc/openvpn/server/ca.crt
/etc/openvpn/server/ta.key
/etc/openvpn/server/issued/client1.crt
/etc/openvpn/server/private/client1.key  

windows客户端配置

从服务端拷贝sample.ovpn配置编辑,也放在config目录下

复制代码
/usr/share/doc/openvpn-x/sample/sample-windows/sample.ovpn

客户端配置说明

复制代码
## vpn服务端所在公网ip和vpn服务端口
remote x.x.x.x 1194
## 客户端证书
ca ca.crt
cert client1.crt
key client1.key 
tls-auth ta.key 1
## 设备类型和服务端配置一致
dev tun
## 协议要和服务端配置一致
proto tcp
相关推荐
花嫁代二娃4 小时前
Linux:环境变量
linux
乌托邦的逃亡者5 小时前
Docker的/var/lib/docker/目录占用100%的处理方法
运维·docker·容器
ldj20205 小时前
Jenkins 流水线配置
运维·jenkins
古希腊数通小白(ip在学)7 小时前
stp拓扑变化分类
运维·服务器·网络·智能路由器
l1x1n09 小时前
Vim 编辑器常用操作详解(新手快速上手指南)
linux·编辑器·vim
12点一刻10 小时前
搭建自动化工作流:探寻解放双手的有效方案(2)
运维·人工智能·自动化·deepseek
未来之窗软件服务10 小时前
东方仙盟AI数据中间件使用教程:开启数据交互与自动化应用新时代——仙盟创梦IDE
运维·人工智能·自动化·仙盟创梦ide·东方仙盟·阿雪技术观
ajassi200011 小时前
开源 python 应用 开发(三)python语法介绍
linux·python·开源·自动化
o不ok!11 小时前
Linux面试问题-软件测试
linux·运维·服务器
DaxiaLeeSuper11 小时前
Prometheus+Grafana+node_exporter监控linux服务器资源的方案
linux·grafana·prometheus