在本指南中,我们将学习如何在 Rocky Linux 8 上安装和轻松设置 OpenVPN 服务器。 OpenVPN 是一个强大且高度灵活的开源 VPN 软件,它使用 OpenSSL 库的所有加密、身份验证和认证功能通过单个 UDP 或 TCP 端口安全地建立 IP 网络隧道。 它有助于在公共网络上扩展专用网络,同时保持在专用网络中可实现的安全性。
在 Rocky Linux 8 上设置 OpenVPN 服务器
安装 EPEL 存储库
最新的 OpenVPN 软件包由 Rocky Linux 8 和其他类似衍生产品上的 EPEL 存储库提供。 通过运行以下命令,可以在 Rocky Linux 8 上安装 EPEL;
dnf install epel-release -y
在 Rocky Linux 8 上安装 OpenVPN
一旦 EPEL 存储库就位,您现在可以通过执行以下命令在 Rocky Linux 8 上安装 OpenVPN 包;
dnf install openvpn
在 Rocky Linux 8 上安装 Easy-RSA CA 实用程序
Easy-RSA 包是一个基于外壳的 CA 实用程序,用于生成用于保护 VPN 连接的 SSL 密钥对。
dnf install easy-rsa
创建 OpenVPN 公钥基础设施
设置 OpenVPN 服务器的第一步是创建一个 PKI,它由 OpenVPN 服务器和连接客户端的公钥和私钥以及用于签署 OpenVPN 服务器和客户端证书的主证书颁发机构证书和私钥组成。 如果可能,出于安全考虑,您应该在运行 OpenVPN 的单独服务器上创建 PKI。
初始化PKI
Easy-RSA 用于 PKI 管理。 Easy-RSA 脚本安装在 /usr/share/easy-rsa
目录。
为确保 Easy-RSA 所做的任何配置在升级时不会被覆盖,请将脚本复制到不同的目录,最好是在 /etc
目录。
mkdir /etc/easy-rsa
cp -air /usr/share/easy-rsa/3/* /etc/easy-rsa/
一旦脚本就位,导航到目录并初始化 PKI。
cd /etc/easy-rsa/
./easyrsa init-pki
示例命令输出;
init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /etc/easy-rsa/pki
生成证书颁发机构 (CA) 证书和密钥
接下来,通过在上面的 Easy-RSA 目录中运行下面的命令,生成将用于签署证书的 CA 证书和密钥。
./easyrsa build-ca
这将提示您输入 CA 密钥密码和服务器通用名称。
Using SSL: openssl OpenSSL 1.1.1g FIPS 21 Apr 2020 Enter New CA Key Passphrase: Re-Enter New CA Key Passphrase: Generating RSA private key, 2048 bit long modulus (2 primes) ........................................+++++ ....................................................+++++ e is 65537 (0x010001) You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Common Name (eg: your user, host, or server name) [Easy-RSA CA]:Kifarunix-demo CA CA creation complete and you may now import and sign cert requests. Your new CA certificate file for publishing is at: /etc/easy-rsa/pki/ca.crt
CA文件是 /etc/easy-rsa/pki/ca.crt
.
生成 Diffie Hellman 参数
在与上述相同的 Easy-RSA 目录中,执行以下命令以生成 Diffie-Hellman 密钥文件,该文件可用于在与连接客户端进行 TLS 握手期间进行密钥交换。
./easyrsa gen-dh
该命令需要一些时间才能完成。 然后它将 DH 参数存储在 /etc/easy-rsa/pki/dh.pem
文件。
生成 OpenVPN 服务器证书和密钥
要为 OpenVPN 服务器生成证书和私钥,请运行以下命令;
cd /etc/easy-rsa
./easyrsa build-server-full server nopass
当命令运行时,系统会提示您输入上面创建的 CA 密钥密码。
nopass
禁用在证书中使用密码。
Using SSL: openssl OpenSSL 1.1.1g FIPS 21 Apr 2020 Generating a RSA private key .....+++++ ...............................+++++ writing new private key to '/etc/easy-rsa/pki/easy-rsa-10170.VLZsfK/tmp.4TRoOP' ----- Using configuration from /etc/easy-rsa/pki/easy-rsa-10170.VLZsfK/tmp.jTJJ7f Enter pass phrase for /etc/easy-rsa/pki/private/ca.key: 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 3 18:03:20 2023 GMT (825 days) Write out database with 1 new entries Data Base Updated
生成基于哈希的消息身份验证代码 (HMAC) 密钥
要生成 TLS/SSL 预共享身份验证密钥,该密钥将用于向所有 SSL/TLS 握手数据包添加额外的 HMAC 签名,以避免 DoS 攻击和 UDP 端口泛洪,请运行以下命令;
openvpn --genkey --secret /etc/easy-rsa/pki/ta.key
生成吊销证书
为了使之前签署的证书无效,您需要生成一个吊销证书。
./easyrsa gen-crl
Using SSL: openssl OpenSSL 1.1.1g FIPS 21 Apr 2020 Using configuration from /etc/easy-rsa/pki/easy-rsa-10284.mSzk9F/tmp.qdix0A Enter pass phrase for /etc/easy-rsa/pki/private/ca.key: An updated CRL has been created. CRL file: /etc/easy-rsa/pki/crl.pe
吊销证书存储为 /etc/easy-rsa/pki/crl.pem
.
将服务器证书和密钥复制到服务器目录
接下来,将所有生成的证书/密钥复制到 OpenVPN 服务器配置目录。
cp -rp /etc/easy-rsa/pki/{ca.crt,dh.pem,ta.key,crl.pem,issued,private} /etc/openvpn/server/
生成 OpenVPN 客户端证书和密钥
要生成 OpenVPN 客户端证书和私钥,请运行以下命令;
cd /etc/easy-rsa
./easyrsa build-client-full gentoo nopass
样本输出;
Using SSL: openssl OpenSSL 1.1.1g FIPS 21 Apr 2020 Generating a RSA private key ........................+++++ .........................................................................................+++++ writing new private key to '/etc/easy-rsa/pki/easy-rsa-10316.rcXRdS/tmp.tauo7u' ----- Using configuration from /etc/easy-rsa/pki/easy-rsa-10316.rcXRdS/tmp.RxlTaw Enter pass phrase for /etc/easy-rsa/pki/private/ca.key: Check that the request matches the signature Signature ok The Subject's Distinguished Name is as follows commonName :ASN.1 12:'gentoo' Certificate is to be certified until Oct 3 18:05:23 2023 GMT (825 days) Write out database with 1 new entries Data Base Updated
在哪里 gentoo 是为其生成证书和密钥的客户端的名称。 始终为您为其生成证书和密钥的每个客户端使用唯一的通用名称。
为第二个客户端生成,
./easyrsa build-client-full johndoe nopass
将客户端证书和密钥复制到客户端目录
在 OpenVPN 客户端目录上为每个客户端创建一个目录
mkdir /etc/openvpn/client/{gentoo,johndoe}
接下来,将所有客户端生成的证书/密钥和 CA 证书复制到 OpenVPN 客户端配置目录。 你可以
cp -rp /etc/easy-rsa/pki/{ca.crt,issued/gentoo.crt,private/gentoo.key} /etc/openvpn/client/gentoo
cp -rp /etc/easy-rsa/pki/{ca.crt,issued/johndoe.crt,private/johndoe.key} /etc/openvpn/client/johndoe/
在 Rocky Linux 8 上配置 OpenVPN 服务器
OpenVPN 在其文档目录中附带了一个示例配置文件。 将文件复制到 /etc/openvpn/server/
并对其进行修改以满足您的需要。
cp /usr/share/doc/openvpn/sample/sample-config-files/server.conf /etc/openvpn/server/
打开配置进行修改。
vim /etc/openvpn/server/server.conf
该文件受到高度评价。 阅读每个配置选项的注释。
在最基本的形式中,下面是我们的配置选项,没有注释。
port 1194 proto udp4 dev tun ca ca.crt cert issued/server.crt key private/server.key # This file should be kept secret dh dh.pem topology subnet server 10.8.0.0 255.255.255.0 ifconfig-pool-persist ipp.txt push "redirect-gateway def1 bypass-dhcp" push "dhcp-option DNS 208.67.222.222" push "dhcp-option DNS 192.168.10.3" client-to-client keepalive 10 120 tls-auth ta.key 0 # This file is secret cipher AES-256-CBC comp-lzo user nobody group nobody persist-key persist-tun status /var/log/openvpn/openvpn-status.log log-append /var/log/openvpn/openvpn.log verb 3 explicit-exit-notify 1 auth SHA512
Save 并在完成修改后退出配置。
想要为您的 OpenVPN 客户端分配固定/静态 IP 地址? 请按照以下指南进行操作;
为 OpenVPN 客户端分配静态 IP 地址
创建日志目录;
mkdir /var/log/openvpn/
探索配置并进一步微调以满足您的需求。
配置 OpenVPN 服务器路由
为确保来自客户端的流量通过服务器 IP 地址路由(有助于屏蔽客户端 IP 地址),您需要在 OpenVPN 服务器上启用 IP 转发;
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
运行以下命令以在不重新启动服务器的情况下使更改生效。
sysctl --system
允许 OpenVPN 服务端口通过防火墙
firewall-cmd --add-port=1194/udp --permanent
激活 IP 伪装
firewall-cmd --add-masquerade --permanent
将在指定 OpenVPN 子网(例如,在我们的示例中为 10.8.0.0/24)上接收到的流量转发到将通过其发送数据包的接口。
通过运行以下命令查找发送数据包的接口;
ip route get 8.8.8.8
8.8.8.8 via 10.0.2.2 dev enp0s3 src 10.0.2.15 uid 0
cache
对于您的情况,定义的接口名称和子网可能不同。 相应地更换它们。
firewall-cmd --permanent --direct --passthrough ipv4 -t nat -A POSTROUTING -s 10.8.0.0/24 -o enp0s3 -j MASQUERADE
重新加载 firewalld 以使更改生效。
firewall-cmd --reload
启动并设置 OpenVPN 在系统启动时运行。
systemctl enable --now [email protected]
当 OpenVPN 服务运行时,它会创建一个隧道接口,tun0;
ip add s
4: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 100
link/none
inet 10.8.0.1/24 brd 10.8.0.255 scope global tun0
valid_lft forever preferred_lft forever
inet6 fe80::afd7:17a6:57ee:7f3b/64 scope link stable-privacy
valid_lft forever preferred_lft forever
检查日志;
tail /var/log/openvpn/openvpn.log
/sbin/ip addr add dev tun0 10.8.0.1/24 broadcast 10.8.0.255 Socket Buffers: R=[212992->212992] S=[212992->212992] UDPv4 link local (bound): [AF_INET][undef]:1194 UDPv4 link remote: [AF_UNSPEC] GID set to nobody UID set to nobody MULTI: multi_init called, r=256 v=256 IFCONFIG POOL: base=10.8.0.2 size=252, ipv6=0 IFCONFIG POOL LIST Initialization Sequence Completed
您的 OpenVPN 服务器现已启动并运行。 关于如何在 Rocky Linux 8 上安装和设置 OpenVPN 服务器的指南到此结束。
您现在可以继续配置 OpenVPN 客户端并通过 vpn 服务器将它们互连。
在 Rocky Linux 8 上安装和配置 OpenVPN 客户端
为 OpenVPN 客户端分配静态 IP 地址
配置基于 OpenVPN LDAP 的身份验证