How to "shut off" all networking on Linux from bash?
Solution 1
Most linux flavors come with scripts to do this already. They're part of the startup/shutdown process.
In Fedora/RHEL flavors, the scripts are usually accessed via /etc/init.d/
. So, you could execute "/etc/init.d/network stop
" to stop the network. To start, substitute "start" for "stop."
From the command line, the preference for these flavors is to use the service
command: "service network stop
" or "service network start
."
These commands are limited to the root user. I would recommend against allowing regular users to do something like this.
Solution 2
As you mentioned you can use iptables for this
These rules will drop all incoming and outgoing packets
iptables -A INPUT -j DROP
iptables -A OUTPUT -j DROP
To check the configured rules
iptables -L
To delete these rules
iptables -D INPUT -j DROP
iptables -D OUTPUT -j DROP
Related videos on Youtube
pnongrata
Updated on September 18, 2022Comments
-
pnongrata over 1 year
I'd like to write a bash/shell script that "turns off" the machine's ability to communicate over it's network card, effectively shutting down all networking. From the command line:
# To turn all networking off sh networking.sh OFF # Then to turn it back on sh networking.sh ON
The only way I can think of doing this would be at the port-level via
IPTABLES
, but having never experimented with that before, I'm not sure if I'm even heading down the right avenue or if I'm way off base. Thanks in advance.-
ganesh over 11 yearsIf you have aq simply setup then this would bring all ethernet devices down.
for n in ifconfig -a | grep eth | cut -d ' ' -f 1
; do bringing down echo $n ; ifconfig $a down ; done (ifconfig -a showing all devices. Grep filters down to lines containing eth. Cut first field (separated by spaces). And for to loop through all the answers. Kludgy, but works. -
pnongrata over 11 yearsThanks @Hennes (+1) - can you explain what "aq" is? Also this would work for ethernet devices, but what if the machine has a Wifi adaptor? Is there a solution that works for both wired/wireless?!? I would imagine that both use ports, which is why I had started looking into
IPTABLES
. Thanks again! -
ganesh over 11 yearsOn GNU/Linux all Ethernet devices seem to be named to ethX (with X starting at 0 for the first, 1 for the second device, 3 for the third, ...). I think that this includes wireless. However note that this is only to bring things down via ifconfig. Ifconfig is used on almost all unices but Linux seems to be moving away from it to a 'new' tool called 'ip'. Also not that while it will work for bringing the network DOWN you want a better answer to bring things up again.
-
ganesh over 11 yearss/aq/a (I need to explain that it was a typo so my text is 15 char or longer)
-
pnongrata over 11 yearsThanks again @Hennes - how about trickle? It looks like I could just run
trickled
(trickle daemon) with up/download bandwidths set to0
. My only question is: how do I restore the up/download bandwidths when I want to bring the network back "up"?!? -
laurent over 11 yearsAll ethernet device on linux are not named
ethx
. Wifi connections arewlanx
for example, dsl (or pppoe) arepppx
, bridged nics arebrx
and so on. Anyways, there are command specific to manage networking that are more practical ;)
-
-
laurent over 11 yearstrickle will control the bandwidth, not stop the network (won't pass anything if 0 but network will be running). The answer here shutdown networking. Obs: in some distro (Ubuntu for example) it is
networking
instead ofnetwork
like/etc/init.d/networking stop
orservice networking start
-
laurent over 11 yearsNot the easiest method as the network managing commands are better and will actually bring the network down (not only block everything). Anyways, for this to work always, you need to replace
-A
with-I
in the "stop network" rules to be sure the new rules are placed before any other rules accepting connections in the INPUT table (and not appended at the end of the table with-A
). With-A
it will basically work only with previously empty iptables.