更新升级 专属应用 系统故障 硬件故障 电脑汽车 鸿蒙刷机 鸿蒙开发Linux教程 鸿蒙开发Linux命令
当前位置:HMXT之家 > 鸿蒙开发Linux教程 > 在Linux系统上安装和配置StrongSwan服务器/客户端

在Linux系统上安装和配置StrongSwan服务器/客户端

更新时间:2023-03-23 10:51:10浏览次数:648+次

本文介绍在Linux系统上安装和配置StrongSwan服务器的方法,同时也演示怎么安装和配置StrongSwan客户端,将以Rocky、AlmaLinux发行版为例说明。StrongSwan是一个开源的、现代的、基于IPsec的VPN解决方案,此多平台解决方案完全支持IKEv1和IKEv2的互联网密钥交换,以在两个主机之间建立安全关联(SA),通过这样做,客户端和服务器之间的连接被加密,并且安全网关被提供给服务器及其网络上可用的其他资源。

在Linux上安装和配置StrongSwan服务器的方法

步骤1:启用内核IP转发

我们将首先在Linux上启用内核IP转发,将以Rocky、AlmaLinux 8/9版本为例说明。这是通过编辑/etc/sysctl.conf文件来完成的:

sudo vim /etc/sysctl.conf

将以下行添加到文件中:

net.ipv4.ip_forward = 1

net.ipv6.conf.all.forwarding = 1

net.ipv4.conf.all.accept_redirects = 0

net.ipv4.conf.all.send_redirects = 0

保存文件并重新加载参数:

$ sudo sysctl -p

net.ipv4.ip_forward = 1

net.ipv6.conf.all.forwarding = 1

net.ipv4.conf.all.accept_redirects = 0

net.ipv4.conf.all.send_redirects = 0

步骤2:安装StrongSwan服务器

StrongSwan软件包在EPEL存储库中提供。要安装它们,请使用以下命令启用EPEL repo:

sudo dnf install epel-release

启用后,使用以下命令安装StrongSwan服务器:

sudo dnf install strongswan libreswan

依赖关系树:

Install  5 Packages

Total download size: 4.5 M

Installed size: 8.7 M

Is this ok [y/N]: y

完成后,验证安装:

$ strongswan version

Linux strongSwan U5.9.8/K5.14.0-70.13.1.el9_0.x86_64

University of Applied Sciences Rapperswil, Switzerland

步骤3:配置StrongSwan服务器

安装后,可以对StrongSwan服务器进行多种配置。通常,配置位于/etc/strongswan/。

如下所示进行验证:

$ ls /etc/strongswan/

ipsec.conf  ipsec.d  ipsec.secrets  strongswan.conf  strongswan.d  swanctl

对于本文,我们将使用IPsec StrongSwan实用程序,因此我们需要对以下内容进行调整:

/etc/strongswan/ipsec.conf:strongswan ipsec子系统的配置文件。

/etc/strongswan/ipsec.secrets:这是机密文件。

a、生成自签名证书

StrongSwan服务器读取/etc/strongswan/ipsec.d/certs中的所有证书。首先设置正确的权限:

mkdir -p ~/pki/{cacerts,certs,private}

chmod 700 ~/pki

安装所需的软件包:

sudo yum install haveged tpm2-abrmd -y

启动并启用服务:

sudo systemctl enable --now haveged

为服务器创建专用证书:

strongswan pki --gen --type rsa --size 4096 --outform pem > ~/pki/private/ca-key.pem

创建根证书颁发机构并对其进行签名:

strongswan pki --self --ca --lifetime 3650 --in ~/pki/private/ca-key.pem \

    --type rsa --dn "CN=VPN root CA" --outform pem > ~/pki/cacerts/ca-cert.pem

为VPN服务器创建私钥:

strongswan pki --gen --type rsa --size 4096 --outform pem > ~/pki/private/server-key.pem

现在生成证书文件:

strongswan pki --pub --in ~/pki/private/server-key.pem --type rsa \

    | strongswan pki --issue --lifetime 1825 \

        --cacert ~/pki/cacerts/ca-cert.pem \

        --cakey ~/pki/private/ca-key.pem \

        --dn "CN=vpn.computingforgeeks.com" --san vpn.computingforgeeks.com --san 192.168.205.12  --san @192.168.205.12 \

        --flag serverAuth --flag ikeIntermediate --outform pem \

    >  ~/pki/certs/server-cert.pem

注:请自行修改替换成自己所需要配置的域名,如将computingforgeeks.com替换成hmxthome.com。另外,拥有额外的--san参数可以为您的IP/域名提供所需的解析。

生成证书后,将证书复制到所需的目录中:

