Why should I use "service sshd reload" in the preference "service sshd restart"?

From my tests on Linux, it seems

service sshd reload

  • Only works when sshd already running
  • Stops sshd if problems occur in the sshd_config file.
  • Returns error code 0, even if the sshd_config file has problems

service sshd restart

  • It works whether sshd is running
  • Stops sshd if the sshd_config file has invalid syntax or other problems.
  • Returns a non-zero error code if problems occur in the sshd_config file.

I understand that they perform different operations, but it seems to me that I should always use service sshd restart . Are there any reasons why service sshd reload preferred in some situations?

+8
linux ssh operating-system sshd
source share
4 answers

When you run the service sshd command, where the option can be reloaded / restarted, it actually starts the program with a modified environment:

  env -i PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" ${OPTIONS} 

eg:.

  env -i PATH=/sbin:/usr/sbin:/bin:/usr/bin TERM=xterm /etc/init.d/sshd reload 

The sshd command does almost the same thing in both cases (reboot / reboot):

reload: trying to kill the process sending the HUP signal, and, as you can see in the snap, this requires the PID of the process. (It works whether sshd is running)

  reload() { echo -n $"Reloading $prog: " if [ -n "`pidfileofproc $SSHD`" ] ; then killproc $SSHD -HUP else failure $"Reloading $prog" fi RETVAL=$? echo } 

restart: it will do the same as if you did stop-> start.

  restart() { stop start } start() { [ -x $SSHD ] || exit 5 [ -f /etc/ssh/sshd_config ] || exit 6 # Create keys if necessary if [ "x${AUTOCREATE_SERVER_KEYS}" != xNO ]; then do_rsa1_keygen do_rsa_keygen do_dsa_keygen fi echo -n $"Starting $prog: " $SSHD $OPTIONS && success || failure RETVAL=$? [ $RETVAL -eq 0 ] && touch $lockfile echo return $RETVAL } stop() { echo -n $"Stopping $prog: " if [ -n "`pidfileofproc $SSHD`" ] ; then killproc $SSHD else failure $"Stopping $prog" fi RETVAL=$? # if we are in halt or reboot runlevel kill all running sessions # so the TCP connections are closed cleanly if [ "x$runlevel" = x0 -o "x$runlevel" = x6 ] ; then trap '' TERM killall $prog 2>/dev/null trap TERM fi [ $RETVAL -eq 0 ] && rm -f $lockfile echo } 
+3
source share

Some applications, including several web servers, support reloading their configuration without rebooting at all. In this case, reload will be the best way to let them know.

As a use case, it would be great if sshd really supported reloading the configuration without affecting existing connections. This will allow you to check the new configuration without losing the current ssh connection (for example, when changing permissions so that you can log in).

Additional Information: List of all systemd device actions

+1
source share

Just mention: as in the examples above, people use sshd, that it is a daemon, an ssh service. The correct line should be:

 service ssh reload 
0
source share

I think that this "reboot" can be used in the shell script for several services to restore to its original state, in this case we did not know if the service was working or not, so we just allow all these services to "reboot".

If in this case we use "restart", some of those services that we did not use will begin.

Usually, for debugging (or modification) for a particular service, we want this service, for example, "sshd", to start, the "reboot" should be better, since we do not need to check whether this service successfully works.

-one
source share

All Articles