Restarting Network through SSH

Solution 1:

If you are doing this interactively, why not start a screen session? It would look something like this:

screen

(scren shell starts)

service network restart

(SSH session disconnects, but the network restart continues in the screen session)

(Wait a few seconds)

(SSH back into the host once the restart finishes)

screen -r

(Reconnect to screen and check for errors)

IMHO, it's always scary to restart a network interface remotely. What happens when it doesn't come back up? Do you have a console or other means into the host if something bad happens?

Solution 2:

The exact commands available to do this vary based on Linux distribution. On option which is pretty standard is to schedule and "at" job for 5 seconds in the future to restart networking. Another one is to use the nohup command.

echo "sleep 5; /etc/init.d/networking start" | at now
nohup sh -c 'sleep 5; /etc/init.d/networking start' &

Other distributions have the daemon command to turn the resulting program into a daemon that is no longer associated with the shell.

Solution 3:

A very simple way to do this is by using the and operator:

service network stop && sleep 5 && service network start