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

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

我在 /etc/ssh/sshd_config 中现有行“端口 22”的下方添加了行“端口 110”,然后希望/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

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     

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  

另一种让 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.

答案1

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

semanage port -l | grep ssh
ssh_port_t                     tcp      22

您可以添加另一个端口

semanage port -a -t ssh_port_t -p tcp 110

这可以解决您当前的问题。

相关内容