问题原文
原帖链接 · Baffer · CC BY-SA 4.0
I want to configure sftp on my server to be accessed by root user.
I changed my configuration of /etc/ssh/sshd_config to:
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
It works and I can connect by sftp to my server, but now I can connect by ssh anymore. I got this message:
This service allows sftp connections only.
The same happen with Match group root
How can I configure sftp to allow root but keeping ssh too?
Update:
Commenting ForceCommand internal-sftp option ssh still works but I cannot connect by sftp. sshd_config:Subsystem sftp /usr/libexec/sftp-server
Match user root
ChrootDirectory /
X11Forwarding no
AllowTcpForwarding no
#ForceCommand internal-sftp
Error:
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
On the server side:
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
Note: If I also remove Subsystem sftp /usr/libexec/sftp-server as I got a different error when trying to connect by sftp:
subsystem request failed on channel 0
社区高分解答
原帖链接 · Esa Jokinen · CC BY-SA 4.0
The OpenSSH man page sshd_config(5) describes this:
Forces the execution of the command specified by
ForceCommand, ignoring any command supplied by the client and~/.ssh/rcif present. The command is invoked by using the user's login shell with the-coption. This applies to shell, command, or subsystem execution. It is most useful inside aMatchblock. The command originally supplied by the client is available in theSSH_ORIGINAL_COMMANDenvironment variable. Specifying a command ofinternal-sftpwill force the use of an in-process SFTP server that requires no support files when used withChrootDirectory. The default isnone.
As told here, your ForceCommand internal-sftp only allows SFTP.
Furthermore, PermitRootLogin yes is dangerous as it enables password authentication for root, and ChrootDirectory / is not really a limiting chroot jail at all. It is recommended to
- use public key authentication for
rootor disablerootlogin altogether - use
sudoinstead of directrootlogin - use public key authentication for any users having
sudoprivileges - use password for
sudo(together with the previous it is effectively an MFA for the escalated privileges).
I keep reminding of these as Server Fault is for questions related to business environments and your current configuration does not demonstrate reasonable practices in this context.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
