Why would I use "service sshd reload" in preference to "service sshd restart"?
Asked Answered
E

4

9

From my tests on Linux, it seems like

service sshd reload

  • Only works when sshd is already running
  • Stops sshd if the sshd_config file has problems
  • Returns error code 0 even if the sshd_config file has problems

service sshd restart

  • Works regardless of whether sshd is already running
  • Stops sshd if the sshd_config file has invalid syntax or other problems
  • Returns non-zero error code if the sshd_config file has problems

I understand that they are performing different operations, but it seems to me a no brainer that I should always use service sshd restart. Are there any reasons why service sshd reload is preferable in some situations?

Esophagitis answered 11/7, 2013 at 10:52 Comment(0)
A
3

When you run the service sshd command where opt could be reload/restart it actually runs a program with a modified enviroment just like this:

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

e.g.:

    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 (restart/reload):

reload: Tries to kill the process sending a HUP signal, and as you can see on the snipet it needs the PID of the process to do it. (Works regardless of whether sshd is already running)

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

restart: It would just do the same as if you were to execute a 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
    }
Allene answered 30/8, 2013 at 20:16 Comment(2)
That's useful info, but why would I ever use reload? Also you said that reload works regardless of whether sshd is already running, but it can't send a signal to the sshd if sshd isn't running.Esophagitis
reload will not drop existing connectionsRam
C
0

Just to mention: as in the above examples people are used sshd, that it is the daemon, the service is ssh. The correct line should be:

service ssh reload
Changeup answered 17/12, 2014 at 12:54 Comment(1)
That's entirely dependent on the distribution you're using.Pitterpatter
X
0

Some apps, including several web servers, support reloading their configuration without restarting at all. In this case, reload would be the best way to signal them to do so.

As a use case, it would be great if sshd actually did support reloading the config without affecting existing connections. That would allow one to verify the new configuration without losing the current ssh connection (e.g. when modifying permissions, to ensure you can still log in).

Further reading: List of all systemd unit actions

Xantho answered 11/8, 2016 at 16:54 Comment(1)
But reloading _is_ supported. I just did so over an SSH connection.Rem
N
-2

I think this "reload" could be used in a shell script for multi services to recover to initial status, in this case we didn't know if a service is running or not, so we just let all these services "reload".

If we use "restart" in this case, some of those services we didn't use will start.

Usually for debugging problems(or modification) on single service, we want this service like "sshd" to start, "restart" should be better for we needn't check if this service is running successfully or not.

Nymphalid answered 2/8, 2016 at 20:6 Comment(5)
This answer does not really make sense. The question is about sshd, not random services running wild in your imagination.Astrodynamics
This question seems about "sshd", but in fact it asks about difference between "reload" and "restart", not the difference between "sshd" and "ssh", so, what's the correct answer for this question?Nymphalid
So, this is a question about a special service, but the answer could be generally used in any services.Nymphalid
Not really, because your answer makes no sense at all in the context of a single service -- especially sshd. "some of those services we didn't use?" There are none -- sshd is the only one. Shell scripts for "multi services" also makes no sense in the context of the question.Astrodynamics
The questions is asking about when we could use "reload" better than "restart". So I list a possible use in "multi services" as an example. This is my understanding of this question. So we have different understanding for this question. And to be honest, I don't understand what's your understanding of this question, my answer based on my understanding, it could means nothing to you, maybe somebody else could understand. But what's your understanding of this question, and do you have some answer?Nymphalid

© 2022 - 2024 — McMap. All rights reserved.