大网数据广告活动
服务器运维问答

Openssh - Allow root to access both sftp and ssh

· 52CE 我爱测

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 ChallengeResponseAuthe…

问题原文

原帖链接 · 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:

ForceCommand

Forces the execution of the command specified by ForceCommand, ignoring any command supplied by the client and ~/.ssh/rc if present. The command is invoked by using the user's login shell with the -c option. This applies to shell, command, or subsystem execution. It is most useful inside a Match block. The command originally supplied by the client is available in the SSH_ORIGINAL_COMMAND environment variable. Specifying a command of internal-sftp will force the use of an in-process SFTP server that requires no support files when used with ChrootDirectory. The default is none.

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 root or disable root login altogether
  • use sudo instead of direct root login
  • use public key authentication for any users having sudo privileges
  • 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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2024-01-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。