因业务需求,需要搭建私密网络,所以理所当然需要需要搭建vpn,在所有的vpn解决方法中当然首选OpenVPN,在折腾了许久后,总算是可以用了,下面是自己的搭建记录。
OpenVPN
OpenVPN是可以用于创建虚拟专用网络加密通道的免费开源软件。使用vpn可以在不同地点不同的网络之间搭建类似于局域网的专用网络通道,OpenVPN使用方便,运行性能优秀,且支持多平台,并且采用了高强度的数据加密,再加上其开源免费的特性,是搭建vpn的理想的开源的产品。
当然介于我们GWF保护我们的网络环境,OpenVPN的官网也是被阻止访问的,本人通过shadowsocks翻墙出去访问了解的OpenVPN信息。
服务器安装配置
安装epel源
1~]# cat /etc/yum.repos.d/epel.repo2[epel]3name=epel4baseurl=https://mirrors.aliyun.com/epel/6Server/x86_645enable=16gpgcheck=07cost=2000安装openvpn
1yum install -y openvpn easy-rsaeasy-rsa配置
1~]# mkdir -p /etc/openvpn/easy-rsa/keys2~]# cp -rf /usr/share/easy-rsa/2.0/* /etc/openvpn/easy-rsa/创建CA证书和秘钥
1~]# vim /etc/openvpn/easy-rsa/vars #修改自己的国家,省份,城市,邮箱等...2...3export KEY_COUNTRY='CN'4export KEY_PROVINCE='ZJ'5export KEY_CITY='HZ'6export KEY_ORG='GM'7export KEY_EMAIL='admin@123.com'8export KEY_OU='tech'9...1011~]# source /etc/openvpn/easy-rsa/vars #初始化证书授权中心12~]# cd /etc/openvpn/easy-rsa13~]# ./clean_all #清理keys目录下的文件14~]# ./build-ca #创建ca证书15Generating a 1024 bit RSA private key16.++++++17......................++++++18writing new private key to 'ca.key'19-----20You are about to be asked to enter information that will be incorporated21into your certificate request.22What you are about to enter is what is called a Distinguished Name or a DN.23There are quite a few fields but you can leave some blank24For some fields there will be a default value,25If you enter '.', the field will be left blank.26-----27Country Name (2 letter code) [CN]:28State or Province Name (full name) [ZJ]:29Locality Name (eg, city) [HZ]:30Organization Name (eg, company) [GM]:31Organizational Unit Name (eg, section) [tech]:32Common Name (eg, your name or your server's hostname) [GM CA]:33Name [changeme]:34Email Address [mail@host.domain]:创建服务器的证书和密钥
1~]# ./build-key-server server2.....++++++3.................++++++4writing new private key to 'server.key'5-----6You are about to be asked to enter information that will be incorporated7into your certificate request.8What you are about to enter is what is called a Distinguished Name or a DN.9There are quite a few fields but you can leave some blank10For some fields there will be a default value,11If you enter '.', the field will be left blank.12-----13Country Name (2 letter code) [CN]:14State or Province Name (full name) [ZJ]:15Locality Name (eg, city) [HZ]:16Organization Name (eg, company) [GM]:17Organizational Unit Name (eg, section) [tech]:18Common Name (eg, your name or your server's hostname) [server]:19Name [changeme]:20Email Address [mail@host.domain]:2122Please enter the following 'extra' attributes23to be sent with your certificate request24A challenge password []:25An optional company name []:26Using configuration from /etc/easy-rsa/2.0/openssl.cnf27Check that the request matches the signature28Signature ok29The Subject's Distinguished Name is as follows30countryName :PRINTABLE:'CN'31stateOrProvinceName :PRINTABLE:'ZJ'32localityName :PRINTABLE:'HZ'33organizationName :PRINTABLE:'GM'34organizationalUnitName:PRINTABLE:'tech'35commonName :PRINTABLE:'server'36name :PRINTABLE:'changeme'37emailAddress :IA5STRING:'mail@host.domain'38Certificate is to be certified until Mar 28 03:05:21 2022 GMT (3650 days)39Sign the certificate? [y/n]:y4041421 out of 1 certificate requests certified, commit? [y/n]y43Write out database with 1 new entries44Data Base Update创建客户端的证书和密钥
1./build-key client123Generating a 1024 bit RSA private key4...++++++5...............++++++6writing new private key to 'client1.key'7-----8You are about to be asked to enter information that will be incorporated9into your certificate request.10What you are about to enter is what is called a Distinguished Name or a DN.11There are quite a few fields but you can leave some blank12For some fields there will be a default value,13If you enter '.', the field will be left blank.14-----15Country Name (2 letter code) [CN]:16State or Province Name (full name) [ZJ]:17Locality Name (eg, city) [HZ]:18Organization Name (eg, company) [GM]:19Organizational Unit Name (eg, section) [tech]:20Common Name (eg, your name or your server's hostname) [client1]: #根据证书来区别不同客户端21Name [changeme]:22Email Address [mail@host.domain]:2324Please enter the following 'extra' attributes25to be sent with your certificate request26A challenge password []:27An optional company name []:28Using configuration from /etc/easy-rsa/2.0/openssl.cnf29Check that the request matches the signature30Signature ok31The Subject's Distinguished Name is as follows32countryName :PRINTABLE:'CN'33stateOrProvinceName :PRINTABLE:'ZJ'34localityName :PRINTABLE:'HZ'35organizationName :PRINTABLE:'tech'36organizationalUnitName:PRINTABLE:'changeme'37commonName :PRINTABLE:'client1'38name :PRINTABLE:'changeme'39emailAddress :IA5STRING:'mail@host.domain'40Certificate is to be certified until Mar 28 03:21:06 2022 GMT (3650 days)41Sign the certificate? [y/n]:y4243441 out of 1 certificate requests certified, commit? [y/n]y45Write out database with 1 new entries46Data Base Updated创建迪菲霍尔曼密钥交换参数
1~]# ./build-dh拷贝服务器证书、密钥
1~]# cp /etc/openvpn/easy-rsa/keys/{server.crt,server.key,dh2048.pem,ca.crt} /etc/openvpn配置vpn服务端
1~]# cp /usr/share/doc/openvpn-2.3.*/sample/sample-config-files/server.conf /etc/openvpn/2~]# cat /etc/openvpn/server.conf | grep "^[^#|^;]"3local 0.0.0.0 #监听地址4port 1194 #监听端口5proto tcp #监听协议6dev tun #采用路由隧道模式7ca ca.crt #ca证书路径8cert server.crt #服务器证书9key server.key # This file should be kept secret 服务器密钥10dh dh2048.pem #密钥交换协议文件11server 10.8.0.0 255.255.255.0 #给客户端分配地址池,注意:不能和VPN服务器内网网段有相同12ifconfig-pool-persist ipp.txt13push "route 192.168.20.0 255.255.255.0" #允许客户端访问内网 20.0 的网段。14push"dhcp-option DNS 8.8.8.8" #dhcp分配dns15client-to-client #客户端之间互相通信16keepalive 10 120 #存活时间,10秒ping一次,120 如未收到响应则视为断线17comp-lzo #传输数据压缩18max-clients 100 #最多允许 100 客户端连接19user openvpn #用户20group openvpn #用户组21persist-key22persist-tun23status /var/log/openvpn/openvpn-status.log24log /var/log/openvpn/openvpn.log25verb 3iptables配置
1~]# iptables -F2~]# iptables -X3配置openvpn的nat功能,将所有网段的包转发到eth0口上:4~]# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE5添加FORWARD白名单:6~]# iptables -A FORWARD -i tun+ -j ACCEPT7开启系统的路由功能8~]# echo "1" > /proc/sys/net/ipv4/ip_forward9~]# service iptables save10~]# service iptables restart配置客户端
1~]# cp /usr/share/doc/openvpn-2.3.*/sample/sample-config-files/client.conf /etc/openvpn/client.ovpn2修改客户端配置文件3~]# cat server.conf | grep "^[^#|^;]"4client5dev tun6proto tcp //改为tcp7remote 203.195.xxx.xxx 1194 //OpenVPN服务器的外网IP和端口8resolv-retry infinite9nobind10persist-key11persist-tun12ca ca.crt //client1的证书13cert client.crt14key client.key //client1的密钥15ns-cert-type server16comp-lzo17verb 3
openvpn客户端配置
- 拷贝服务器端的/etc/openvpn/easy-rsa/{ca.crt,client.crt,client.key}和/etc/openvpn/client.ovpn到客户端的主机
- 下载openvpn客户端安装
windows客户端
mac客户端 - 把刚才复制过来的几个文件拷贝到openvpn客户端安装目录下面的config目录里面
- 启动OpenVPN GUI
在电脑右下角的openvpn图标上右击,选择“Connect”。正常情况下应该能够连接成功,分配正常的IP。