Centos8安装配置openvpn实现服务器代理上网,Ubuntu使用docker安装配置
一、服务器(因为ubuntu没有虚拟网卡所以选择centos)
1.安装openvpn(服务器端)、easy-rsa(证书生成管理)、iptables-service(流量转发,服务器用作代理需要)
dnf -y install openvpn easy-rsa iptables-service
第一步:生成服务器端证书
查看已安装的easy-rsa目录
rpm -ql easy-rsa
复制一份easy-rsa到/etc/openvpn目录下,也可以直接在原目录修改生成证书
cp -r /usr/share/easy-rsa/ /etc/openvpn/easy-rsa
复制easy-rsa配置文件到/etc/openvpn/easy-rsa/3.0.8目录下,并重命名为vars
cp -r /usr/share/doc/easy-rsa/vars.example /etc/openvpn/easy-rsa/3.0.8/vars
查看/etc/openvpn目录结构
tree /etc/openvpn
修改证书配置文件vars,其他的默认就好,主要是修改个人信息,也可以不改
vim /etc/openvpn/easy-rsa/3.0.8/varsset_var EASYRSA_REQ_COUNTRY "CN"set_var EASYRSA_REQ_PROVINCE "SICHUAN"set_var EASYRSA_REQ_CITY "CHENGDU"set_var EASYRSA_REQ_ORG "geren"set_var EASYRSA_REQ_EMAIL "407906638@qq.com"set_var EASYRSA_REQ_OU "tpc"
初始化,如果以前的证书不要了,要从头开始创建证书就要从这一步开始
cd /etc/openvpn/easy-rsa/3.0.8/usr/share/easy-rsa/3/easyrsa init-pki
创建根证书,会提示设置密码,此处我用nopass参数选择不要密码,如果有密码服务器每次启动都要求输入密码,还需要自己写脚本启动(ps:脚本我不会写)
/usr/share/easy-rsa/3/easyrsa build-ca nopass
创建server端证书和私钥文件
/usr/share/easy-rsa/3/easyrsa gen-req server nopass
给server端证书签名,提示confirm request details:时,输入yes
/usr/share/easy-rsa/3/easyrsa sign server server
创建dh文件,秘钥交换算法
/usr/share/easy-rsa/3/easyrsa gen-dh
创建tls认证秘钥
openvpn --genkey tls-auth ta.key
查看当前生成的文件目录结构
tree pki
拷贝证书文件到openvpn目录下
mkdir /etc/openvpn/certscp ./pki/ca.crt /etc/openvpn/certs/cp ./pki/dh.pem /etc/openvpn/certs/cp ./pki/issued/server.crt /etc/openvpn/certscp ./pki/private/server.key /etc/openvpn/certscp ta.key /etc/openvpn/certs
第二步:创建server配置文件
拷贝配置文件模板
cp /usr/share/doc/openvpn/sample/sample-config-files/server.conf /etc/openvpn/
修改配置文件
cd /etc/openvpnvim server.conf
#监听本机ip地址local 0.0.0.0(这里填本机地址)#监控本机端口号port 1194#指定采用的传输协议,可以选择tcp或udpproto tcp#指定创建的通信隧道类型,可选tun或tap,window服务器必须是tapdev tun#指定CA证书的文件路径ca /etc/openvpn/certs/ca.crt#指定服务器端的证书文件路径cert /etc/openvpn/certs/server.crt#指定服务器端的私钥文件路径key /etc/openvpn/certs/server.key#指定迪菲赫尔曼参数的文件路径dh /etc/openvpn/certs/dh.pem#指定虚拟局域网占用的IP地址段和子网掩码,不能和服务器eth0同网段server 10.8.0.0 255.255.255.0#服务器自动给客户端分配IP后,客户端下次连接时,仍然采用上次的IP地址(第一次 分配的IP保存在ipp.txt中,下一次分配其中保存的IP)。ifconfig-pool-persist ipp.txt#自动推送客户端上的网关及DHCP,此项开启了流量转发,有这项才能使用服务器代理上 网push "redirect-gateway def1 bypass-dhcp"#OpenVPN的DHCP功能为客户端提供指定的 DNS、WINS 等push "dhcp-option DNS 114.114.114.114"#允许客户端与客户端相连接,默认情况下客户端只能与服务器相连接client-to-client允许同一个客户端证书多次登录,看需配置#duplicate-cn#每10秒ping一次,连接超时时间设为120秒keepalive 10 120#开启TLS-auth,使用ta.key防御攻击。服务器端的第二个参数值为0,客户端的为1。tls-auth /etc/openvpn/certs/ta.key 0#加密认证算法,2.4之前是AES-256-CBCcipher AES-256-GCM#使用lzo压缩的通讯,服务端和客户端都必须配置comp-lzo#最大连接用户max-clients 100#定义运行的用户和组,openvpn用户是安装的时候系统自动创建的user openvpngroup openvpn#重启时仍保留一些状态persist-keypersist-tun#输出短日志,每分钟刷新一次,以显示当前的客户端status /var/log/openvpn-status.log#日志保存路径log /etc/openvpn/log/openvpn.loglog-append /etc/openvpn/log/openvpn.log#指定日志文件的记录详细级别,可选0-9,等级越高日志内容越详细verb 3#相同信息的数量,如果连续出现 20 条相同的信息,将不记录到日志中mute 20#下面这项只能udp连接开启#explicit-exit-notify 1#设置tls最低版本为1.3,连接的客户端如果是2.4以下则配置为1.0tls-version-min 1.3
第三步:配置系统转发,需要代理上网的必须配置
允许转发
echo net.ipv4.ip_forward=1 >> /etc/sysctl.conf
配置立即生效
sysctl -p
关闭firewall
systemctl stop firewalldsystemctl disable firewalld
启动iptables
systemctl enable iptablessystemctl start iptables
配置iptables转发流量,代理主要以iptables转发实现
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
允许tcp/udp 1194通过防火墙
iptables -I INPUT -p tcp --dport 1194 -j ACCEPTiptables -I INPUT -p udp --dport 1194 -j ACCEPT
保存规则并重启
service iptables savesystemctl restart iptables
第四步:创建启动的服务脚本文件
因centos8没有unit文件还需要自己新建一个,写入以下内容
vim /lib/systemd/system/openvpn@.service[Unit]Description=OpenVPN Robust And Highly Flexible Tunneling Application On %IAfter=network.target[Service]Type=notifyPrivateTmp=trueExecStart=/usr/sbin/openvpn --cd /etc/openvpn/ --config %i.conf[Install]WantedBy=multi-user.target
设置openvpn开机启动
systemctl enable openvpn@server
启动openvpn
systemctl start openvpn@server
查看端口和进程是否启动成功,
netstat -lntp|grep openvpnps -aux|grep openvpn
如果启动失败:
首先查看服务状态
systemctl status openvpn@server
然后进入日志目录查看日志,将error项到网上搜索解决办法
cat /etc/openvpn/log/openvpn.log
二、客户端
客户端下载地址分享:https://www.aliyundrive.com/s/qpBM79CJNKC
如果这个客户端过期了可以去github上下载源码重新编译一个
https://github.com/OpenVPN/openvpn-gui
1.服务器侧配置
进入证书管理目录
cd /etc/openvpn/easy-rsa/3.0.8
生成客户端1的证书,客户端2依次类推
/usr/share/easy-rsa/3/easyrsa gen-req client1 nopass
注册客户端1的证书,要输入yes
/usr/share/easy-rsa/3/easyrsa sign client client1
将证书拷贝到一个目录存着
cp ./pki/issued/client1.crt /etc/openvpn/clientcp ./pki/private/client1.key /etc/openvpn/client
2.本地电脑侧配置
将client1.crt client1.key ta.key ca.crt四个文件下载到本地客户端目录的config目录下
配置客户端配置文件,拷贝客户端sample-config目录下的client.ovpn文件到config目录下
修改内容如下:
#客户端client#隧道类型,与服务器一致dev tun#tcp还是udp,与服务器一致proto tcp#服务器ip和端口remote http://xxx.xxx.xxx.xxx 1194#自动重连resolv-retry infinite#不绑定本地特定的端口nobind#服务器重启后保持一些状态persist-keypersist-tun#客户端证书目录ca ca.crtcert client.crtkey client.key#不明,推测是远程证书验证之类的remote-cert-tls server#tls握手秘钥,与服务器保持一致,服务器0,客户端1tls-auth ta.key 1#加密方式cipher AES-256-GCM#开启数据压缩comp-lzo日志级别verb 3#相同信息的数量,如果连续出现 20 条相同的信息,将不记录到日志中mute 20#tls最低版本,与服务器保持一致tls-version-min 1.3#不保存密码auth-nocache#使客户端中所有流量经过VPN,所有网络连接都使用vpnredirect-gateway def1
如果是云服务器,记得打开云服务器安全组里面的1194端口
我的实践
服务端配置文件
# Secure OpenVPN Server Config# Basic Connection Configdev tunproto udpport 5000keepalive 10 120max-clients 5# Certsca /path/to/ca.crtcert /path/to/server.crtkey /path/to/server.keydh /path/to/dh.pem#tls-auth ../ta.key 0# Ciphers and Hardeningreneg-sec 0remote-cert-tls client#crl-verify ../crl.pemtls-version-min 1.2cipher AES-256-CBCauth SHA512tls-cipher TLS-DHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-RSA-WITH-AES-256-CBC-SHA256:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-128-CBC-SHA256# Drop Privsuser nobodygroup nobody# IP poolserver 172.31.100.0 255.255.255.0topology subnetifconfig-pool-persist ../ipp.txtclient-config-dir ../client# Miscpersist-keypersist-tuncomp-lzo# DHCP Push options force all traffic through VPN and sets DNS serverspush "redirect-gateway def1 bypass-dhcp"push "dhcp-option DNS 1.1.1.1"push "dhcp-option DNS 8.8.8.8"push "dhcp-option DNS 8.8.4.4"# Logginglog-append /var/log/openvpn.logverb 3
客户端配置文件
Ubuntu
此配置是通过图形界面配置之后,系统实际的配置文件.
[connection]id=thseauuid=xxxxxxxxx-xxxx-xxxxtype=vpnautoconnect=falsepermissions=user:<USER_NAME>:;timestamp=xxxxxxxxx[vpn]auth=SHA512ca=/path/to/ca.crtcert=/path/to/client.crtcert-pass-flags=0cipher=AES-256-CBCcompress=lzoconnection-type=tlskey=/path/to/client.keykeysize=256port=5000remote=<YOU_VPN_HOST>service-type=org.freedesktop.NetworkManager.openvpn[ipv4]dns-search=method=auto[ipv6]addr-gen-mode=stable-privacydns-search=method=auto[proxy]
安卓设备
使用OpenVPN Profile导入下面配置文件即可
[connection]id thseatype vpnautoconnect flase[vpn]auth SHA512ca /storage/emulated/0/myvpn/ca.crtcert /storage/emulated/0/myvpn/client.crtcert-pass-flags 0cipher AES-256-CBCcomp-lzoconnection-type tlskey /storage/emulated/0/myvpn/client.keykeysize 256proto udpport 5000remote <YOU_VPN_HOST>
之前踩了一个坑,在安卓的配置文件里把数据的压缩方式配置成了
compres lzo 因为借鉴了Ubuntu的 配置
结果VPN可以连接成功 ..但是无法访问网络,最后在服务端 查询日志才发现这个问题.特此记录 Openvpn的客户端配置文件中的说数据压缩 应该是comp-lzo才对
