如何从终端禁用我的互联网连接?

pre*_*ise 15 command-line networking

自动连接选项允许我有线有线连接1。当我从面板的Network > Disconnect菜单中断开连接时,它会起作用。但是当我用命令这样做时:

nmcli con down id "Wired connection 1"
Run Code Online (Sandbox Code Playgroud)

只要它断开连接,连接就会重新打开。

网络 > 断开连接如何工作?我们可以在nmcli不禁用自动连接的情况下做同样的事情吗?

笔记:

  1. nmcli con down id "Wired connection 1"与禁用自动连接时一样工作(但这又不是一个选项),
  2. 我不想使用sudo(在脚本中实现不太好!)。

Rad*_*anu 21

如果我想从终端禁用任何互联网连接,以下命令对我来说就像一个魅力:

nmcli nm enable false
Run Code Online (Sandbox Code Playgroud)

要再次启用它:

nmcli nm enable true
Run Code Online (Sandbox Code Playgroud)

注意:正如CPBL评论的,这不再适用于 Ubuntu 15.04 及更高版本。而是使用nmcli networking offnmcli networking on


另一种非常接近您的追求的方法是使用:

nmcli dev disconnect iface eth0
Run Code Online (Sandbox Code Playgroud)

要再次启用 eth0,您需要键入:

nmcli -p con up id "<connection name>" iface eth0
Run Code Online (Sandbox Code Playgroud)

名为“有线连接 1”的连接示例:

nmcli -p con up id "Wired connection 1" iface eth0
Run Code Online (Sandbox Code Playgroud)

更改eth0为您的有线接口名称。这将防止在没有用户/手动干预的情况下进行任何进一步连接,如下所示man nmci

disconnect iface <iface> [--nowait] [--timeout <timeout>]
           Disconnect a device and prevent the device from automatically
           activating further connections without user/manual intervention.

           Available options are:
                --nowait     – exit immediately without waiting for
                command completion

                --timeout    – how long to wait for command completion
                (default is 10 s)
Run Code Online (Sandbox Code Playgroud)

请阅读man nmcli以获取更多信息。