4

I want to simulate unplugging a cable from a Linux instance. My initial response was ip link set eth0 down. However, this command has impact on higher-level networking functions, e.g. routes (including default route) are removed, etc. When I "plug it back in" with ip link set eth0 up, the routes now are missing.

How can I simulate unplugged network without using down, or conversely tell iproute2 to just cause the network cable to be disconnected?

  • Depends what you want to achieve, exactly. One way might be to filter all packets in and out with iptables. Another way might be to disable autonegotiation and configure incompable/mismatched speed and duplex settings on each side of the cable. The latter used to work really well with 10/100 ethernet, I don't know how easy or hard it has become to purposefully break ethernet settings in the era of gige and 10gige... – Celada Feb 28 '16 at 12:33
  • Actually, it all is virtual, so playing with auto-negotiation and speeds won't work here. But iptables is a nice idea. I might be able to do something here. – deitch Feb 28 '16 at 12:42
  • @Celada iptables was a brilliant idea, and far simpler than anything I had in mind. In my case, I was using containers, so I had to ip netns exec <pid> iptables ... but it was excellent. Please propose it as an answer and I will choose it. – deitch Feb 28 '16 at 15:24
  • Do you want to simulate unplugging the cable in a way that notifies the OS and applications that the network is down? Or do you want to just temporarily drop all traffic? – Gilles 'SO- stop being evil' Feb 28 '16 at 19:46
  • @Giles, I would have preferred the first, which is similar to an actual cable being pulled. However, I could not find any way to do that, without also removing routes and higher level network config, which definitely does not happen when a cable is pulled. – deitch Feb 29 '16 at 4:25
-1

Try this command:

nmcli device disconnect eth0

To turn interface on, use this command.

nmcli device connect eth0

Note: Please change eth0 to your interface device.

  • Isn't that basically the same as ip li set down? – deitch Jan 4 '18 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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