Openssh - 允許 root 存取 sftp 和 ssh

Openssh - 允許 root 存取 sftp 和 ssh

我想在我的伺服器上設定 sftp 由 root 使用者存取。

我將我的配置更改/etc/ssh/sshd_config為:

PermitRootLogin yes
AuthorizedKeysFile  .ssh/authorized_keys
PermitEmptyPasswords yes
ChallengeResponseAuthentication no
Compression no
ClientAliveInterval 15
ClientAliveCountMax 4
#Other options are commented
[...]
Subsystem sftp /usr/libexec/sftp-server

Match user root
ChrootDirectory /
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp

它可以工作,我可以透過 sftp 連接到我的伺服器,但現在我可以再透過 ssh 連線。我收到這則訊息:

This service allows sftp connections only.

同樣的情況也發生在Match group root

如何設定 sftp 以允許 root 但也保留 ssh?


更新:

評論 ForceCommand 內部 sftp 選項 ssh 仍然有效,但我無法透過 sftp 連線。 sshd_配置:
Subsystem   sftp    /usr/libexec/sftp-server
Match user root
ChrootDirectory /
X11Forwarding no
AllowTcpForwarding no
#ForceCommand internal-sftp

錯誤:

user@notebook:~$ sftp root@my_device
Warning: the ECDSA host key for 'my_device' differs from the key for the IP address 'xx.xx.xx.xx'
Offending key for IP in /home/myUser/.ssh/known_hosts:78
Matching host key in /home/myUer/.ssh/known_hosts:93
Are you sure you want to continue connecting (yes/no)? yes
Connection closed.  
Connection closed

在伺服器端:

root@my_device:~# cat /var/log/auth.log | grep ssh
2024-01-17T19:25:21.659884+00:00 my_device sshd[30592]: Accepted none for root from xx.xx.xx.xx port 38024 ssh2
2024-01-17T19:25:21.881328+00:00 my_device sshd[30592]: Received disconnect from xx.xx.xx.xx port 38024:11: disconnected by user
2024-01-17T19:25:21.881997+00:00 my_device sshd[30592]: Disconnected from user root xx.xx.xx.xx port 38024

注意:如果我也刪除,Subsystem sftp /usr/libexec/sftp-server因為在嘗試透過 sftp 連線時出現不同的錯誤: subsystem request failed on channel 0

答案1

指令ForceCommand僅允許 root 使用 sftp,請嘗試註解掉該行。

答案2

OpenSSH 手冊頁sshd_配置(5)描述了這一點:

ForceCommand

強制執行由 指定的命令ForceCommand,忽略客戶端提供的任何命令(~/.ssh/rc如果存在)。該命令是透過使用使用者的登入 shell 和-c選項來呼叫的。這適用於 shell、命令或子系統執行。它在Match塊內最有用。客戶端最初提供的命令在SSH_ORIGINAL_COMMAND環境變數中可用。指定指令internal-sftp將強制使用進程內 SFTP 伺服器,與 一起使用時不需要支援檔案ChrootDirectory。預設為none.

如這裡所述,您ForceCommand internal-sftp只允許 SFTP。

此外,PermitRootLogin yes很危險,因為它啟用 root 密碼身份驗證,並且ChrootDirectory /並不是真正的限制chroot監獄根本不。建議

  • 使用公鑰身份驗證rootroot完全停用登入
  • 使用sudo而不是直接root登入
  • sudo對任何具有權限的使用者使用公鑰身份驗證
  • 使用密碼sudo(與先前的密碼一起,它實際上是用於升級權限的 MFA)。

我一直提醒這些,因為伺服器故障是針對與業務環境相關的問題,而您目前的配置在這種情況下沒有表現出合理的做法。

答案3

您可以嘗試執行此 ansible playbook,它會檢查您的 ssh 配置並建立安全的 ssh 配置。 https://github.com/dev-sec/ansible-collection-hardening/tree/master/roles/ssh_hardening

執行此命令後,您只需要在 sshd_config 中允許 rootlogin,然後 ssh 和 sftp 就可以工作

答案4

好吧,這裡的問題是/usr/libexec/sftp-server伺服器端不存在。我使用新的 Yocto 建置將其新增至映像中,然後我能夠使用預設配置透過 ssh 和 sftp 與 root 進行連接:

# override default of no subsystems
Subsystem   sftp    /usr/libexec/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
#   X11Forwarding no
#   AllowTcpForwarding no
#   PermitTTY no
#   ForceCommand cvs server

相關內容