Linux 为什么我会优先使用“service sshd reload”而不是“service sshd restart”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17591458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:13:56  来源:igfitidea点击:

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

linuxsshoperating-systemsshd

提问by Tim Bellis

From my tests on Linux, it seems like

从我在 Linux 上的测试来看,似乎

service sshd reload

service sshd reload

  • Only works when sshdis already running
  • Stops sshdif the sshd_configfile has problems
  • Returns error code 0 even if the sshd_config file has problems
  • 仅在sshd已经运行时有效
  • sshd如果sshd_config文件有问题则停止
  • 即使 sshd_config 文件有问题,也返回错误代码 0

service sshd restart

service sshd restart

  • Works regardless of whether sshdis already running
  • Stops sshdif the sshd_configfile has invalid syntax or other problems
  • Returns non-zero error code if the sshd_configfile has problems
  • 无论是否sshd已经在运行都有效
  • sshd如果sshd_config文件有无效语法或其他问题,则停止
  • 如果sshd_config文件有问题,则返回非零错误代码

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 reloadis preferable in some situations?

我知道他们正在执行不同的操作,但在我看来,我应该始终使用service sshd restart. service sshd reload在某些情况下有什么理由更可取吗?

回答by nay743

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

当您运行可以重新加载/重新启动optservice sshd命令时,它实际上会运行一个具有修改环境的程序,如下所示:

    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):

sshd 命令在这两种情况下几乎做同样的事情(重启/重新加载):

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:尝试终止发送 HUP 信号的进程,正如您在 snipet 上看到的那样,它需要进程的 PID 来执行此操作。(不管 sshd 是否已经在运行都有效)

    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:它就像执行停止->启动一样。

    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
    }

回答by Borja Tarraso

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:

顺便提一下:在上面的例子中,人们使用的是 sshd,它是守护进程,服务是 ssh。正确的行应该是:

service ssh reload

回答by Jevin

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".

我认为这个“重载”可以用在多个服务的shell脚本中以恢复到初始状态,在这种情况下我们不知道一个服务是否正在运行,所以我们只是让所有这些服务“重载”。

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.

通常对于单个服务的调试问题(或修改),我们希望像“sshd”这样的服务启动,“重启”应该更好,因为我们不需要检查这个服务是否运行成功。

回答by jpaugh

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

某些应用程序(包括多个 Web 服务器)支持无需重新启动即可重新加载其配置。在这种情况下,reload向他们发出信号是最好的方式。

As a use case, it would be great if sshdactually didsupport 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).

作为使用情况下,如果这将是巨大sshd居然没有支持重装配置,而不会影响现有的连接。这将允许在不丢失当前 ssh 连接的情况下验证新配置(例如,在修改权限时,以确保您仍然可以登录)。

Further reading: List of all systemdunit actions

进一步阅读:所有systemd单位行动的列表