由于疫情的影响,姜同学要居家办公啦,远程桌面如teamViewer,向日葵之类用的总是不爽,姜同学虽然水平不咋地,但是怎么说也是一名程序员,该有的的极客精神还是有的,so姜同学决定在公司内网搭建vpn,快乐自己的同时也能方便其它同事,这可能就是程序员的快了吧。 于是姜同学便是踏上了漫漫的google之旅,功夫不负有心人,姜同学发现了openvpn这款产品以及Nyr大佬在github上开源的openvpn搭建脚本。
首先献上大佬的github地址:https://github.com/Nyr/openvpn-install,感谢大佬
This server is behind NAT. What is the public IPv4 address or hostname?
Public IPv4 address / hostname [101.33.76.24)]: #公网IP,默认就好,我这是假的
Which protocol should OpenVPN use?
1) UDP (recommended)
2) TCP
Protocol [1]: 2 #协议我这里使用TCP
What port should OpenVPN listen to?
Port [1194]: #端口可以额外制定,或者默认1194
Select a DNS server for the clients:
1) Current system resolvers
2) Google
3) 1.1.1.1
4) OpenDNS
5) Quad9
6) AdGuard
DNS server [1]: 1 #DNS我这里选择1 系统默认
Enter a name for the first client:
Name [client]: jiangtongxue #创建用户
...
The client configuration is available in: /root/jiangtongxue.ovpn
New clients can be added by running this script again.
[root@VM-0-10-centos ~]# sz /root/jiangtongxue.ovpn #下载证书
https://openvpn.net/vpn-client/
大佬的一键安装脚本讲服务端的证书,配置以及用来生成证书的easy-rsa都放在/etc/openvpn/server下面了,生成新的客户端证书需要用到最开始的ca证书,所以我们把大佬的easy-rsa复制一份就好了。
cp -r /etc/openvpn/server/easy-rsa /etc/openvpn/client/
创建用户脚本
# ! /bin/bash
set -e
OVPN_USER_KEYS_DIR=/etc/openvpn/client/jiangtongxue/keys
EASY_RSA_DIR=/etc/openvpn/client/easy-rsa
PKI_DIR=$EASY_RSA_DIR/pki
for user in "$@"
do
if [ -d "$OVPN_USER_KEYS_DIR/$user" ]; then
rm -rf $OVPN_USER_KEYS_DIR/$user
rm -rf $PKI_DIR/reqs/$user.req
sed -i '/'"$user"'/d' $PKI_DIR/index.txt
fi
cd $EASY_RSA_DIR
# 生成客户端SSL证书文件
./easyrsa build-client-full $user nopass
# 整理下生成的文件
mkdir -p $OVPN_USER_KEYS_DIR/$user
cp $PKI_DIR/ca.crt $OVPN_USER_KEYS_DIR/$user/ # CA 根证书
cp $PKI_DIR/issued/$user.crt $OVPN_USER_KEYS_DIR/$user/ # 客户端证书
cp $PKI_DIR/private/$user.key $OVPN_USER_KEYS_DIR/$user/ # 客户端证书密钥
cp /etc/openvpn/client/sample.ovpn $OVPN_USER_KEYS_DIR/$user/$user.ovpn # 客户端配置文件
sed -i 's/client.crt/'"$user".crt'/g' $OVPN_USER_KEYS_DIR/$user/$user.ovpn
sed -i 's/client.key/'"$user".key'/g' $OVPN_USER_KEYS_DIR/$user/$user.ovpn
cd $OVPN_USER_KEYS_DIR
zip -r $user.zip $user
done
exit 0
别忘了/etc/openvpn/client/sample.ovpn哦,用大佬生成的jjiangtongxue.ov**就可以了
sh create_vpn_user.sh jiangtongxue
client
dev tun
proto tcp
remote ip你也想看? 1194
resolv-retry infinite
nobind
persist-key
persist-tun
remote-cert-tls server
auth SHA512
cipher AES-256-CBC
ignore-unknown-option block-outside-dns
block-outside-dns
verb 3
<cert>
jiangtongxue.crt
</cert>
<key>
jiangtongxue.key
</key>
然后将证书导入openvpn客户端就可以快乐上网啦~
del_vpn_user.sh
# ! /bin/bash
set -e
OVPN_USER_KEYS_DIR=/etc/openvpn/client/jiangtongxue/keys
EASY_RSA_DIR=/etc/openvpn/client/easy-rsa/
for user in "$@"
do
cd $EASY_RSA_DIR
echo -e 'yes\n' | ./easyrsa revoke $user
./easyrsa gen-crl
# 吊销掉证书后清理客户端相关文件
if [ -d "$OVPN_USER_KEYS_DIR/$user" ]; then
rm -rf $OVPN_USER_KEYS_DIR/${user}*
fi
systemctl restart openvpn-server@server.service
done
exit 0
服务器异常关机啦,打开之后v**可以连接但是却无法上网。 执行如下命令
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j MASQUERADE