20

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.

nopcorn
  • 9,559
SafeY
  • 333

3 Answers3

19

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
Chris Down
  • 125,559
  • 25
  • 270
  • 266
  • thank you ,I'm reading about it here – SafeY Apr 06 '13 at 08:36
  • 1
    that's working successfully but ip link set eth0 up is not working :( and I need them both – SafeY Apr 06 '13 at 08:46
  • @NancySmith "Is not working" is not meaningful. What error messages occur? Did you assign an IP and netmask? – Chris Down Apr 06 '13 at 08: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 06 '13 at 09:05
  • That's my command : exec('ssh -p 22 root@192.168.0.101 "ip link set dev eth0 up"'); – SafeY Apr 06 '13 at 09:07
  • @NancySmith Er... if the network is down, how are you expecting to SSH back into it? – Chris Down Apr 06 '13 at 09:19
  • Do you have an alternative solution ? I need to switch server on and off . – SafeY Apr 06 '13 at 09:22
  • Use another network interface that will still emulate your conditions for downtime. – Chris Down Apr 06 '13 at 09:44
  • can you help me ? I found this still ifconfig is not doing what i want – SafeY Apr 06 '13 at 10:19
  • 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 06 '13 at 10:22
  • thanks , I will do a search , have no idea about what you are talking about . – SafeY Apr 06 '13 at 10:47
  • On ubuntu, I have to run dhclient after ip link set eth0 up to get internet access back. – ilia choly Jun 06 '17 at 18:35
11

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
Mocking
  • 317
  • 1
    good 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
  • 2
    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
  • 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
1

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.

MelBurslan
  • 6,966