在Centos 7系统上安装和配置OpenVPN,可以确保公司外部员工安全地访问内部办公网络,以下是详细的步骤和配置指南:
环境准备
项目 | 说明 |
服务器系统 | CentOS Linux release 7.9.2009 (Core) |
OpenVPN版本 | 2.5.6 |
EasyRSA版本 | 3.0.8 |
外网IP | 124.70.49.106 |
内网IP | 192.168.0.155 |
测试服务器IP | 192.168.0.21 |
客户端网段 | 172.16.16.0/24 |
安装步骤
1. 准备工作

- 关闭防火墙
- systemctl stop firewalld
- systemctl disable firewalld
- 关闭SELinux
- sed i 's/enforcing/disabled/' /etc/selinux/config
- setenforce 0
- 安装依赖包
- yum install y vim wget lrzsz gccc++ openssl openssldevel nettools lzo lzodevel pam pamdevel
- 下载并安装OpenVPN和EasyRSA
- mkdir /tmp/install
- cd /tmp/install
- wget https://swupdate.openvpn.org/community/releases/openvpn2.5.6.tar.gz
- wget https://github.com/OpenVPN/easyrsa/releases/download/v3.0.8/EasyRSA3.0.8.tgz
2. 安装OpenVPN和EasyRSA
- 解压缩文件
- tar zxvf openvpn2.5.6.tar.gz
- tar zxvf EasyRSA3.0.8.tgz
- 编译并安装OpenVPN
- cd openvpn2.5.6
- ./configure prefix=/data/openvpn/
- make && make install
- 添加环境变量
- echo e "PATH=\$PATH:/data/openvpn/sbin" > /etc/profile.d/openvpn256.sh
- source /etc/profile
- 检查是否成功安装
- openvpn version
3. 服务端配置
- 创建CA签发机构环境
- cp r /data/EasyRSA /data/openvpn/easyrsaserver
- cd /data/openvpn/easyrsaserver
- 准备默认变量文件
- egrep v "^$|^#" vars.example > vars
- vim vars # 设置CA证书有效期为100年,服务器证书为10年
- set_var EASYRSA_CA_EXPIRE 36500
- set_var EASYRSA_CERT_EXPIRE 3650
- 创建CA机构和服务端证书
- ./easyrsa initpki
- ./easyrsa buildca nopass # 使用nopass表示不需要密码
- ./easyrsa genreq openvpnserver nopass
生成TLS认证密钥及配置文件
- 生成TLS认证密钥
- openvpn genkey secret /etc/openvpn/ta.key
- 创建配置文件
- cat <<EOF > /etc/openvpn/server.conf
- port 1194
- proto udp
- dev tun
- ca ca.crt
- cert server.crt
- key server.key
- dh dh.pem
- server 10.8.0.0 255.255.255.0
- ifconfigpoolpersist ipp.txt
- keepalive 10 120
- maxclients 10
- status openvpnstatus.log
- verb 3
- EOF
启动服务端并配置防火墙与流量转发
- 启动OpenVPN服务并设为开机启动
- systemctl start openvpn@server
- systemctl enable openvpn@server
- 配置防火墙与流量转发
- firewallcmd permanent addservice openvpn
- firewallcmd permanent addmasquerade
- firewallcmd reload
- sysctl w net.ipv4.ip_forward=1
相关问答FAQs
Q1: OpenVPN连接后无法访问内网资源怎么办?
A1: 确保在OpenVPN的配置文件中正确设置了路由推送(push "route ..."),并且客户端机器的网关设置正确,可以使用ping
命令测试连通性,如果问题依旧,检查防火墙和路由表设置。
Q2: 如何生成客户端证书?
A2: 在服务端执行以下命令生成客户端证书:

- cd /data/openvpn/easyrsaserver
- ./easyrsa genreq client nopass
- ./easyrsa sign client client
将生成的客户端证书(client.crt和client.key)发送给客户端进行配置。
