How can I disable my internet connection from terminal?

Solution 1:

The following command works for me like a charm if I want to disable any internet connection from terminal:

nmcli nm enable false

To enable it again:

nmcli nm enable true

NOTE: As commented by CPBL, this no longer works in Ubuntu 15.04 and later. Instead use nmcli networking off and nmcli networking on.


Another way very close to your quest is to use:

nmcli dev disconnect iface eth0

To enable eth0 again you need to type:

nmcli -p con up id "<connection name>" iface eth0

Example for connection named "Wired connection 1":

nmcli -p con up id "Wired connection 1" iface eth0

Change eth0 to your wired interface name. This will prevent any further connections without user/manual intervention as man nmci says:

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)

Please read man nmcli for more info.