root 的 SSH 隧道错误:sys_tun_open:配置隧道失败(模式 1):不允许操作

root 的 SSH 隧道错误:sys_tun_open:配置隧道失败(模式 1):不允许操作

我尝试设置 ssh vpn(Debian OS 服务器和客户端)。

我更改了 /etc/ssh/sshd_config (服务器):

PermitTunnel yes
PermitRootLogin without-password

但是当我尝试连接(客户端)时:

$ ssh -NTCf -vvvw  0:0 root@server-ip

...
debug1: Authentication succeeded (publickey).
Authenticated to <server-ip> ([<server-ip>]:22).
debug1: Requesting tun unit 0 in mode 1
debug1: sys_tun_open: failed to configure tunnel (mode 1): Operation not permitted
Tunnel device open failed.
Could not request tunnel forwarding.
...

有什么问题吗?请帮忙!

更新

我尝试以 root 身份运行 ssh,但仍然有错误(客户端):

# ssh -i ~user/.ssh/id_rsa -NTCf -vvvw any root@server-ip
....    
debug1: Requesting tun unit 2147483647 in mode 1
debug1: sys_tun_open: tunnel mode 1 fd 5
Tunnel device open failed.
Could not request tunnel forwarding.
debug2: fd 3 setting TCP_NODELAY
...

客户端配置:

OpenSSH_7.7p1 Debian-2, OpenSSL 1.0.2o  27 Mar 2018
Linux 4.16.0-2-amd64 #1 SMP Debian 4.16.12-1 (2018-05-27) x86_64
Debian GNU/Linux testing (buster)

答案1

创建网络接口需要 root(或至少CAP_NET_ADMIN)权限。错误消息告诉客户端 ssh 命令无法创建 tun0 接口,正如运行客户端 ssh 时的 strace 摘录所证实的那样:

21510 open("/dev/net/tun", O_RDWR)      = 4
21510 ioctl(4, TUNSETIFF, 0x7fff5f9f1530) = -1 EPERM (Operation not permitted)
21510 close(4)                          = 0
21510 write(2, "Tunnel device open failed.\r\n", 28) = 28
21510 write(2, "Could not request tunnel forward"..., 38) = 38

最简单的是以 root 身份运行它,例如(配置和)使用sudosu -c并可能提供正确的 ssh 密钥-i(或者它可以选择 root 的 ssh 密钥)。在 Debian 客户端上,这有效:

su -c 'ssh -i ~myuser/.ssh/id_rsa -NTCf -w 0:0 root@server-ip'

您应该在测试时替换-w 0:0-w any以避免冲突。

另一种方法仍然需要 root(或CAP_NET_ADMIN),即按照通用命名约定提前创建接口(tunX,其中 X 是稍后在 ssh 参数中重用的数字),但授予 ssh 用户对此接口的访问权限:

# ip tuntap add name tun0 mode tun user myuser
# ip address add 192.0.2.10/24 dev tun0
# ip link set dev tun0 up

如果远程 ssh 用户不是 root,则可以在远程服务器上(通过 root)执行相同的操作。

然后,ssh 命令(以 myuser 身份运行,例如:)ssh -NTCf -w 0:0 remoteuser@server-ip将能够通过使用具有匹配名称的预先存在的隧道接口来对流量进行隧道传输。

如果您在客户端没有任何权限,也不允许运行具有服务器 IP 网络访问权限的虚拟机或容器,那么您可能无法成功。

注意:在本问答时,OP 发现,在 Debian buster(此时测试)中,包 openssh-client 和 openssh-server 版本 1:7.7p1-2 有一个阻止使用 tun/tap 隧道的错误。这在 Debian 中修复自 (Debian) 版本 1:7.7p1-3 起可用。

相关内容