Baf*_*fer 3 linux permissions ssh sftp
我想在我的服务器上配置 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
Run Code Online (Sandbox Code Playgroud)
它可以工作,我可以通过 sftp 连接到我的服务器,但现在我可以再通过 ssh 连接。我收到这条消息:
This service allows sftp connections only.
同样的情况发生在Match group root
如何配置 sftp 以允许 root 但也保留 ssh?
Subsystem sftp /usr/libexec/sftp-server
Match user root
ChrootDirectory /
X11Forwarding no
AllowTcpForwarding no
#ForceCommand internal-sftp
Run Code Online (Sandbox Code Playgroud)
错误:
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
Run Code Online (Sandbox Code Playgroud)
在服务器端:
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
Run Code Online (Sandbox Code Playgroud)
注意:如果我也删除,Subsystem sftp /usr/libexec/sftp-server因为在尝试通过 sftp 连接时出现不同的错误:
subsystem request failed on channel 0
OpenSSH 手册页sshd_config(5)对此进行了描述:
强制执行由 指定的命令
ForceCommand,忽略客户端提供的任何命令(~/.ssh/rc如果存在)。该命令是通过使用用户的登录 shell 和选项来调用的-c。这适用于 shell、命令或子系统执行。它在Match块内最有用。客户端最初提供的命令在SSH_ORIGINAL_COMMAND环境变量中可用。指定命令internal-sftp将强制使用进程内 SFTP 服务器,与 一起使用时不需要支持文件ChrootDirectory。默认为none.
如此处所述,您ForceCommand internal-sftp只允许 SFTP。
此外,PermitRootLogin yes它是危险的,因为它支持 root 的密码身份验证,并且ChrootDirectory /根本不是一个真正的限制性chroot 监狱。建议
root或root完全禁用登录sudo而不是直接root登录sudo对任何具有权限的用户使用公钥身份验证sudo(与之前的密码一起,它实际上是用于升级权限的 MFA)。我不断提醒这些,因为服务器故障是针对与业务环境相关的问题,而您当前的配置在这种情况下没有表现出合理的做法。