安装 StrongSwan

在 Ubuntu 上安装

  1. 打开终端并运行以下命令安装 strongswan 和相关工具:
    sudo apt-get install strongswan strongswan-ipsec-tools
  2. 安装完成后,生成默认的密钥:
    ipsecctl --generate-keypair
  3. 启动 strongswan 服务:
    sudo systemctl start strongswan
  4. 使其自动启动:
    sudo systemctl enable strongswan

在 CentOS/RHEL 上安装

  1. 安装 EPEL 和其他必要的存储库:
    yum install epel-release
  2. 安装 strongswan 和相关工具:
    yum install strongswan strongswan-ipsec-tools
  3. 生成默认的密钥:
    ipsecctl --generate-keypair
  4. 启动并启用服务:
    systemctl start strongswan
    systemctl enable strongswan

配置 StrongSwan

配置文件路径

默认情况下,strongswan 的配置文件位于 /etc/strongswan/,主要文件包括:

  • /etc/strongswan/strongswan.conf:主配置文件
  • /etc/strongswan/ikev2.conf:IKEv2 配置文件
  • /etc/strongswan/ipseckdf.conf(可选):IPsec KDF 配置

编辑配置文件

  1. 打开配置文件 /etc/strongswan/strongswan.conf
    sudo nano /etc/strongswan/strongswan.conf
  2. [strongswan] 部分添加以下内容:
    charon {
        load_modular = yes
        save_serial = yes
        # 生成唯一的序列号
        unique_serial = yes
        # 启用IPsec KDF
        ipsec_use_kdf = yes
        # 使用AES-GCM加密(可选)
        aes_gcm = yes
        # 使用Des-Hmac(可选)
        des_hmac = yes
    }
  3. 保存并退出编辑器。

配置 IKEv2

打开 /etc/strongswan/ikev2.conf

sudo nano /etc/strongswan/ikev2.conf
v2 {
    # 生成一个新的 IKEv2 密钥
    keyexchange = ikev2
    # 生成新的初始密钥(建议使用SHA1)
    initial_lifetime = 360
    # 重启密钥间隔(可选)
    rekey_interval = 360
    # 生成新的 IPsec 密钥
    ipsec_lifetime = 360
    # 生成新的 IPsec 密钥间隔
    ipsec_rekey_interval = 360
    # 使用AES-GCM加密
    encryption = aes_gcm
    # 使用HMAC-SHA1
    hashing = hmac_sha1
    # 生成新的 Diffie-Hellman 密钥
    dh_group = dh_2048
    # 生成新的 Diffie-Hellman 参数
    dh_parameters = 2048
}

保存并退出编辑器。

配置 IPsec KDF

如果需要使用 IPsec KDF(可选),打开 /etc/strongswan/ipseckdf.conf

sudo nano /etc/strongswan/ipseckdf.conf
# IPsec KDF配置示例
# 使用 SHA1 加密
hashing {
    # 使用 HMAC-SHA1
    type = hmac_sha1
}

保存并退出编辑器。


配置完成后

  1. 重新加载 strongswan 服务:
    sudo systemctl reload strongswan
  2. 检查服务状态:
    systemctl status strongswan
  3. 查看日志以确保配置正确:
    journalctl -u strongswan

测试配置

  1. 生成一个新的 IKEv2 密钥
    ipsecctl --add-certificate-to-file server-certs/server.pem
  2. 查看现有的密钥或证书
    ipsecctl --list-certs
  3. 连接测试
    ipsecctl --connect

    或者:

    ipsecctl --socket /var/run/ipsec/some.socket --connect

常见配置问题

问题:连接无法建立

  • 检查防火墙规则,确保 IKE 和 IPsec 转发组合接口(如 NAT 和专用接口)。
  • 检查密钥长度是否足够(建议至少 256 位)。
  • 确保使用相同的证书或密钥在客户端和服务器端。

问题:认证失败

  • 检查证书的颁发者是否被正确添加到 CA 证书存储中。
  • 确保服务器和客户端的密钥或证书匹配。

问题:性能优化

  • 调整 MTU
    echo 150 > /proc/sys/net/ipv4/ip_forward_mtu
    echo 300 > /proc/sys/net/ipv6/ip_forward_mtu
  • Fragmentation
    ip route add 0.../ src 192.168.1.1 dev eth fragment 1

高级配置

使用证书

  1. 生成自签名证书:
    openssl req -x509 -newkey rsa:2048 -keyout server.pem -out server.pem -days 365 -nodes
  2. 将证书添加到 strongswan
    ipsecctl --add-certificate-to-file server-certs/server.pem

使用预定义密钥

  1. 生成一个预定义密钥:
    ipsecctl --generate-keypair
  2. 将密钥添加到服务器:
    ipsecctl --set-certificate certs/server.pem --set-private-key private.pem

故障排除

问题:服务无法启动

  • 检查配置文件是否正确:
    nano /etc/strongswan/strongswan.conf
  • 确保没有语法错误。
  • 重新加载服务:
    sudo systemctl reload strongswan

问题:连接超时

  • 调整 initial_lifetimerekey_interval
    # 在 Ikev2 配置中
    initial_lifetime = 360
    rekey_interval = 360

进一步优化

启用 fragmentation

strongswan.conf 中添加以下内容:

# 启用 fragmentation
fragmentation = yes

保存并重新加载服务:

sudo nano /etc/strongswan/strongswan.conf
sudo systemctl reload strongswan

调整 MTU

如前所述,调整 /proc/sys/net/ipv4/ip_forward_mtu/proc/sys/net/ipv6/ip_forward_mtu


通过以上步骤,您可以配置并优化 StrongSwan 来满足您的网络需求,常见的故障排除问题包括配置错误、密钥管理、防火墙规则和性能优化,如果有更多问题,可以参考 StrongSwan documentation 或社区求助。

IKEv2配置示例

扫码添加小飞机VPN网络工具官方微信

扫码添加小飞机VPN网络工具官方微信

400-638-2751
扫码添加小飞机VPN网络工具官方微信

扫码添加小飞机VPN网络工具官方微信

网站地图