1、下载软件
安装openvpn软件及相关依赖包
yum -y install openssh-server openssl openssl-devel lzo lzo-devel pam-devel openvpn
查看openvpn包安装情况
rpm -ql openvpn
# 下载不安装
mkdir -p /root/openvpn
yum install --downloadonly --downloaddir=/root/openvpn openssh-server openssl openssl-devel lzo lzo-devel pam-devel openvpn
2、下载证书软件
# 下载证书软件
wget https://github.com/OpenVPN/easy-rsa/archive/v3.0.7.tar.gz
mv v3.0.7.tar.gz easy-rsa-3.0.7.tar.gz
tar zvxf easy-rsa-3.0.7.tar.gz
3、证书制作软件
# 制作服务端证书
cd easy-rsa-3.0.7/easyrsa3
cp -a vars.example vars
vim vars
# 国家
set_var EASYRSA_REQ_COUNTRY "CN"
# 省
set_var EASYRSA_REQ_PROVINCE "BJ"
# 城市
set_var EASYRSA_REQ_CITY "BeiJing"
# 组织
set_var EASYRSA_REQ_ORG "zhangshan"
# 邮箱
set_var EASYRSA_REQ_EMAIL "zhangshan@xxxx.com"
# 拥有者
set_var EASYRSA_REQ_OU "ZS"
# 长度
set_var EASYRSA_KEY_SIZE 2048
# 算法
set_var EASYRSA_ALGO rsa
# CA证书过期时间,单位天
set_var EASYRSA_CA_EXPIRE 36500
# 签发证书的有效期是多少天,单位天
set_var EASYRSA_CERT_EXPIRE 36500
4、生成服务端
# 进入目录
cd /root/easy-rsa-3.0.7/easyrsa3
# 初始化与创建CA根证书
./easyrsa init-pki
# 初始化,会在当前目录创建PKI目录,用于存储一些中间变量及最终生成的证书
./easyrsa build-ca
# 在这部分需要输入PEM密码 PEM pass phrase,输入两次,此密码必须记住,不然# 以后不能为证书签名。
# 还需要输入common name 通用名,如:openvpn,这个你自己随便设置个独一无#二的。
# 生成服务端证书
./easyrsa build-server-full server nopass
# 为服务端生成证书对并在本地签名。nopass参数生成一个无密码的证书;在此过程中会让你确认ca密码
./easyrsa gen-dh
# 创建Diffie-Hellman,确保key穿越不安全网络的命令,时间会有点长,耐心等待
6、生成客户端证书
./easyrsa build-client-full client nopass # 无密码,实际应用中不推荐,客户端有密码可提高安全性
./easyrsa build-client-full zhangsan # 让你输入密码,后续VPN连接时会使用
./easyrsa build-client-full lisi # 让你输入密码,后续VPN连接时会使用
./easyrsa build-client-full wangwu # 让你输入密码,后续VPN连接时会使用
# 为客户端生成证书对并在本地签名。nopass参数生成一个无密码的证书;在此过程中都会让你确认ca密码
# 为了提高安全性,生成ta.key
openvpn --genkey --secret ta.key
# 加强认证方式,防攻击。如果配置文件中启用此项(默认是启用的),就需要执行上述命令,并把ta.key放到/etc/openvpn/server目录。配置文件中服务端第二个参数为0,同时客户端也要有此文件,且client.conf中此指令的第二个参数需要为1。【服务端有该配置,那么客户端也必须要有】
7、整理服务端证书
mkdir -p /etc/openvpn/server/
cp -a pki/ca.crt /etc/openvpn/server/
cp -a pki/private/server.key /etc/openvpn/server/
cp -a pki/issued/server.crt /etc/openvpn/server/
cp -a pki/dh.pem /etc/openvpn/server/
cp -a ta.key /etc/openvpn/server/
# 创建服务端配置文件
服务端配置文件
# cat /etc/openvpn/server/server.conf # 配置文件内容
local 0.0.0.0
port 1194
proto tcp
dev tun
ca /etc/openvpn/server/ca.crt
cert /etc/openvpn/server/server.crt
key /etc/openvpn/server/server.key
dh /etc/openvpn/server/dh.pem
server 10.192.252.0 255.255.252.0
ifconfig-pool-persist ipp.txt
push "route 10.0.16.0 255.255.252.0"
# 如需代理上网,打开以下三行
# 一般情况不建议打开,若服务端不能上外网
# push "redirect-gateway def1 bypass-dhcp"
# push "dhcp-option DNS 114.114.114.114"
# push "dhcp-option DNS 8.8.8.8"
client-to-client
duplicate-cn
keepalive 10 120
tls-auth /etc/openvpn/server/ta.key 0
cipher AES-256-CBC
compress lz4-v2
push "compress lz4-v2"
;comp-lzo
max-clients 1000
user nobody
group nobody
persist-key
persist-tun
status openvpn-status.log
log /var/log/openvpn.log
verb 3
;explicit-exit-notify 1
启动openvpn服务并查看进程与端口
# 启动openvpn
systemctl start openvpn-server@server
# 相关查看工具
netstat -lntup | grep '1194'
ps -ef |grep openvpn
ip addr 查看tun0网卡信息
iptables -t nat -A POSTROUTING -s 10.192.252.0/22 -o eth0 -j MASQUERADE
# 将本机的8021端口转发到ip地址为192.168.16.3的8021端口上,只能使用IP地址,不能使用主机名:
firewall-cmd --permanent --zone=public --add-forward-port=port=8021:proto=tcp:toaddr=192.168.16.3:toport=8021
# 删除转发规则
firewall-cmd --permanent --zone=public --remove-forward-port=port=8021:proto=tcp:toaddr=192.168.16.3:toport=8021
# 重新启动服务,让规则生效:
firewall-cmd --reload
firewall-cmd --query-masquerade # 检查是否允许伪装IP
firewall-cmd --add-masquerade # 允许防火墙伪装IP
firewall-cmd --remove-masquerade# 禁止防火墙伪装IP
8、整理客户端证书
mkdir -p /etc/openvpn/client
cp -a pki/ca.crt /etc/openvpn/client/
cp -a ta.key /etc/openvpn/client/
cp -a pki/issued/client.crt /etc/openvpn/client
cp -a pki/private/client.key /etc/openvpn/client/
# 新建 clent.ovpn,如果是linux直接.conf即可
cd /etc/openvpn/client/
vim clent.ovpn
打包下载:放置在windows得openvpn软件安装OpenVPN\config目录中即可
client.ovpn内容如下:
;# 文件名 windows为client.ovpn,Linux为client.conf
client
dev tun
proto tcp
remote 公网IP 1194
resolv-retry infinite
nobind
;user nobody
;group nobody
persist-key
persist-tun
ca ca.crt
cert client.crt
key client.key
remote-cert-tls server
tls-auth ta.key 1
cipher AES-256-CBC
compress lz4-v2
verb 3
;mute 20
linux安装客户端
# 把服务器上/etc/openvpn/client下的拷贝到vpn客户端指定目录
scp * root@10.2.13.174:/etc/openvpn/client
# 安装openvpn
yum -y install openvpn
systemctl start openvpn-client@client
吊销证书
./easyrsa revoke zhangsan
./easyrsa gen-crl
9、配置固定IP
# 新增 client-config-dir
client-config-dir /etc/openvpn/ccd
# 在/etc/openvpn/ccd新增具体固定IP客户
# 例如:zhangsan
mkdir - p /etc/openvpn/ccd
vim /etc/openvpn/ccd/zhansan[替换成具体客户名称]
ifconfig-push 10.192.252.6 10.192.252.5
完整的配置如下:
# cat /etc/openvpn/server/server.conf # 配置文件内容
local 0.0.0.0
port 1190
proto tcp
dev tun
ca /etc/openvpn/server/ca.crt
cert /etc/openvpn/server/server.crt
key /etc/openvpn/server/server.key
dh /etc/openvpn/server/dh.pem
server 10.192.252.0 255.255.252.0
ifconfig-pool-persist ipp.txt
push "route 172.16.16.0 255.255.240.0"
client-config-dir /etc/openvpn/ccd
client-to-client
duplicate-cn
keepalive 10 120
tls-auth /etc/openvpn/server/ta.key 0
cipher AES-256-CBC
compress lz4-v2
push "compress lz4-v2"
;comp-lzo
max-clients 1000
user nobody
group nobody
persist-key
persist-tun
status openvpn-status.log
log /var/log/openvpn.log
verb 3
;explicit-exit-notify 1
设置固定IP客户时,错误解决:
# 设置成ifconfig-push 10.192.252.6 10.192.252.7
# 会报如下错误:
VPN endpoints cannot use the first or last address within a given 255.255.255.252 subnet
# 这个时候把IP设置成 ifconfig-push 10.192.252.6 10.192.252.5 即可。
重启服务端,这个时候就完成了指定客户的固定IP设置。
C:\Program Files\OpenVPN\bin>openvpn --show-valid-subnets
On Windows, point-to-point IP support (i.e. --dev tun)
is emulated by the TAP-Windows driver. The major limitation
imposed by this approach is that the --ifconfig local and
remote endpoints must be part of the same 255.255.255.252
subnet. The following list shows examples of endpoint
pairs which satisfy this requirement. Only the final
component of the IP address pairs is at issue.
As an example, the following option would be correct:
--ifconfig 10.7.0.5 10.7.0.6 (on host A)
--ifconfig 10.7.0.6 10.7.0.5 (on host B)
because [5,6] is part of the below list.
[ 1, 2] [ 5, 6] [ 9, 10] [ 13, 14] [ 17, 18]
[ 21, 22] [ 25, 26] [ 29, 30] [ 33, 34] [ 37, 38]
[ 41, 42] [ 45, 46] [ 49, 50] [ 53, 54] [ 57, 58]
[ 61, 62] [ 65, 66] [ 69, 70] [ 73, 74] [ 77, 78]
[ 81, 82] [ 85, 86] [ 89, 90] [ 93, 94] [ 97, 98]
[101,102] [105,106] [109,110] [113,114] [117,118]
[121,122] [125,126] [129,130] [133,134] [137,138]
[141,142] [145,146] [149,150] [153,154] [157,158]
[161,162] [165,166] [169,170] [173,174] [177,178]
[181,182] [185,186] [189,190] [193,194] [197,198]
[201,202] [205,206] [209,210] [213,214] [217,218]
[221,222] [225,226] [229,230] [233,234] [237,238]
[241,242] [245,246] [249,250] [253,254]
网友评论