14

On server A, I want to issue the following commands to Server B through ssh.

service network stop
sleep 5
service network start

The problem is because I issued a network 'stop', then my current ssh connection is lost as well. Therefore I cannot execute the succeeding commands (sleep 5 and service network start). Note that I cannot use (service network restart).

Does anyone have a workaround / solution for this?

Carmen
  • 807
  • 3
  • 11
  • 13
  • 6
    why wouldn't you just use the "restart" option? why separately stop, and then start? – SpacemanSpiff Jun 10 '11 at 03:39
  • This is the only solution that works with modern Debian and Ubuntu versions: http://serverfault.com/a/731120/10361 the service approach became "faulty" in both distros in the last years. – sorin Oct 23 '15 at 08:42

9 Answers9

9

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?

Corey S.
  • 2,379
  • 1
  • 18
  • 23
  • 1
    I always operate through `screen` when working on remote machines. In case of unplanned disconnections it can be a life-saver. Also having several active shells in the same session can be handy. You might also like to look at `tmux` too, I've not used it myself but some prefer it to `screen` and its core function provides the same benefits. – David Spillett Jun 11 '11 at 14:51
  • Outdated: this does not work anymore: `Failed to restart network.service: Unit network.service failed to load: No such file or directory.` This works: http://serverfault.com/a/731120/10361 – sorin Oct 23 '15 at 08:36
  • @sorin Which platform? This was assuming a RedHat variant. – Corey S. Feb 08 '16 at 15:47
  • `systemctl restart networking` didn't work for me for some reason in `tmux` session on Ubuntu Xenial. `systemctl restart network` worked on CentOS 7 (OpenVZ) not even in `tmux`/`screen` session. Without losing connection. – x-yuri Apr 27 '18 at 15:22
5

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.

Seth Robertson
  • 1,119
  • 6
  • 10
3

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

service network stop && sleep 5 && service network start
Jordan S. Jones
  • 1,083
  • 9
  • 13
  • 3
    I'd prefer service network stop; sleep 5; service network start. If any of the commands fail it will execute the rest anyway. – ghm1014 Jun 09 '11 at 17:30
  • @ghm1014 Aye, that is also a very good alternate form. In fact, that is what I typically use. – Jordan S. Jones Jun 09 '11 at 18:42
  • 1
    What is the advantage over `service network restart`? – Caleb Jun 11 '11 at 14:25
  • When you do `service network stop` the interface gets down, `bash` receives `SIGHUP`, and no other commands get executed. Am I missing something? – x-yuri Apr 27 '18 at 15:23
  • 2
    This is literally the worst answer ever for exactly the reason that @x-yuri has explained above. If you are logged in over SSH this will a) not work and b) mean you can not access the server over this interface again to fix it. – jakew009 Mar 10 '21 at 16:31
2

Try this (maybe installing cron if needed):

$ at now+5min
at> service network stop
at> sleep 5
at> service network start
at> [control-D]

Then logout, wait 6 min and relogin

Caleb
  • 11,583
  • 4
  • 35
  • 49
  • 1
    The `at` scheduler is not in the cron package on most distros, try looking for an `at` package directly. – Caleb Jun 11 '11 at 14:23
2

This works with modern Debian and Ubuntu, while all the other answers will not work.

screen
sudo ifdown --exclude=lo -a && sudo ifup --exclude=lo -a

Please keep in mind that it can take a little bit to get the interface back. In my case about ~15 seconds as I do have a bond.

sorin
  • 7,668
  • 24
  • 75
  • 100
1

Why not put it into a shell script and execute that via SSH?

  • If I put it in a script, then this will happen: on Server A, ssh server_B "execute_script". But won't I still lose my ssh connection from A to B? I am hoping that I can still preserve the ssh connection... – Carmen Jun 07 '11 at 23:49
  • 2
    You can't preserve the SSH session if you're stopping the network interface on a server. – Christopher Armstrong Jun 07 '11 at 23:55
  • so, it would look like my ssh session will hang while I am running that restart script. Is there a way to fake this? Eg. break out of the current ssh session and re-connect to it? – Carmen Jun 08 '11 at 00:10
  • 1
    Yeah, screen, as noted by today's answer. – mfinni Jun 11 '11 at 13:40
  • Yeah, that won't work. The "service network restart" actually calls a shell script, but internally it just runs stop then start. The ssh session gets cut off after the stop and the start never happens. Screen or nohup is the answer. Note, I've done this through screen and it actually preserved the existing ssh session for me (just a bit of lag), but it will not without screen. DANGER WILL ROBINSON: this is a good way to loose access to the remote system entirely (if you loose the connection, no debugging, you're just hosed). – Jared Jul 31 '14 at 17:57
  • @ChristopherArmstrong I've just successfully did `systemctl restart network` on CentOS (OpenVZ) not in `tmux`/`screen` session. And the connection was not lost. – x-yuri Apr 27 '18 at 15:26
0

Generally, systemctl restart networking (Debian/Ubuntu), systemctl restart network (CentOS) should work, without a tmux/screen session.

x-yuri
  • 1,845
  • 1
  • 22
  • 27
0

It sounds like you want either screen or tmux. These will allow you to preserve your session through the loss of a network connection. They're really quite useful, almost all of my terminal sessions are through screen.

Jeff Tang
  • 141
  • 2
0
#!/bin/sh

# CentOS Linux release 7.4.1708 (Core) 

# 1. restart the network service
# 2. take the NIC [ens32] down
# 3. bring the NIC [ens32] up

systemctl restart network \
  && ifdown ens32 \
  && ifup ens32
#!/bin/sh

# Linux far-seer-01 4.9.0-8-amd64 #1 SMP Debian 4.9.144-3.1 (2019-02-19) x86_64 GNU/Linux

/etc/init.d/networking restart \
    && ifdown eth0 \
    && ifup eth0

e.g.

[root@localhost tmp]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:b3:74:da brd ff:ff:ff:ff:ff:ff
    inet 192.168.224.129/24 brd 192.168.224.255 scope global dynamic ens32
       valid_lft 1796sec preferred_lft 1796sec
    inet6 fe80::f06e:8b57:23fc:b25/64 scope link 
       valid_lft forever preferred_lft forever
[root@localhost tmp]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 
[root@localhost tmp]# cat net.sh 
#!/bin/sh

# 1. restart the network service
# 2. take the NIC [ens32] down
# 3. bring the NIC [ens32] up

systemctl restart network \
  && ifdown ens32 \
  && ifup ens32
[root@localhost tmp]# sh net.sh 
Device 'ens32' successfully disconnected.
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/17)
$ cat /etc/os-release 
PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
NAME="Debian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
$ cat net.sh 
#!/bin/sh

# Linux far-seer-01 4.9.0-8-amd64 #1 SMP Debian 4.9.144-3.1 (2019-02-19) x86_64 GNU/Linux

/etc/init.d/networking restart \
    &&ifdown eth0 \
    && ifup eth0
$ sh net.sh 
[ ok ] Restarting networking (via systemctl): networking.service.
ifdown: interface eth0 not configured
Internet Systems Consortium DHCP Client 4.3.5
Copyright 2004-2016 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/eth0/00:0c:29:8a:67:72
Sending on   LPF/eth0/00:0c:29:8a:67:72
Sending on   Socket/fallback
DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 5
DHCPREQUEST of 192.168.224.128 on eth0 to 255.255.255.255 port 67
DHCPOFFER of 192.168.224.128 from 192.168.224.254
DHCPACK of 192.168.224.128 from 192.168.224.254
bound to 192.168.224.128 -- renewal in 847 seconds.
ROY
  • 171
  • 1
  • 4