#!/bin/bash
# 一键部署OpenVPN服务脚本
# 更新系统并安装必要的软件包
sudo apt-update
sudo apt-get update
sudo apt-get install -y openvpn easy-rsa
# 创建所需的目录和文件
mkdir -p ~/easy-rsa/easyrsa3/vars
touch ~/easy-rsa/easyrsa3/vars/{vars.example,build-vars}
# 设置Easy-RSA变量
echo "set_var EASYRSA_REQ_COUNTRY \"CN\"" >> ~/easy-rsa/easyrsa3/vars/build-vars
echo "set_var EASYRSA_REQ_PROVINCE \"Beijing\"" >> ~/easy-rsa/easyrsa3/vars/build-vars
echo "set_var EASYRSA_REQ_CITY \"Beijing\"" >> ~/easy-rsa/easyrsa3/vars/build-vars
echo "set_var EASYRSA_REQ_ORG \"Fabric\"" >> ~/easy-rsa/easyrsa3/vars/build-vars
echo "set_var EASYRSA_REQ_EMAIL \"test@example.com\"" >> ~/easy-rsa/easyrsa3/vars/build-vars
echo "set_var EASYRSA_REQ_OU \"Fabric CA\"" >> ~/easy-rsa/easyrsa3/vars/build-vars
# 初始化PKI
cd ~/easy-rsa/easyrsa3
./easyrsa init-pki
# 构建CA
./easyrsa build-ca nopass
# 创建服务端证书
./easyrsa build-server-full server nopass
# 生成Diffie-Hellman参数
./easyrsa gen-dh
# 创建HMAC签名
openvpn --genkey --secret ta.key
# 复制证书和密钥到OpenVPN目录
cp ~/easy-rsa/easyrsa3/pki/ca.crt /etc/openvpn
cp ~/easy-rsa/easyrsa3/pki/issued/server.crt /etc/openvpn
cp ~/easy-rsa/easyrsa3/pki/private/server.key /etc/openvpn
cp ~/easy-rsa/easyrsa3/pki/dh.pem /etc/openvpn
cp ~/easy-rsa/easyrsa3/ta.key /etc/openvpn
# 复制OpenVPN配置文件
cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn
cd /etc/openvpn
gzip -d server.conf.gz
# 修改OpenVPN服务器配置文件
sed -i 's/port 1194/port 5555/' server.conf
sed -i 's/proto udp/proto tcp/' server.conf
sed -i 's/dev tun/dev tun\nuser nobody\ngroup nogroup/' server.conf
sed -i 's/ca ca.crt/ca \/etc\/openvpn\/ca.crt/' server.conf
sed -i 's/cert server.crt/cert \/etc\/openvpn\/server.crt/' server.conf
sed -i 's/key server.key/key \/etc\/openvpn\/server.key/' server.conf
sed -i 's/dh dh.pem/dh \/etc\/openvpn\/dh.pem/' server.conf
sed -i 's/tls-auth ta.key 0/tls-auth \/etc\/openvpn\/ta.key 0/' server.conf
sed -i 's/cipher AES-256-CBC/cipher AES-256-CBC\ncom
评论已关闭