「Linux」- 通过 NetworkManager 连接 L2TP over IPSec 服务

  CREATED BY JENKINSBOT

问题描述

该笔记将记录:在 Linux 中,如何使用 Network Manager 连接 L2TP/IPSec VPN 服务。

解决方案

补充说明:
1)本文中的“NetworkManager”实际上指的是使用NetworkManger的前端程序。
2)操作系统:Ubuntu 16.04 TLS

通过 nmcli(1) 命令(CLI)

nmcli connection add type vpn con-name k4nz00 ifname ppp0 vpn-type l2tp

通过 nm-applet 程序(GUI)

在 GNOME 中,系统托盘的网络设置功能便是 nm-applet 提供的。这里使用 nm-applet(1) 来实现 VPN 配置,它常用是 Network Manager 的 GUI 程序。

流程概览

配置过程大致可以分为以下几个步骤:

  1. 安装NetworkManage及相关的软件包;
  2. 创建VPN连接;
  3. 配置VPN连接的IPSec参数;
  4. 配置VPN连接的L2TP参数;
  5. 配置网络连接参数;

后面的配置过程将按如上顺序展开。

第一步、安装NetworkManage及相关的软件包

实际上,大多数桌面发行版中已经预先安装了NetworkManage软件,不需要我们自己手动安装。而需要我们手动安装的是NetworkManager的L2TP模块。有了该模块之后,NetworkManager才能连接L2TP/IPSec服务。下面是安装L2TP模块的教程:

#!/bin/sh

################################################################################
# 安装相关依赖。
################################################################################
apt install intltool libtool network-manager-dev libnm-util-dev \
    libnm-glib-dev libnm-glib-vpn-dev libnm-gtk-dev libnm-dev \
    libnma-dev ppp-dev libdbus-glib-1-dev libsecret-1-dev \
    libgtk-3-dev libglib2.0-dev xl2tpd strongswan

################################################################################
# 下载、编译、安装network-manager-l2tp模块
################################################################################
git clone https://github.com/nm-l2tp/network-manager-l2tp.git
cd network-manager-l2tp
autoreconf -fi
intltoolize

./configure \
    --disable-static --prefix=/usr \
    --sysconfdir=/etc --libdir=/usr/lib/x86_64-linux-gnu \
    --libexecdir=/usr/lib/NetworkManager \
    --localstatedir=/var \
    --with-pppd-plugin-dir=/usr/lib/pppd/2.4.7

make && make install

################################################################################
# 移除AppArmor中关于IPSec的设置
################################################################################
apparmor_parser -R /etc/apparmor.d/usr.lib.ipsec.charon
apparmor_parser -R /etc/apparmor.d/usr.lib.ipsec.stroke

################################################################################
# 重新编译x2ltpd服务
################################################################################
apt remove xl2tpd
apt install libpcap0.8-dev

wget https://github.com/xelerance/xl2tpd/archive/v1.3.6/xl2tpd-1.3.6.tar.gz
tar xvzf xl2tpd-1.3.6.tar.gz
cd xl2tpd-1.3.6
make
make install

################################################################################
# 重启电脑
################################################################################
reboot

第二步、创建VPN连接

这一步是新建VPN连接。使用NetworkManager创建VPN连接的入口有很多,这里只列出了一种。

通过「System Settings」-「Network」-「+」中,先选择要使用的网络接口。如图所示:

因为使用VPN会创建虚拟接口,所以选择「VPN」即可,点击「Create…」按钮。然后会弹出一个名为“Network Connections”的窗口,点击「Add」来选择连接类型,如图所示:

选择「Layer 2 Tunneling Protocol(L2TP)」,点击「Create…」按钮弹出如图窗口,填写VPN连接的基本信息:

按照提示填写信息即可。

第三步、配置VPN连接的IPSec参数

点击上图中的「IPSec Settings…」。在弹出的界面中,将共享密钥填写入「Pre-shared key」中,并在「Phase 1 Algorithms」中填写阶段一的算法。点击「OK」按钮。在「Phase 1 Algorithms」中填写的值需要使用 ike-scan 来确定:

按照提示填写信息即可。

第四步、配置VPN连接的L2TP参数

点击「PPP Settings」,进行PPP选项设置,如下图:

按照提示填写信息即可。

第五步、配置网络连接参数

至此,VPN设置已经完成。接下来编辑网络连接,连接时自动连接VPN就可以了:

按照提示填写信息即可。

至此,配置已经完成,在系统托盘中选择你命名的VPN配置即可。

常见问题处理

received NO_PROPOSAL_CHOSEN error notify

使用ike-scan确定阶段1的算法:ike-scan --verbose vpn.office.com

详细参考「VPN Client not connecting」一文中的讨论

EAP: peer reports authentication failure

首先,进入「<PPP Settings>

In Authentication section,

  • CANCEL <PAP>, <CHAP>, and <EAP>,
  • KEEP ONLY <MSCHAP> and <MSCHAPv2> SELECTED.

In Security and Compression section,

  • SELECT <Use Point-to-Point encryption (MPPE)>, and choose Security to be <All Available (Default)>

最后,保存关闭:OK -> Accept -> Close

详细参考「HOWTO: PPTP: Ubuntu Client connect to Windows VPN Server」一文

Could not determine local IP address

GitHub/network-manager-l2tp

原因分析:在 network-manager-l2tp 中,没有关于LAC的更精细化的设置(可能是我们没有发现)。带来的问题之一就是:如果服务端“拒绝分配IP地址(assign ip = no)”,那么network-manager-l2tp就会连接失败。

Could not find source connection

arch linux – NetworkManager fails with “Could not find source connection”

问题描述:Ubuntu 20.04 LTS,桌面环境,创建 L2TP 连接,当启动时,按钮状态变化,但是没有创建连接,而 journalctl -f -u NetworkManager.service 提示 Could not find source connection 错误。

解决方案:参考 NetworkManager fails with “Could not find source connection” 讨论,通过 nm-applet 命令,然后在系统托盘中开启,此时能够成功建立 VPN 连接,所以我们猜测是系统的 BUG 导致。

# 05/16/2022 经过我们的尝试,发现是因为没有配置默认网关,而导致该问题,添加网关后便能解决。

参考文献

Arch Wiki/Openswan L2TP/IPsec VPN client setup
Gentoo Forums/VPN Client not connecting
GitHub/Configure IPsec/L2TP VPN Clients
Enabling L2TP over IPSec on Ubuntu 16.04
VPN Client not connecting
HOWTO: PPTP: Ubuntu Client connect to Windows VPN Server