问题描述
从我在Linux上进行的测试看来,
From my tests on Linux, it seems like
service sshd reload
- 仅在
sshd
已经运行时起作用 - 如果
sshd_config
文件有问题,请停止sshd
- 即使sshd_config文件有问题,也返回错误代码0
- Only works when
sshd
is already running - Stops
sshd
if thesshd_config
file has problems - Returns error code 0 even if the sshd_config file has problems
service sshd restart
- 不管
sshd
是否已在运行,都可以工作 - 如果
sshd_config
文件语法无效或存在其他问题,请停止sshd
- 如果
sshd_config
文件有问题,则返回非零错误代码
- Works regardless of whether
sshd
is already running - Stops
sshd
if thesshd_config
file has invalid syntax or other problems - Returns non-zero error code if the
sshd_config
file has problems
我知道他们正在执行不同的操作,但是对我来说,我应该始终使用service sshd restart
似乎毫无道理.在某些情况下为什么最好使用service sshd reload
?
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?
推荐答案
当您运行 service sshd 命令时,其中的 opt 可能会被重新加载/重新启动,它实际上会运行一个程序具有这样的修改后的环境:
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}
例如:
env -i PATH=/sbin:/usr/sbin:/bin:/usr/bin TERM=xterm /etc/init.d/sshd reload
在两种情况下(重新启动/重新加载),sshd命令都执行几乎相同的操作:
The sshd command does almost the same thing in both cases (restart/reload):
重新加载:尝试终止发送HUP信号的进程,正如您在摘录中看到的那样,它需要进程的PID来执行此操作. (不管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()
{
echo -n $"Reloading $prog: "
if [ -n "`pidfileofproc $SSHD`" ] ; then
killproc $SSHD -HUP
else
failure $"Reloading $prog"
fi
RETVAL=$?
echo
}
重新启动:就像执行stop-> start一样.
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
}
这篇关于我为什么要使用"service sshd reload"?优先于"service sshd restart"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!