12

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.

15

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
  • thank you ,I'm reading about it here – SafeY Apr 6 '13 at 8:36
  • 1
    that's working successfully but ip link set eth0 up is not working :( and I need them both – SafeY Apr 6 '13 at 8:46
  • @NancySmith "Is not working" is not meaningful. What error messages occur? Did you assign an IP and netmask? – Chris Down Apr 6 '13 at 8:59
  • I mean that I used ip link set eth0 down via php and ping shows that my server is unreachable . then I wanted to get it back "up" , I executed ip link set eth0 up via php and server still down . – SafeY Apr 6 '13 at 9:05
  • 3
    @NancySmith Create another network interface that does not have access to whatever it is connecting to (using routes, etc). SSH to the host using that interface. Bring down the interface that you are not connecting to SSH through. – Chris Down Apr 6 '13 at 10:22
5

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
  • 1
    good stuff. Also, relevant link for those who aren't sure about which interface to down: unix.stackexchange.com/questions/125400/… – Jeutnarg Nov 6 '17 at 17:20
  • erm... 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
  • @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
  • @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 1 '18 at 11:20
0

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.

  • I want to do this from a web page . surly I know how to power off machines . – SafeY Apr 6 '13 at 8:22
  • and I don't want to fake it . not really shut down and boot it again . – SafeY Apr 6 '13 at 8:23
  • 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 6 '13 at 8:28
  • I will edit my question to make it more clear , thanks – SafeY Apr 6 '13 at 8:33

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.