返回更新列表
新发布Sep 8, 2026

strongswan v6.1.0

strongSwan - 基于IPsec的VPN

分享

strongSwan 配置

概述

strongSwan 是一个基于 IPsec 的开源 VPN 解决方案。

本文档只是对 strongSwan swanctl 命令的简短介绍, 它使用了现代的 vici 多功能 IKE 配置接口。已弃用的 ipsec 命令使用传统的 stroke 配置接口,见 这里。 如需更详细的信息,请查阅手册页、我们新的 文档站点 以及旧的 wiki。

快速入门

用户、主机和网关的证书由一个虚构的 strongSwan CA 签发。在我们的示例场景中,CA 证书 strongswanCert.pem 必须存在于所有 VPN 端点上,以便能够认证 对端。对于您的特定 VPN 应用,您既可以使用来自任何第三方 CA 的证书,也可以 使用 strongSwan pki 工具自己生成所需的私钥和证书, 该工具的使用方法将在下面的某个 章节中介绍。

站点到站点场景

在此场景中,两个安全网关 moon 和 sun 将通过在两个网关之间建立的 VPN 隧道,将两个子网 moon-net 和 sun-net 相互连接:

10.1.0.0/16 -- | 192.168.0.1 | === | 192.168.0.2 | -- 10.2.0.0/16
  moon-net          moon                 sun           sun-net

网关 moon 上的配置:

/etc/swanctl/x509ca/strongswanCert.pem
/etc/swanctl/x509/moonCert.pem
/etc/swanctl/private/moonKey.pem

/etc/swanctl/swanctl.conf:

    connections {
        net-net {
            remote_addrs = 192.168.0.2

            local {
                auth = pubkey
                certs = moonCert.pem
            }
            remote {
                auth = pubkey
                id = "C=CH, O=strongSwan, CN=sun.strongswan.org"
            }
            children {
                net-net {
                    local_ts  = 10.1.0.0/16
                    remote_ts = 10.2.0.0/16
                    start_action = trap
                }
            }
        }
    }

网关 sun 上的配置:

/etc/swanctl/x509ca/strongswanCert.pem
/etc/swanctl/x509/sunCert.pem
/etc/swanctl/private/sunKey.pem

/etc/swanctl/swanctl.conf:

    connections {
        net-net {
            remote_addrs = 192.168.0.1

            local {
                auth = pubkey
                certs = sunCert.pem
            }
            remote {
                auth = pubkey
                id = "C=CH, O=strongSwan, CN=moon.strongswan.org"
            }
            children {
                net-net {
                    local_ts  = 10.2.0.0/16
                    remote_ts = 10.1.0.0/16
                    start_action = trap
                }
            }
        }
    }

此场景中使用的本地和远程身份是终端实体证书中包含的 subjectDistinguishedNames。 证书和私钥通过以下命令加载到 charon 守护进程中:

swanctl --load-creds

而

swanctl --load-conns

加载 swanctl.conf 中定义的连接。使用 start_action = trap 时,IPsec 连接会在第一个想要通过隧道的明文负载 IP 数据包出现时自动建立。

主机到主机场景

这是两台没有子网在其后的单主机之间的设置。虽然 IPsec 传输模式足以用于主机到主机的连接,但我们将使用默认的 IPsec 隧道模式。

| 192.168.0.1 | === | 192.168.0.2 |
     moon                sun

主机 moon 上的配置:

/etc/swanctl/x509ca/strongswanCert.pem
/etc/swanctl/x509/moonCert.pem
/etc/swanctl/private/moonKey.pem

/etc/swanctl/swanctl.conf:

    connections {
        host-host {
            remote_addrs = 192.168.0.2

            local {
                auth=pubkey
                certs = moonCert.pem
            }
            remote {
                auth = pubkey
                id = "C=CH, O=strongSwan, CN=sun.strongswan.org"
            }
            children {
                net-net {
                    start_action = trap
                }
            }
        }
    }

主机 sun 上的配置:

/etc/swanctl/x509ca/strongswanCert.pem
/etc/swanctl/x509/sunCert.pem
/etc/swanctl/private/sunKey.pem

