致命:无法初始化 SFTP:无法连接

致命:无法初始化 SFTP:无法连接

我在 Cygwin 上运行 OpenSSH,我试图将其仅用作 SFTP 服务器。我首先使用默认设置(外部 SFTP 服务器sftp-server)安装了它,它工作正常。我能够运行 PuTTY 的 PSFTP 并“打开本地主机”并浏览我的文件。

但为了安全起见我想:

  • 仅允许 SFTP 访问(不允许 SSH)
  • 仅允许我登录(不允许其他用户登录)
  • 仅允许我浏览自己的主目录
  • 仅允许只读访问

为了实现这一点,我在末尾添加了以下内容/etc/sshd_config

Subsystem   sftp    internal-sftp

Match User myusername
        ChrootDirectory /home/myusername
        AllowTCPForwarding no
        X11Forwarding no
        ForceCommand internal-sftp -d / -R

Match User !myusername
        ForceCommand echo 'successful login man, congrats'

(最后一部分来自这个答案,这实际上阻止了匹配的用户登录。)

请注意,我还添加了-d /命令internal-sftp行,因为它告诉我它默认为主目录,所以我认为它可能会尝试默认加载/home/myusername,这将映射到/home/myusername/home/myusername真实系统(不存在的路径)。

但是,当我尝试登录时,出现以下错误:

psftp> open localhost
login as: myusername
myusername@localhost's password:
Fatal: unable to initialise SFTP: could not connect
psftp>

有趣的是,我在服务器上,但实际上并不在 SFTP 上:

psftp> open localhost
login as: myusername
myusername@localhost's password:
Fatal: unable to initialise SFTP: could not connect
psftp> open localhost
psftp: already connected
psftp> pwd
Remote directory is (null)
psftp>

我做错了什么,或者我如何获取日志以进行进一步的故障排除?

答案1

来自 sshd 手册:

ChrootDirectory
Specifies the pathname of a directory to chroot(2) to after 
authentication. All components of the pathname must be root-owned 
directories that are not writable by any other user or group. After the 
chroot, sshd(8) changes the working directory to the user's home directory.

最有可能的是,您的主目录不属于 root,而 sshd 拒绝将根目录切换到非 root 拥有的目录,因为这不安全。创建 /home/chroot,将其切换为 root,然后将您的主目录移动到该目录下。

为了轻松调试这些问题并避免将自己锁定,我建议在不同的端口上运行单独的 sshd 实例,处于非分离模式并启用调试:

 /usr/sbin/sshd -D -d -p 2222 

相关内容