只有在使用 bash -x /etc/init.d/sshd restart 重新啟動時,sshd 才會綁定到低編號端口

只有在使用 bash -x /etc/init.d/sshd restart 重新啟動時,sshd 才會綁定到低編號端口

我在 /etc/ssh/sshd_config 中新增了行“Port 110”,就在現有行“Port 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

之後 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

這將解決你眼前的問題。

相關內容