如何在Ubuntu 20.04 | 18.04 | 16.04上安装Libreswan

时间:2020-02-23 14:44:43  来源:igfitidea点击:

如何在Ubuntu 20.04/18.04/16.04服务器上安装Libreswan的教程。
Libreswan是Linux系统的互联网密钥交换(IKE)实现。
它支持与IPsec相关的IKEv1和IKEv2和其他扩展(RFC + IETF草案),包括IKEv2,X.509数字证书,NAT遍历和许多其他。

Libreswan从OpenSwan 2.6.38分叉,它默认使用本机Linux IPsec堆栈(NetKey/XFRM)。
我们将在源自源Ubuntu 20.04/18.04/16.04上安装Libreswan。

第1步:更新系统

确保我们正在使用Ubuntu的最新安装。

sudo apt -y update && sudo apt -y upgrade
sudo reboot

系统提升后,继续执行第二步。

第2步:安装构建依赖项

Libreswan从源编译有几个包。
通过运行以下命令将它们安装在Ubuntu系统上。

sudo apt install libnss3-dev libnspr4-dev pkg-config libpam-dev \
 libcap-ng-dev libcap-ng-utils libselinux-dev \
 libcurl3-nss-dev flex bison gcc make libldns-dev \
 libunbound-dev libnss3-tools libevent-dev xmlto \
 libsystemd-dev git devscripts build-essential fakeroot libsystemd-dev

第3步:下载Libreswan源代码

由于我们将从源中构建Libreswan,以确保我们使用最新版本。
从GitHub克隆项目。

SWAN_VER=3.32
swan_file="libreswan-$SWAN_VER.tar.gz"
swan_url1="https://github.com/libreswan/libreswan/archive/v$SWAN_VER.tar.gz"
swan_url2="https://download.libreswan.org/$swan_file"
if ! { wget -t 3 -T 30 -nv -O "$swan_file" "$swan_url1" || wget -t 3 -T 30 -nv -O "$swan_file" "$swan_url2"; }; then
  exit 1
fi

下载文件后,提取它。

tar xzf "$swan_file" && /bin/rm -f "$swan_file"

第4步:在Ubuntu上构建和安装Libreswan

我们现在可以在Ubuntu上构建和安装Libreswan。
第一次更改为libreswan目录。

cd "libreswan-$SWAN_VER" || exit 1

创建构建选项文件。

cat > Makefile.inc.local <<'EOF'
WERROR_CFLAGS =
USE_DNSSEC = false
USE_DH31 = false
USE_NSS_AVA_COPY = true
USE_NSS_IPSEC_PROFILE = false
USE_GLIBC_KERN_FLIP_HEADERS = true
EOF

最后构建了Libreswan。

NPROCS=$(grep -c ^processor /proc/cpuinfo)
[ -z "$NPROCS" ] && NPROCS=1
make "-j$((NPROCS+1))" -s base && sudo make -s install-base

成功的构建应输出。

.......................................................
running: systemctl --system daemon-reload
running: systemd-tmpfiles --create /usr/lib/tmpfiles.d/libreswan.conf
DESTDIR=''
** **** **** **** **** **** **** WARNING ** **** **** **** **** **** **** **** *****
The ipsec service is currently disabled. To enable this service issue:
 systemctl enable ipsec.service
** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****
../../OBJ.linux.x86_64/testing/enumcheck/enumcheck -> /usr/local/libexec/ipsec/enumcheck
../../OBJ.linux.x86_64/testing/ipcheck/ipcheck -> /usr/local/libexec/ipsec/ipcheck
../../OBJ.linux.x86_64/testing/fmtcheck/fmtcheck -> /usr/local/libexec/ipsec/fmtcheck
../../OBJ.linux.x86_64/testing/timecheck/timecheck -> /usr/local/libexec/ipsec/timecheck

如果安装成功。
我们应该能够检查版本。

$/usr/local/sbin/ipsec --version
Linux Libreswan 3.32 (netkey) on 5.4.0-26-generic

第5步:启动和启用LibleSwan IPSec服务

IPSec服务当前已禁用。
要启用此服务问题:

sudo systemctl enable --now ipsec.service

验证服务状态。

$systemctl status ipsec.service
● ipsec.service - Internet Key Exchange (IKE) Protocol Daemon for IPsec
   Loaded: loaded (/lib/systemd/system/ipsec.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2019-06-29 09:17:26 CEST; 37s ago
     Docs: man:ipsec(8)
           man:pluto(8)
           man:ipsec.conf(5)
 Main PID: 13782 (pluto)
   Status: "Startup completed."
    Tasks: 2 (limit: 2299)
   CGroup: /system.slice/ipsec.service
           └─13782 /usr/local/libexec/ipsec/pluto --leak-detective --config /etc/ipsec.conf --nofork
Jun 29 09:17:26 ubuntu16 pluto[13782]: adding interface eth0/eth0 116.203.48.203:4500
Jun 29 09:17:26 ubuntu16 pluto[13782]: Kernel supports NIC esp-hw-offload
Jun 29 09:17:26 ubuntu16 pluto[13782]: adding interface lo/lo (esp-hw-offload=no) 127.0.0.1:500
Jun 29 09:17:26 ubuntu16 pluto[13782]: adding interface lo/lo 127.0.0.1:4500
Jun 29 09:17:26 ubuntu16 pluto[13782]: Kernel supports NIC esp-hw-offload
Jun 29 09:17:26 ubuntu16 pluto[13782]: adding interface lo/lo (esp-hw-offload=no) ::1:500
Jun 29 09:17:26 ubuntu16 pluto[13782]: Kernel supports NIC esp-hw-offload
Jun 29 09:17:26 ubuntu16 pluto[13782]: adding interface eth0/eth0 (esp-hw-offload=no) 2a01:4f8:c2c:83a2::1:500
Jun 29 09:17:26 ubuntu16 pluto[13782]: loading secrets from "/etc/ipsec.secrets"
Jun 29 09:17:26 ubuntu16 pluto[13782]: no secrets filename matched "/etc/ipsec.d/*.secrets"