首先我们先使用easy-rsa制作openVPN证书
下载并解压easy-rsa软件包
1 2 3 mkdir /data/ tools -p wget -P /data/ tools http:// down.i4t.com/easy-rsa.zip unzip -d /usr/ local /data/ tools/easy-rsa.zip
在开始制作CA证书之前,我们还需要编辑vars文件,修改如下相关选项
1 2 3 4 5 6 7 8 9 10 11 12 cd /usr/local/easy-rsa-old-master/easy-rsa/2.0/ vim varsexport KEY_COUNTRY="cn" export KEY_PROVINCE="BJ" export KEY_CITY="BJ" export KEY_ORG="abcdocker" export KEY_EMAIL="cyh@i4t.com" export KEY_CN=abcexport KEY_NAME=abcexport KEY_OU=abc
注意:以上内容,我们也可以使用系统默认的,也就是说不进行修改也是可以使用的
然后使用使环境变量生效
1 2 3 4 source vars ./clean-all
接下来开始正式制作CA证书,命令如下
这时我们可以查看keys目录,已经帮我们生成ca.crt和ca.key两个文件,其中ca.crt就是我们的证书文件
1 2 [root@abc01 2.0] ca.crt ca.key index.txt serial
制作Server端证书 为服务端生成证书和密钥
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 .... An optional company name []: Using configuration from /usr/local/easy-rsa-old-master/easy-rsa/2.0/openssl-1.0.0.cnf Check that the request matches the signatureSignature ok The Subject's Distinguished Name is as follows countryName :PRINTABLE:' cn' stateOrProvinceName :PRINTABLE:' BJ' localityName :PRINTABLE:' BJ' organizationName :PRINTABLE:' abcdocker' organizationalUnitName:PRINTABLE:' abc' commonName :PRINTABLE:' abc' name :PRINTABLE:' abc' emailAddress :IA5STRING:' cyh@i4t.com' Certificate is to be certified until Jan 31 14:01:35 2030 GMT (3650 days) Sign the certificate? [y/n]:y 1 out of 1 certificate requests certified, commit? [y/n]y Write out database with 1 new entries Data Base Updated #这里的server就是我们server端的证书
查看新生成的证书
1 2 3 4 [root@abc01 2.0] 01.pem abc.key index.txt serial server.crt ca.crt index.txt.attr serial.old server.csr ca.key index.txt.old
这里我们已经生成了server.crt、server.key、server.csr三个文件,其中server.crt和server.key两个文件是我们需要使用的
制作Client端证书 这里我们创建2个用户,分别为client1和client2
1 2 3 4 ./build-key client1 ./build-key client2
每一个登陆的VPN客户端需要有一个证书,每个证书在同一时刻只可以一个客户端连接(可以修改配置文件)
现在为服务器生成加密交换时的Diffie-Hellman文件
证书生成完毕
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 [root@abc01 2.0] total 84 -rw-r--r-- 1 root root 7997 Feb 3 09:01 01.pem -rw-r--r-- 1 root root 7880 Feb 3 09:09 02.pem -rw-r--r-- 1 root root 7997 Feb 3 09:01 abc.crt -rw-r--r-- 1 root root 1765 Feb 3 09:01 abc.csr -rw------- 1 root root 3272 Feb 3 09:01 abc.key -rw-r--r-- 1 root root 2293 Feb 3 09:01 ca.crt -rw------- 1 root root 3272 Feb 3 09:01 ca.key -rw-r--r-- 1 root root 424 Feb 3 09:06 dh2048.pem -rw-r--r-- 1 root root 211 Feb 3 09:09 index.txt -rw-r--r-- 1 root root 21 Feb 3 09:09 index.txt.attr -rw-r--r-- 1 root root 21 Feb 3 09:01 index.txt.attr.old -rw-r--r-- 1 root root 105 Feb 3 09:01 index.txt.old -rw-r--r-- 1 root root 3 Feb 3 09:09 serial -rw-r--r-- 1 root root 3 Feb 3 09:01 serial.old -rw-r--r-- 1 root root 7880 Feb 3 09:09 test.crt -rw-r--r-- 1 root root 1765 Feb 3 09:09 test.csr -rw------- 1 root root 3272 Feb 3 09:09 test.key
其中包含了一个test用户和abc用户的证书
其中只有*.crt和*.key文件是我们需要使用的
安装OpenVPN 安装vpn的方法有2种,一种是使用yum安装,另外一种是编译安装。这两个我们选择一个就可以
编译安装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum makecache yum install -y lzo lzo-devel openssl openssl-devel pam pam-devel net-tools git lz4-devel wget -P /data/tools http://down.i4t.com/openvpn-2.4.7.tar.gzcd /data/tools tar zxf openvpn-2.4.7.tar.gzcd openvpn-2.4.7 ./configure --prefix=/usr/local/openvpn-2.4.7 make make install ln -s /usr/local/openvpn-2.4.7 /usr/local/openvpn
yum安装
OpenVPN需要安装EPEL
1 2 curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum clean all && yum makecache
yum 安装openVPN
配置OpenVPN服务端 我们需要创建openVPN文件目录和证书目录
1 2 3 4 5 mkdir /etc/openvpn mkdir /etc/openvpn/keys
生成tls-auth key并将其拷贝到证书目录中(防DDos攻击、UDP淹没等恶意攻击)
1 2 3 4 5 6 7 8 /usr/local/openvpn/sbin/openvpn --genkey --secret ta.key openvpn --genkey --secret ta.key mv ./ta.key /etc/openvpn/keys/
将我们上面生成的CA证书和服务端证书拷贝到证书目录中
1 2 3 4 5 6 7 8 9 10 11 12 $ cp /usr/local/easy-rsa-old-master/easy-rsa/2.0/keys/{server.crt,server.key,ca.crt,dh2048.pem} /etc/openvpn/keys/ [root@k8s-01 ~] total 24 -rw-r--r-- 1 root root 2342 Feb 3 12:48 ca.crt -rw-r--r-- 1 root root 424 Feb 3 12:48 dh2048.pem -rw-r--r-- 1 root root 8089 Feb 3 12:48 server.crt -rw------- 1 root root 3272 Feb 3 12:48 server.key -rw------- 1 root root 636 Feb 3 12:47 ta.key
拷贝OpenVPN配置文件
1 2 3 4 5 cp /data/tools/openvpn-2.4.7/sample/sample-config-files/server.conf /etc/openvpn/ cp /usr/share/doc/openvpn-2.4.7/sample/sample-config-files/server.conf /etc/openvpn/
接下来我们来配置服务端的配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 $ cat /etc/openvpn/server.conf port 1194 proto tcp dev tun ca keys/ca.crt cert keys/server.crt key keys/server.key dh keys/dh2048.pem server 10.4.82.0 255.255.255.0 ifconfig-pool-persist ipp.txt push "route 10.4.82.0 255.255.255.0" client-to-client duplicate-cn keepalive 10 120 tls-auth keys/ta.key 0 comp-lzo persist-key persist-tun status openvpn-status.log log-append openvpn.log verb 3 plugin /usr/lib64/openvpn/plugin/lib/openvpn-auth-ldap.so "/etc/openvpn/auth/ldap.conf cn=%u" client-cert-not-required
开启内核路由转发功能
1 echo "net.ipv4.ip_forward = 1" >>/etc/sysctl.confsysctl -p
如果有iptables可以开启iptables策略
1 2 3 iptables -P FORWARD ACCEPT iptables -I INPUT -p tcp --dport 1194 -m comment --comment "openvpn" -j ACCEPT iptables -t nat -A POSTROUTING -s 10.4.82.0/24 -j MASQUERADE
firewalld策略
1 2 3 4 5 6 7 8 9 firewall-cmd --permanent --zone=public --add-port=1194/tcp firewall-cmd --permanent --add-masquerade firewall-cmd --permanent --direct --passthrough ipv4 -t nat -A POSTROUTING -o eth0 -j MASQUERADE firewall-cmd --reload
启动openvpn服务
1 2 $ cd /etc/openvpn/ $ /usr/local/openvpn/sbin/openvpn --daemon --config /etc/openvpn/server.conf
检查服务
1 2 $ netstat -lntup|grep 1194 tcp 0 0 0.0.0.0:1194 0.0.0.0:* LISTEN 48091/openvpn
设置开机启动
1 echo "/usr/local/openvpn/sbin/openvpn --daemon --config /etc/openvpn/server.conf > /dev/null 2>&1 &" >> /etc/rc.local
客户端连接测试 无论我们是在Windows还是Linux OS上Client端的配置,都需要将Client证书、CA证书以及Client配置文件下载下来
现在我们需要先配置一下client.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 cp /data/tools/openvpn-2.4.7/sample/sample-config-files/client.conf /root/ $ cat /root/client.conf client dev tun proto tcp remote 192.168.0.10 1194 resolv-retry infinite nobind persist-key persist-tun ca ca.crt cert client1.crt key client1.key tls-auth ta.key 1 cipher AES-256-CBC comp-lzo verb 3 cert key,我们这里使用用户的证书,所以证书也应当修改为client1.crt和client1.key tls-auth 因为使用加密协议,所以ta.key也需要下载下来
修改后缀并导出
1 2 3 4 5 6 7 8 9 10 [root@vpn ~] [root@vpn ~] mv client.conf client.ovpn sz /root/client.ovpn sz /etc/openvpn/keys/ca.crt sz /etc/openvpn/keys/ta.key sz /usr/local/easy-rsa-old-master/easy-rsa/2.0/keys/client1.crt sz /usr/local/easy-rsa-old-master/easy-rsa/2.0/keys/client1.key
添加用户
以后我们如果想添加用户只需要到cd /usr/local/easy-rsa-old-master/easy-rsa/2.0目录下执行./build-key 用户名,在将keys目录下生成的用户名.crt和key导出,修改一下client.ovpn的用户key名称即可