15

I would like to know which is the right method to restart networking service in Debian Jessie. I know that I can use:

service networking restart

or

/etc/init.d/networking restart

that anyway gave me problems on ssh connection, or

invoke-rc.d networking restart

and other two methods with systemctl and with ifup/ifdown.

But which is the right way to do it?

cloud81
  • 163
  • 1
  • 2
  • 8
  • Try asking on http://unix.stackexchange.com, they have better knowledge of different Linux flavours and dialects. – Massimo Mar 24 '16 at 17:41
  • 1
    Oh, and the first three commands are basically equivalent, `service` and `invoke-rc.d` only call `/etc/init.d/servicename` with the same options they were given. – Massimo Mar 24 '16 at 17:43
  • Also, see here: http://unix.stackexchange.com/questions/136481/should-invoke-rc-d-or-service-be-used-to-restart-services. – Massimo Mar 24 '16 at 17:44

2 Answers2

13

I would use the service command because it is more consistent across different distributions. So of the commands you mentioned, the variant I would go for is:

service networking restart

And I would definitely run it inside a screen session or by other means ensure that it won't fail to complete in case you lost connection with the shell in which you typed it. (I have tried losing connectivity to a machine by logging in with ssh and then restarting the network only to have the ssh connection terminate while the network was down and send a HUP to service such that it would not bring up the network again.)

In the past there have been systems where service was a simple wrapper around the scripts in /etc/init.d and the first two of your commands would do the exact same thing. But nowadays there are systems where service will sometimes do something different, and in general service knows better what to do on your particular distribution. And invoke-rc.d is also distribution dependent.

Though service is the most similar across distributions, it is still possible for the service names to be different. For example there are distributions where the service is named network and others where it is named networking. And in some configurations it may be more appropriate to restart network-manager rather than networking.

kasperd
  • 29,894
  • 16
  • 72
  • 122
  • Thanks for the explanation, very clear! Next time I'll use screen along with the service command mentioned prior. – cloud81 Apr 03 '16 at 17:34
  • 3
    isnt it deprecated to use service on systemd systems at all? – greenone83 Oct 29 '16 at 11:56
  • @greenone83 There is no indication in the man page about the command being deprecated. On all systems where I have seen it, it has been a convenient wrapper script around whatever init system that distribution was using. And it has been updated as init has been redesigned. Which means you can use the same `service` command now as you could 10 years ago, and it will do the right thing even though the underlying init scripts behave totally different. – kasperd Oct 29 '16 at 12:24
  • @kasperd this pretty much explains my point... http://unix.stackexchange.com/questions/240528/apache-and-systemd#240603 you can do it... but then you might disturb systemd – greenone83 Oct 29 '16 at 20:02
  • @greenone83 That posting does not mention the `service` command **at all**. I do not know of any distribution where the `service` command does not work properly. – kasperd Oct 29 '16 at 21:53
  • @kasperd the point is that if you bypass systemd by using the service command (which invokes init.d-scripts) systemd can get into undefined/bad states where it does not recognize that a service is in fact running – greenone83 Oct 30 '16 at 08:54
  • @greenone83 None of the distributions I have been using has suffered from such a bug. They have all had a `service` command which would do the correct thing for that distribution. Do you know of any distribution where it doesn't work? – kasperd Oct 30 '16 at 18:01
  • 1
    I swapped out my LAN cables, which killed the network connection. systemctl restart networking did nothing, but service networking restart worked a charm. I'm running Debian Jessie. – aSystemOverload Oct 21 '17 at 10:17
  • Although `service` is still available, it's basically a [wrapper around `systemctl`](https://salsa.debian.org/debian/init-system-helpers/-/blob/debian/1.62/script/service#L159-214). | About systems where it doesn't work, that's e.g. Arch Linux, because it's not available there, but well, that's probably out of scope here. | What `screen` has to do with this? It can't prevent losing connection, can't it? What it can do is keep other programs you were running running. | I just did `systemctl restart networking`. The connection wasn't lost. – x-yuri Feb 19 '22 at 19:54
  • @x-yuri and @kasperd, perhaps you may be in a position to enlighten me further as your answers have brought light into something I was checking on. @kasperd stated: *And in some configurations it may be more appropriate to restart network-manager rather than networking.* Do you have specific instances where one may work as opposed to the other? Furthermore, is there an appropriate time to invoke `nmcli` as opposed to `systemd` service? – Avocado_man Jun 23 '22 at 04:58
  • @Avocado_man The command depends on the OS and other factors. If you want to... reconfigure (?) the network on a remote server over SSH, for Debian-like systems that would be `systemctl restart networking`, for CentOS-like `systemctl restart network`. If you want to be extra careful, you can run the command in a `tmux` or `screen` session. I can imagine that in the past it was needed to not end up with a server you can't connect to, but these days... It worked w/o `tmux`/`screen` for me so far... – x-yuri Jun 23 '22 at 13:41
  • ...`systemctl restart NetworkManager`? Well, that would be the command if it's NetworkManager that manages the network on your server. Never seen that in RL. I'd expect to see NetworkManager on a desktop machine. At least that's one of the options. The same goes for `nmcli`, because that's part of NetworkManager. If in doubt, run `systemctl` and look for `network`, `networking`, `NetworkManager` services (not targets). Generally you have only one of those. And that's the one you've got to restart. – x-yuri Jun 23 '22 at 13:42
  • Thank you @x-yuri for the detailed explainer. It makes more sense. – Avocado_man Jun 27 '22 at 05:58
0

I just restarted networking on Debian 11 (Bullseye):

# systemctl restart networking

The connection wasn't lost. I believe it also worked for me on older releases.

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