6

Split question: this other one here is for Windows.

Linux: Fastest way to disable internet (keeping LAN) from command line?

I think the easiest way to deactivate internet (not LAN) in Linux is to remove the default gateway, so, assuming LAN is 10.0.2.0/24 and gateway is 10.0.2.1 :

`sudo /sbin/route del default gw 10.0.2.1`

To reactivate internet:

`sudo /sbin/route add default gw 10.0.2.1`

But, even when this a simple line, it requires to discover the default gateway IP first:

`sudo /sbin/route`

I am going to build some general purpose shell scripts that need to enable/disable internet(but keep LAN working), so it seems I am going to need some (¿grep, maybe?) operations to filter and detect the exact gateway IP number (it could be 10.0.2.1, 127.0.0.1, 127.0.50.1, 192.168.0.1 ... etc), unless I achieve to find a simpler command line.
Any ideas, please?

  • 2
    Do you need to disconnect from internet (no internet access, but maintain LAN connection), or completely disconnected from the network? – Darius Feb 3 '14 at 22:28
  • Read first line: «deactivate internet (not LAN)». Anyway, I have edited the post to be clearer. – Sopalajo de Arrierez Feb 3 '14 at 22:36
7

On *nix, to find the gateway:

GW="$(sudo /sbin/route -n | awk '$1=="0.0.0.0" {print $2; exit}')"
sudo /sbin/route del default gw "$GW"
echo "$GW" >~/my_tmp_file

The last line saves the value in a file for later use when you want to restart the network:

sudo /sbin/route add default gw "$(cat ~/my_tmp_file)"

Note: If there is more than one default gateway it will require this code to run again for each, or to be rewritten in order to support that.

How it works: The above awk command is able to capture the gateway because route -n output looks like:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.2.1        0.0.0.0         UG    0      0        0 eth0
10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0

The internet gateway is the one that allows destination anywhere (0.0.0.0). Thus, the awk script goes through this output line by line looking at the first column (called $1 in awk notation). When the first column is destination-anywhere, then it prints the second column which is the gateway. The output of the awk command is then captured into the shell variable GW. The shell can then be asked to substitute $GW into any command that needs it.

  • the author confused q's sed command on awk, he probably wanted to use exit command instead, if done so GW variable will be guaranteed to have only one line, and then the command route del will work. However this way will not work if there is more than one gateway, in order to support that the route del will have to be called for each line, in other words the code requires to be rewritten. – Tiago Pimenta Oct 22 '18 at 18:58

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.