跳转至

Ubuntu部署strongSwan客户端

安装

sudo apt update
sudo apt install strongswan -y

配置sudo nano /etc/ipsec.conf

config setup


conn %default
    ikelifetime=86400s
    keylife=43200s
    rekeymargin=3m
    keyingtries=1
    keyexchange=ikev2

conn gre-over-ipsec-ct
    keyexchange=ikev2
    ike=aes256-sha256-modp1024!
    esp=aes256-sha256-modp1024!

    dpddelay=10s
    dpdtimeout=30s
    dpdaction=restart


#   考虑使用left=interface:enp1s0指定接口
    left=%any
    leftid=198.19.0.14
#    leftprotoport=gre
    leftsubnet=198.19.0.14/32[gre]

    right=198.51.100.221
    rightid=%any
#    rightprotoport=gre
    rightsubnet=198.51.100.221/32[gre]

    authby=secret
    type=tunnel
    auto=start

sudo nano /etc/ipsec.secrets,密码

198.19.0.14      198.51.100.221    : PSK       "xxxxxxxxx"
# 把loopback地址添加到lo口
sudo ip addr add 198.19.0.12/32 dev lo

# 1. 创建名为 tunnel1 的 GRE 隧道接口,绑定物理 IP
sudo ip tunnel add tunnel1 mode gre local 198.19.0.12 remote 198.51.100.221 ttl 255

# 2. 启动该接口
sudo ip link set tunnel1 up

# 3. 为 GRE 隧道配置内网 IP (对应飞塔 tunnel1 配置)
sudo ip addr add 198.19.11.6/30 peer 198.19.11.5 dev tunnel1

串联、旁挂

network:
  version: 2
  renderer: networkd
  ethernets:
    lo:
      addresses:
        - 127.0.0.1/8
        - ::1/128
        - 198.19.0.14/32
    enp1s0:
      dhcp4: no
      dhcp6: no
      optional: true
      addresses:
        - 10.0.0.20/24
      routes:
      - to: 198.51.100.221/32
        via: 10.1.0.1
    enp2s0:
      dhcp4: no
      optional: true
    enp3s0:
      dhcp4: no
      optional: true
    enp4s0:
      dhcp4: no
      optional: true
    enp5s0:
      dhcp4: no
      optional: true
    enp6s0:
      dhcp4: no
      optional: true
  bridges:
    br0:
      interfaces: [enp2s0, enp3s0, enp4s0, enp5s0, enp6s0]
      addresses: [172.20.22.254/24]
      dhcp6: no
      parameters:
        stp: false
        forward-delay: 0
  tunnels:
    tunnel1:
      mode: gre
      local: 198.19.0.14
      remote: 203.0.113.242
      ttl: 255
      addresses:
        - 198.19.11.18/30
      routes:
        - to: 0.0.0.0/0
          via: 198.19.11.17
      mtu: 1360

单臂

network:
  version: 2
  renderer: networkd
  ethernets:
    lo:
      addresses:
        - 127.0.0.1/8
        - ::1/128
        - 198.19.0.12/32
    enp2s0:
      optional: true
      dhcp4: false
      dhcp6: false
    enp3s0:
      optional: true
      dhcp4: false
    enp4s0:
      optional: true
      dhcp4: false
    enp5s0:
      optional: true
      dhcp4: false
  bridges:
    br0:
      addresses:
      - "10.240.0.254/24"
      routes:
      - to: 198.51.100.221/32
        via: 10.240.0.1
      dhcp6: false
      interfaces:
      - enp2s0
      - enp3s0
      - enp4s0
      parameters:
        forward-delay: "0"
        stp: false
  tunnels:
    tunnel1:
      mode: gre
      local: 198.19.0.12
      remote: 198.51.100.221
      ttl: 255
      addresses:
        - 198.19.11.6/30
      routes:
        - to: 0.0.0.0/0
          via: 198.19.11.5
      mtu: 1360

修改 wait-online 服务的超时参数,缩短strongswan的开机启动等待时间

执行以下命令来创建一个覆盖配置:

sudo systemctl edit systemd-networkd-wait-online.service

在打开的编辑器中,填入以下内容:

[Service]# 先清空原有的 ExecStart 才能重新赋值
ExecStart=
# 将超时时间从默认的 120s 缩减到 15s
ExecStart=/usr/lib/systemd/systemd-networkd-wait-online --timeout=15



[Service]
ExecStart=
# 只监控 br0,只要 br0 就绪就立刻通过,不再等其他接口,且超时设为 10s
ExecStart=/usr/lib/systemd/systemd-networkd-wait-online -i br0 --timeout=10
[Service]
ExecStart=
ExecStart=/usr/lib/systemd/systemd-networkd-wait-online --timeout=15



[Service]
ExecStart=
ExecStart=/usr/lib/systemd/systemd-networkd-wait-online -i br0 --timeout=10