sudo cp -r ~/pki/* /etc/strongswan/ipsec.d/

现在为证书设置以下权限:

sudo chmod -R 775 /etc/strongswan/ipsec.d/

b、配置安全网关

要在StrongSwan上配置安全网关,您需要编辑/etc/strongswan/ipsec.conf。

创建一个备份conf文件:

sudo cp /etc/strongswan/ipsec.conf /etc/strongswan/ipsec.conf.orig

现在打开文件进行编辑:

sudo vim /etc/strongswan/ipsec.conf

在打开的文件中,进行以下调整:

config setup

        charondebug="ike 2, knl 2, cfg 2, net 2, esp 2, dmn 2, mgr 2"

        strictcrlpolicy=no

        uniqueids=no

conn ikev2-vpn

    auto=add

    compress=no

    type=tunnel  # defines the type of connection, tunnel

    keyexchange=ikev2

    fragmentation=yes

    forceencaps=yes

    dpdaction=clear

    dpddelay=300s

    rekey=no

    left=%any

    leftid=@vpn.computingforgeeks.com # if using IP, define it without the @ sign

    leftcert=server-cert.pem  # reads the VPN server cert in /etc/strongswan/ipsec.d/certs

    leftsendcert=always

    leftsubnet=192.168.205.0/24

    right=%any

    rightid=%any

    rightauth=eap-mschapv2

    rightsourceip=192.168.205.0/24

    rightdns=192.168.205.1,8.8.8.8 #DNS to be assigned to clients

    rightsendcert=never

    eap_identity=%identity  # defines the identity the client uses to reply to an EAP Identity request.

    ike=aes128-sha256-ecp256,aes256-sha384-ecp384,aes128-sha256-modp2048,aes128-sha1-modp2048,aes256-sha384-modp4096,aes256-sha256-modp4096,aes256-sha1-modp4096,aes128-sha256-modp1536,aes128-sha1-modp1536,aes256-sha384-modp2048,aes256-sha256-modp2048,aes256-sha1-modp2048,aes128-sha256-modp1024,aes128-sha1-modp1024,aes256-sha384-modp1536,aes256-sha256-modp1536,aes256-sha1-modp1536,aes256-sha384-modp1024,aes256-sha256-modp1024,aes256-sha1-modp1024!

    esp=aes128gcm16-ecp256,aes256gcm16-ecp384,aes128-sha256-ecp256,aes256-sha384-ecp384,aes128-sha256-modp2048,aes128-sha1-modp2048,aes256-sha384-modp4096,aes256-sha256-modp4096,aes256-sha1-modp4096,aes128-sha256-modp1536,aes128-sha1-modp1536,aes256-sha384-modp2048,aes256-sha256-modp2048,aes256-sha1-modp2048,aes128-sha256-modp1024,aes128-sha1-modp1024,aes256-sha384-modp1536,aes256-sha256-modp1536,aes256-sha1-modp1536,aes256-sha384-modp1024,aes256-sha256-modp1024,aes256-sha1-modp1024,aes128gcm16,aes256gcm16,aes128-sha256,aes128-sha1,aes256-sha384,aes256-sha256,aes256-sha1!

在上述文件中:

config setup:定义适用于所有连接的IPSec常规配置信息。

charondebug:指定应该记录多少Charon调试输出。

uniqueids:定义特定参与者ID是否应保持唯一。

conn ikev2-vpn:定义设置的连接名称。

keyexchange:定义要使用的IKE协议的版本。

left:定义左侧参与者的公共网络接口的IP地址。

leftid:指定服务器的域名或IP地址。

leftcert:指定服务器证书的名称。

leftsubnet:定义左侧参与者后面的私有子网。

right:声明权利参与者的公共网络接口的IP地址。

rightsubnet:定义右侧参与者后面的私有子网。

rightsourceip:要分配给客户端的IP地址池。

rightdns:要分配给客户端的DNS。

要查找所有其他参数的更多定义,请阅读ipsec.conf手册页:

man ipsec.conf

c、为对等身份验证配置PSK

我们已经将strongSwan服务器配置为接受客户端连接。接下来,我们需要配置客户端-服务器身份验证凭据,定义RSA私钥并提供EAP用户凭据。

使用以下命令生成密码的字符串:

$ head -c 24 /dev/urandom | base64

7yyoC32RpKeSRDhdEI9Z+XXaL8aTS7V0

生成后,将其添加到下面的文件中:

sudo vim /etc/strongswan/ipsec.secrets

将以下行添加到文件中:

: RSA "server-key.pem"

vpnsecure : EAP "your-secure-password"

保存文件并启动服务:

sudo strongswan start

检查服务的状态:

$ sudo strongswan statusall

Status of IKE charon daemon (strongSwan 5.9.8, Linux 5.14.0-70.13.1.el9_0.x86_64, x86_64):

  uptime: 33 seconds, since Jan 27 10:47:42 2023

  malloc: sbrk 1892352, mmap 0, used 1204176, free 688176

  worker threads: 11 of 16 idle, 5/0/0/0 working, job queue: 0/0/0/0, scheduled: 0

  loaded plugins: charon pkcs11 tpm aesni aes des rc2 sha2 sha1 md4 md5 mgf1 random nonce x509 revocation constraints acert pubkey pkcs1 pkcs7 pkcs12 pgp dnskey sshkey pem openssl gcrypt pkcs8 fips-prf gmp curve25519 chapoly xcbc cmac hmac kdf ctr ccm gcm drbg newhope curl attr kernel-netlink resolve socket-default farp stroke vici updown eap-identity eap-sim eap-aka eap-aka-3gpp eap-aka-3gpp2 eap-md5 eap-gtc eap-mschapv2 eap-dynamic eap-radius eap-tls eap-ttls eap-peap xauth-generic xauth-eap xauth-pam xauth-noauth dhcp led duplicheck unity counters

Virtual IP pools (size/online/offline):

  192.168.205.0/24: 254/0/0

Listening IP addresses:

  192.168.205.12

Connections:

   ikev2-vpn:  %any...%any  IKEv2, dpddelay=300s

   ikev2-vpn:   local:  [vpn.computingforgeeks.com] uses public key authentication

   ikev2-vpn:    cert:  "CN=vpn.computingforgeeks.com"

   ikev2-vpn:   remote: uses EAP_MSCHAPV2 authentication with EAP identity '%any'

   ikev2-vpn:   child:  192.168.205.0/24 === dynamic TUNNEL, dpdaction=none

Security Associations (0 up, 0 connecting):

  none

从上面的输出来看,我们目前与服务器没有关联。允许所需的端口和服务通过防火墙:

sudo firewall-cmd --add-port=500/udp --permanent

sudo firewall-cmd --add-port=4500/udp --permanent

sudo firewall-cmd --permanent --add-service="ipsec"

sudo firewall-cmd --permanent --add-masquerade

sudo firewall-cmd --reload

在Linux上安装和配置StrongSwan客户端的方法

要建立要配置StrongSwan客户端,请安装StrongSwan客户端软件包:

##Debian/Ubuntu

sudo apt update && sudo apt-get install strongswan libcharon-extra-plugins -y

##RHEL/Rocky/Alma Linux

sudo dnf install epel-release && sudo dnf install strongswan -y

从StrongSwan服务器复制ca证书:

##例如

sudo scp rocky9@192.168.205.12:/etc/strongswan/ipsec.d/cacerts/ca-cert.pem ~/

现在将证书复制到IPsec目录:

##Debian/Ubuntu

sudo mv ~/ca-cert.pem /etc/ipsec.d/cacerts/

##RHEL/Rocky/Alma Linux

sudo mv ~/ca-cert.pem /etc/strongswan/ipsec.d/cacerts/

在基于Rhel的客户端上,将SELinux设置为允许模式,以便访问证书:

sudo setenforce 0

在客户端上创建身份验证:

##Debian/Ubuntu

sudo vim /etc/ipsec.secrets

##RHEL/Rocky/AlmaLinux

sudo vim /etc/strongswan/ipsec.secrets

将以下行添加到文件中,根据需要替换值:

vpnsecure : EAP "your-secure-password"

此外,在客户端上创建IPsec配置:

##Debian/Ubuntu

sudo vim /etc/ipsec.conf

##RHEL/Rocky/Alma Linux

sudo vim /etc/strongswan/ipsec.conf

将以下行添加到配置中:

config setup

        # strictcrlpolicy=yes

        # uniqueids = no

conn ikev2-vpn

    right=vpn.computingforgeeks.com

    rightid=@vpn.computingforgeeks.com  # This should match the `leftid` value on your server's configuration

    rightsubnet=192.168.205.0/24

    rightauth=pubkey

    leftsourceip=%config

    leftid=vpnsecure

    leftauth=eap-mschapv2

    leftcacert=ca-cert.pem 

    eap_identity=%identity

    auto=start

现在重新启动服务:

sudo systemctl restart strongswan-starter

现在检查服务的状态:

$ systemctl status strongswan-starter

strongswan-starter.service - strongSwan IPsec IKEv1/IKEv2 daemon using ipsec.conf

Loaded: loaded (/lib/systemd/system/strongswan-starter.service; enabled; vendor preset: enabled)

Active: active (running)

现在,在StrongSwan服务器上,您可以检查状态:

sudo strongswan status

样本输出截图:

\

结论

通过以上的操作,已经实现在两个设备之间创建了一个VPN隧道。您可以使用此连接进行加密,并为服务器及其网络上的其他可用资源提供安全网关。