sshd 仅在使用 bash -x /etc/init.d/sshd restart 重新启动时绑定到低编号端口

nde*_*mou -2 ssh centos

我在 /etc/ssh/sshd_config 中添加了行“端口 110”,就在现有行“端口 22”的/etc/init.d/sshd restart下方,然后期待看到 sshd 侦听两个端口(22 和 110)。但是 netstat -anp 显示 sshd 只侦听默认端口 (22)。

后来我尝试bash -x /etc/init.d/sshd restart并惊讶地看到 sshd 立即绑定到端口 110 !/etc/init.d/sshd restart再次发出一秒钟无视我的变化。重新启动也会忽略我的更改,所以我被卡住了,完全不解。

更新这个奇怪的行为出现在低端口(<1024)

这是在 CentOS 6 服务器上。


细节

这是我对 /etc/ssh/sshd_config 的修改:

grep ^Port /etc/ssh/sshd_config
Port 22
Port 110
Run Code Online (Sandbox Code Playgroud)

netstat 后的输出 bash -x /etc/init.d/sshd restart

netstat -anp | grep -i listen|grep sshd|grep -v :::
tcp        0      0 0.0.0.0:110                 0.0.0.0:*                   LISTEN      7031/sshd           
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      7031/sshd     
Run Code Online (Sandbox Code Playgroud)

netstat 后的输出 /etc/init.d/sshd restart

netstat -anp | grep -i listen|grep sshd|grep -v :::
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      8962/sshd  
Run Code Online (Sandbox Code Playgroud)

另一种说服 sshd 绑定到端口 110 的方法是在调试模式下运行它/usr/sbin/sshd -de (注意这些listening on port 22,110行,但我也通过连接到两个端口进行了测试):

/usr/sbin/sshd -de
debug1: sshd version OpenSSH_5.3p1
debug1: read PEM private key done: type RSA
debug1: private host key: #0 type 1 RSA
debug1: read PEM private key done: type DSA
debug1: private host key: #1 type 2 DSA
debug1: rexec_argv[0]='/usr/sbin/sshd'
debug1: rexec_argv[1]='-de'
Set /proc/self/oom_score_adj from 0 to -1000
debug1: Bind to port 22 on 0.0.0.0.
Server listening on 0.0.0.0 port 22.
debug1: Bind to port 22 on ::.
Server listening on :: port 22.
debug1: Bind to port 110 on 0.0.0.0.
Server listening on 0.0.0.0 port 110.
debug1: Bind to port 110 on ::.
Server listening on :: port 110.
Run Code Online (Sandbox Code Playgroud)

use*_*517 6

SELinux系统限制1024以下端口的端口绑定

semanage port -l | grep ssh
ssh_port_t                     tcp      22
Run Code Online (Sandbox Code Playgroud)

您可以添加另一个端口

semanage port -a -t ssh_port_t -p tcp 110
Run Code Online (Sandbox Code Playgroud)

那将解决你眼前的问题。

  • 如果您直接运行 init 脚本,那么域将转换为 initrc_t,然后进一步转换为 sshd_t。如果您从 bash 子shell 调用它,则不会发生转换并且它会不受限制地运行。 (3认同)