19

In RHEL, instead of using service network restart command, how can i restart a particular network interface, lets say "eth1", with only one command.

"Only one command" because that is the only interface where my ssh is working on also. So if i'm about to use: ifdown and then ifup, i will never be able to hit the ifup command as my ssh has been terminated once after ifdown eth1 command.

So there should be a single command which allows me to altogether bring down and then bring up the interface which is serving my current ssh connection. So i do not need to worry about connection totally lost to my server.

Any idea please?

夏期劇場
  • 455
  • 2
  • 5
  • 18

4 Answers4

30

You can use:

ifdown eth1 && ifup eth1

As a single command. The && just runs one command, then the other if the first command succeeds. If you are required to use sudo make sure you use it before each command:

sudo ifdown eth1 && sudo ifup eth1

As long as your interface is configured to have the neccessary IP and route to match the current configuration, your ssh connection won't drop.

If you're worried about using it on a production server that you don't have another method of access to, that's understandable. Though the command does exactly what you want, it's very easy to have a configuration error that is only noticed after running this command. If you don't have an alternate method of access (for example, out-of-band console, or SSHD running on another interface), it's safest not to do this.

I use this technique often to perform a 'restart' of the interface, but I generally have a backup method of access available just in case when I do it.

blahnana
  • 666
  • 5
  • 5
1

You can 'restart' one interface by issuing following commands:

# ifdown eth1
# ifup eth1

After that, you can verify that your new configuration is active

# ip a
jerQ
  • 19
  • 1
  • Sorry but i need .. "Only one command" because that is the only interface where my ssh is working on also. So if i'm about to use: ifdown and then ifup, i will never be able to hit the ifup command as my ssh has been terminated once after ifdown eth1 command. So there should be a single command which i can bring down and then bring up the interface which is serving my current ssh connection. So i do not need to worry about connection lost to my server. – 夏期劇場 Mar 20 '13 at 06:18
  • You could make them a shell script and run that, your ssh-session should not get terminated, even though there will be short network blackout. Other option is to run that script inside of screen-session, where it will be executed to the end, even if your ssh-session would fail. – jerQ Mar 20 '13 at 06:19
  • 1
    will this help? `ifdown eth1 ; ifup eth1` ? – 夏期劇場 Mar 20 '13 at 06:21
0

Are you trying to change the IP address (or any interface parameter for that matter) of an interface on a remote machine? If the answer is yes, then you can try one of the following:

A cool method is by using the screen utility to create a session by just typing 'screen'. Once you restart the network service and your SSH connection drops, use screen -ls to check that the session is still active (but detached) and screen -r to resume the detached session. This will work in case there is no error in your interface configuration file, i.e. the network service is restarted successfully.

You can also use the following trick:

$ cp -v current_conf_file current_conf_file.bak
$ echo "mv current_conf_file.bak current_conf_file && service network restart" | at now+10min

And proceed with editing your interface configuration file and restarting the network service. In case of a mistake, you just have to wait 10 minutes, in order for the previous configuration to be reapplied and the network service to be restarted.

IG83
  • 3
  • 4
-4

all answers talking about ifup & ifdown are not according to the question. ifup & ifdown just bring the interface up and down only, it doesn't effect the network service. for linux network service doesn't mean interfaces ip setup only.

please check /etc/rc.d/network script for more details.

  • 1
    I'm not sure what point you're making. The OP doesn't ask about restarting the network *service*, but only restarting an individual *interface*. Moreover, the OP has accepted an answer that explicitly uses the commands you're recommending against. -1 from me. – MadHatter Jul 29 '16 at 07:26