I have simulated network latency with netem and It's great. Now I want to simulate unplugged network cable or when server goes down. I need this to make testing of my application easier and I couldn't find anything on the web that would help me. My servers are virtual CentOS instances and they are on Virtualbox. I want to do this from a php web page.
3 Answers
Just bring the interface down. For example, with eth0:
ip link set eth0 down
To bring the interface back up:
ip link set eth0 up

- 125,559
- 25
- 270
- 266
When using the top voted answer on a machine you are connecting via SSH, you will bring down the network and have no means of bringing it back up. The following is a way to bring it down and bring it back up while on a SSH connection.
Example using interface ens32
:
If you run:
ip link set ens32 down
And you are connecting through SSH, you will be unable to bring it up again since you have disabled your network and therefore cannot SSH to your server.
I use the following script to simulate 20 seconds of downtime:
#!/bin/sh
ip link set ens32 down
sleep 20
ip link set ens32 up
Save it as downtime.sh, make sure the file has execute rights, and then run:
sudo ./downtime.sh

- 317
-
1good stuff. Also, relevant link for those who aren't sure about which interface to down: https://unix.stackexchange.com/questions/125400/how-can-i-find-available-network-interfaces – Jeutnarg Nov 06 '17 at 17:20
-
2erm... I am a linux newbie but I think there may be some bad advice here: if you don't nohup this script, what happens is that you execute it, you session dies (due to disconnection) and the script stops (and never revives the net interface). I think some reading around nohup should be valuable! – DraxDomax Oct 31 '18 at 15:27
-
1@DraxDomax what you say sounds logical but I haven't had any problems with it not coming back up. Whereas I've had issues with the accepted answer. – Mocking Oct 31 '18 at 21:15
-
1@Mocking Sorry, I couldn't edit my comment. It's not necessarily "bad advice". It's actually a nice pro-active answer because 90% of people who will find this question are trying to do the same thing: "I need to test if my software works after machine disconnection. I don't have access to the virtualisation host". Actually, I resorted to your script (just my adapter was br0) - it didn't work in my case (SSH from MacOS to CentOS) and I had to call our IT guy to bring the net up again. – DraxDomax Nov 01 '18 at 11:20
On your virtualbox console window, you should have a Machine menu pull down. Under that one there are several actions. One of which is Power off. You need to select that one for an immediate power down. This is the closest thing to yanking the power cord of a physical server.

- 6,966
-
I want to do this from a web page . surly I know how to power off machines . – SafeY Apr 06 '13 at 08:22
-
-
Judging by the mention of
netem
, I assume the question is really about unplugging the network cable from a server, and not the power cable (although it is not clear). – Chris Down Apr 06 '13 at 08:28 -
ip link set eth0 up
is not working :( and I need them both – SafeY Apr 06 '13 at 08:46ip link set eth0 down
via php and ping shows that my server is unreachable . then I wanted to get it back "up" , I executedip link set eth0 up
via php and server still down . – SafeY Apr 06 '13 at 09:05exec('ssh -p 22 root@192.168.0.101 "ip link set dev eth0 up"');
– SafeY Apr 06 '13 at 09:07ifconfig
is not doing what i want – SafeY Apr 06 '13 at 10:19dhclient
afterip link set eth0 up
to get internet access back. – ilia choly Jun 06 '17 at 18:35