环境准备
VPNServer | eth0:192.168.1.240 | eth1:10.10.10.10 不配网关 |
Client | eth0:192.168.1.70 | eth1:10.10.10.9 |
时间同步
ntpdate time2.aliyun.com echo '#time sync' >>/var/spool/cron/root echo '*/5 * * * * /usr/sbin/ntpdate time.aliyun.com > 2>&1' >>/var/spool/cron/root crontab -l
1.脚本自动安装
地址:https://github.com/Nyr/openvpn-install
https://build.openvpn.net/downloads/releases/
wget https://git.io/vpn -O openvpn-install.sh && bash openvpn-install.sh
安装完成后如下提示
init-pki complete; you may now create a CA or requests. Your newly created PKI dir is: /etc/openvpn/server/easy-rsa/pki Generating RSA private key, 2048 bit long modulus .....................+++ ...................................................................+++ e is 65537 (0x10001) Using SSL: openssl OpenSSL 1.0.2k-fips 26 Jan 2017 Generating a 2048 bit RSA private key .....................................................+++ .............................................................................................+++ writing new private key to '/etc/openvpn/server/easy-rsa/pki/private/server.key.fdy6cHMlO5' ----- Using configuration from ./safessl-easyrsa.cnf Check that the request matches the signature Signature ok The Subject's Distinguished Name is as follows commonName :ASN.1 12:'server' Certificate is to be certified until Oct 19 12:30:45 2029 GMT (3650 days) Write out database with 1 new entries Data Base Updated Using SSL: openssl OpenSSL 1.0.2k-fips 26 Jan 2017 Generating a 2048 bit RSA private key ....................................................................................+++ .............................................................................................+++ writing new private key to '/etc/openvpn/server/easy-rsa/pki/private/leiyan.key.02wPZejNfv' ----- Using configuration from ./safessl-easyrsa.cnf Check that the request matches the signature Signature ok The Subject's Distinguished Name is as follows commonName :ASN.1 12:'leiyan' Certificate is to be certified until Oct 19 12:30:45 2029 GMT (3650 days) Write out database with 1 new entries Data Base Updated Using SSL: openssl OpenSSL 1.0.2k-fips 26 Jan 2017 Using configuration from ./safessl-easyrsa.cnf An updated CRL has been created. CRL file: /etc/openvpn/server/easy-rsa/pki/crl.pem Created symlink from /etc/systemd/system/multi-user.target.wants/openvpn-iptables.service to /etc/systemd/system/openvpn-iptables.service. Created symlink from /etc/systemd/system/multi-user.target.wants/openvpn-server@server.service to /usr/lib/systemd/system/openvpn-server@.service. Finished! Your client configuration is available at: /root/leiyan.ovpn If you want to add more clients, just run this script again!
手动安装
OPENVPN-2.2.2(服务端): wget http://swupdate.openvpn.org/community/releases/openvpn-2.2.2.tar.gz #安装LZO压缩模块 wget http://www.oberhumer.com/opensource/lzo/download/lzo-2.06.tar.gz tar zxf lzo-2.06.tar.gz cd lzo-2.06 ./configure make make install cd ../ #yum install -y lzo lzo-devel openssl openssl-devel pam pam-devel ./configure --with-lzo-headers=/usr/local/include --with-lzo-lib=/usr/local/lib make make install
[root@liaosnet ~]# rpm -qa lzo lzo-devel openssl openssl-devel pam pam-devel \ pkcs11-helper pkcs11-helper-devel pam-devel-1.1.1-10.el6_2.1.i686 pkcs11-helper-devel-1.08-1.el6.rf.i686 pam-1.1.1-10.el6_2.1.i686 lzo-2.03-3.1.el6.i686 lzo-devel-2.03-3.1.el6.i686 openssl-1.0.0-25.el6_3.1.i686 openssl-devel-1.0.0-25.el6_3.1.i686 pkcs11-helper-1.08-1.el6.rf.i686
2、配置openvpn服务(服务端)
创建CA证书,初始化命令
cd /home/root/tools/openvpn cd openvpn-2.2.2/easy-rsa/2.0 cp vars vars.qinzc.2019 vi vars #shift+g 到结尾 修改最后的几行的export的值自定义的值 export KEY_COUNTRY="CN" export KEY_PROVINCE="GD" export KEY_CITY="GuangZhou" export KEY_ORG="qinzc.me" export KEY_EMAIL="honey-z@qq.com" export KEY_EMAIL=honey-z@qq.com export KEY_CN=qinzc export KEY_NAME=qinzc export KEY_OU=qinzc export PKCS11_MODULE_PATH=qinzc export PKCS11_PIN=1234 [root@qinzc]# . vars NOTE: If you run ./clean-all, I will be doing a rm -rf on /usr/share/doc/openvpn-2.2.2/easy-rsa/2.0/keys 使用当前变量生效(. vars),若出现提示的找不到openssl.cnf,直接做个链接到openssl-1.0.0.cnf,再试即可。 [root@qinzc]# ln -s openssl-1.0.0.cnf openssl.cnf 1)清除之前的keys [root@qinzc]# ./clean-all 2) 创建私钥(ca private key) [root@qinzc]# ./build-ca 由于刚配置了默认变量,故一路回车下去即可 完成后,在keys目录下生成1024位RSA私钥ca.key和ca.crt 两个文件 3) 创建服务器密钥(server key) [root@qinzc]# ./build-key-server server 完成后,在keys目录下生成1024位RSA服务器密钥server.key、server.crt和server.csr 三个文件 4) 创建客户端用户密钥文件(user private key) [root@qinzc]# ./build-key leiyan 创建用户私钥的过程与服务器密钥的过程类似。完成后,在keys目录下生成1024位RSA服务器密钥leiyan.key、leiyan.crt和leiyan.csr 三个文件 若要建立多个用户私钥,以不用的用户名build-key USERNAM,生成多个用户密钥。 创建带密码的用户,就是客户端需要输入密码则 ./build-key-pass user1 5) 生成Diffie Hellman参数 协议文件 [root@qinzc]# ./build-dh Generating DH parameters, 1024 bit long safe prime, generator 2 This is going to take a long time 完成后,在keys目录下生成dh参数文件dh1024.pem 将/usr/share/doc/openvpn-2.2.2/easy-rsa/2.0/keys/* 目录下的所有文件复制到/etc/openvpn下 [root@qinzc ~] mkdir /etc/openvpn/ [root@qinzc ~] cp -ap /usr/share/doc/openvpn-2.2.2/easy-rsa/2.0/keys/* /etc/openvpn/ ###[root@qinzc ~] openvpn --genkey --secret keys/ta.key #生成防止恶意攻击的文件。 ###[root@qinzc ~] ./make-crl vpncrl.pem #吊销链接文件 6) 创建服务端配置文件server.conf 将/usr/share/doc/openvpn-2.2.2/sample-config-files/server.conf复制到/etc/openvpn/目录下server.conf并修改以下参数 #客户端文件也拷贝,client.conf [root@qinzc openvpn]# vim server.conf \# TCP or UDP server? proto tcp ;proto udp 用分号注释proto udp,去掉proto tcp的分号注释,即使用tcp方式连接 ;server 10.8.0.0 255.255.0.0 server 192.168.18.0 255.255.255.240 改成自己使用的IP网络段 user nobody group nobody 去除user nobody 和group nobody前的分号,使系统使用nobody用户启动openvpn进程 log openvpn.log ;log-append openvpn.log 这两个建议去掉其中一个的分号,若不去除之一,openvpn的日志将写到系统日志中去。log项,每次启动将清空之前的openvpn.log;log-append项,日志是追加的(每次启动不清空之前的openvpn.log) push "route 10.10.10.0 255.255.254.0" #如果有多个网段,就写多条 push "redirect-gateway def1 bypass-dhcp" #推送默认网关走OPENVPN线路 push "dhcp-option DNS 114.114.114.114" #推送设置DNS push "dhcp-option DNS 114.114.114.115" client-to-client #允许vpnclient互通 duplicate-cn #允许多个客户端使用同一个账号连接 comp-lzo #开启压缩功能 7) 设置允许IP转发 在/etc/openvpn目录下编写openvpn-startup文件,内容如下: \# enable IP forwarding echo 1 > /proc/sys/net/ipv4/ip_forward sysctl -p vim /etc/sysctl.conf 完成以上所有操作后,openvpn基本上就配置完成了。
使用service openvpn start 即可启动openvpn,而后可以看到openvpn的进程 [root@liaosnet openvpn]# ps -ef | grep openvpn nobody 9428 1 0 00:53 ? 00:00:00 /usr/sbin/openvpn --daemon --writepid /var/run/openvpn/server.pid --config server.conf --cd /etc/openvpn 使用ifconfig -a可以看到配置使用的openvpn接口tun0也已经启动 注:可能需要增加iptables的nat策略 iptables -t nat -A POSTROUTING -s 192.168.0.0/23 -o eth0 -j MASQUERADE 其中的网络地址与server.conf中的网络配置应一致
3、配置openvpn客户端(windows)
()(Windows客户端): http://openvpn.se/files/install_packages/openvpn-2.0.9-gui-1.0.3-install.exe假设安装目录为:C:\Program Files\OpenVPN
1) 加载密钥文件(用户leiyan,其它用户如user2则下载user2的用户密钥)
将服务端/etc/openvpn目录下的 leiyan.crt leiyan.csr leiyan.key ca.crt ca.key 文件下载到C:\Program Files\OpenVPN\config目录下
2) 配置client.conf
将C:\Program Files\OpenVPN\sample-config\下的client.opvn复制到C:\Program Files\OpenVPN\config目录下,并修改之 proto tcp ;proto udp 用分号注释proto udp,去掉proto tcp的分号注释,即使用tcp方式连接(与服务端相等) ;remote my-server-1 1194 remote 192.168.80.60 1194 将服务器名改成实际的IP地址或者域名 cert leiyan.crt key leiyan.key cert和key改成实际用户leiyan的密钥
完成后即可使用Openvpn GUI 与openvpn建立VPN连接。
停留在世界边缘,与之惜别