从 WSL2 使用 Ubuntu 启用 ufw 失败

zun*_*yen 4 ufw ubuntu wsl2

这是 Ubuntu 20.4 或 Ubuntu 22.04 的全新安装,但当我尝试从 Ubuntu 控制台启用 ufw 时,它们都失败了,并出现如下错误。

 sudo ufw enable
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/ufw/util.py", line 427, in under_ssh
    ppid = get_ppid(pid)
  File "/usr/lib/python3/dist-packages/ufw/util.py", line 419, in get_ppid
    ppid = open(name).readlines()[0].split(')')[1].split()[1]
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/sbin/ufw", line 138, in <module>
    not ui.continue_under_ssh():
  File "/usr/lib/python3/dist-packages/ufw/frontend.py", line 901, in continue_under_ssh
    if self.backend.do_checks and ufw.util.under_ssh(): # pragma: no cover
  File "/usr/lib/python3/dist-packages/ufw/util.py", line 457, in under_ssh
    return under_ssh(ppid)
  File "/usr/lib/python3/dist-packages/ufw/util.py", line 457, in under_ssh
    return under_ssh(ppid)
Run Code Online (Sandbox Code Playgroud)

我的 WSL2 有以下内容...

 wsl -l -v
  NAME            STATE           VERSION
* Ubuntu-22.04    Running         2
  Ubuntu-20.04    Stopped         2
Run Code Online (Sandbox Code Playgroud)

我在另一台电脑上有类似的配置,但没有这个问题。我已经关闭、更新了 WSL、重新安装了不同版本的 Ubuntu,但得到了相同的结果。我四处搜寻但找不到任何匹配项。希望有ufw经验的专家可以帮助我。谢谢,

Alm*_*non 6

正如@NotTheDr01ds 所说,这是一个错误。我在这里提交了错误报告。

更新:0.36.2版本已修复。但是,0.36.2 可能尚不适用于您的 Ubuntu 版本。您可以通过运行来检查可用的版本sudo apt update;apt policy ufw


您可以通过转到 util.py 中的 get_ppid 函数并将第一个分割替换为 来解决此问题,rsplit(')', 1)以便它根据最后一个括号而不是所有括号进行分割。

前: ppid = open(name).readlines()[0].split(')')[1].split()[1]

后: ppid = open(name).readlines()[0].rsplit(')',1)[1].split()[1]

  • 代码更改成功了!谢谢!! (2认同)