/etc/swanctl/swanctl.conf:

    connections {
        host-host {
            remote_addrs = 192.168.0.1

            local {
                auth = pubkey
                certs = sunCert.pem
            }
            remote {
                auth = pubkey
                id = "C=CH, O=strongSwan, CN=moon.strongswan.org"
            }
            children {
                host-host {
                    start_action = trap
                }
            }
        }
    }

远程访问用户场景

这是一种非常常见的场景:strongSwan 网关为任意数量的远程 VPN 客户端提供服务,这些客户端通常具有动态 IP 地址。

10.1.0.0/16 -- | 192.168.0.1 | === | x.x.x.x |
  moon-net          moon              carol

网关 moon 上的配置:

/etc/swanctl/x509ca/strongswanCert.pem
/etc/swanctl/x509/moonCert.pem
/etc/swanctl/private/moonKey.pem

/etc/swanctl/swanctl.conf:

    connections {
        rw {
            local {
                auth = pubkey
                certs = moonCert.pem
                id = moon.strongswan.org
            }
            remote {
                auth = pubkey
            }
            children {
                net-net {
                    local_ts  = 10.1.0.0/16
                }
            }
        }
    }

远程访问用户 carol 的配置:

/etc/swanctl/x509ca/strongswanCert.pem
/etc/swanctl/x509/carolCert.pem
/etc/swanctl/private/carolKey.pem

/etc/swanctl/swanctl.conf:

    connections {
        home {
            remote_addrs = moon.strongswan.org

            local {
                auth = pubkey
                certs = carolCert.pem
                id = [email protected]
            }
            remote {
                auth = pubkey
                id = moon.strongswan.org
            }
            children {
                home {
                    local_ts  = 10.1.0.0/16
                    start_action = start
                }
            }
        }
    }

对于 remote_addrs,选择了主机名 moon.strongswan.org,它将在运行时由 DNS 解析为相应的目标 IP 地址。在此场景中,远程访问用户 carol 的身份是电子邮件地址 [email protected],该地址必须作为 subjectAlternativeName 包含在远程访问用户证书 carolCert.pem 中。

带虚拟 IP 的远程访问用户场景

远程访问用户通常拥有其当前所连接 ISP 分配的动态 IP 地址。为了简化从 moon-net 返回到远程访问客户端 carol 的路由,如果远程访问用户拥有一个从预定义地址池中选择的内部 IP 地址,那将是理想的。

10.1.0.0/16 -- | 192.168.0.1 | === | x.x.x.x | -- 10.3.0.1
  moon-net          moon              carol       virtual IP

在我们的示例中,虚拟 IP 地址从地址池 10.3.0.0/16 中选择,可以通过添加以下小节来配置:

pools {
    rw_pool {
        addrs = 10.3.0.0/16
    }
}

将其添加到网关的 swanctl.conf 中,使用以下命令将它们加载到 charon 守护进程中:

swanctl --load-pools

要从该地址池请求 IP 地址,远程访问用户可以使用 IKEv1 模式配置或 IKEv2 配置负载。两者的配置相同:

vips = 0.0.0.0

网关 moon 上的配置:

/etc/swanctl/x509ca/strongswanCert.pem
/etc/swanctl/x509/moonCert.pem
/etc/swanctl/private/moonKey.pem

/etc/swanctl/swanctl.conf:

    connections {
        rw {
            pools = rw_pool

            local {
                auth = pubkey
                certs = moonCert.pem
                id = moon.strongswan.org
            }
            remote {
                auth = pubkey
            }
            children {
                net-net {
                    local_ts  = 10.1.0.0/16
                }
            }
        }
    }

    pools {
        rw_pool {
            addrs = 10.30.0.0/16
        }
    }

远程访问用户 carol 的配置:

/etc/swanctl/x509ca/strongswanCert.pem
/etc/swanctl/x509/carolCert.pem
/etc/swanctl/private/carolKey.pem

/etc/swanctl/swanctl.conf:

    connections {
        home {
            remote_addrs = moon.strongswan.org
            vips = 0.0.0.0

分类