在 Oracle® Solaris 11.2 中管理安全 Shell 访问

退出打印视图

更新时间: 2014 年 9 月
 
 

如何为 sftp 文件创建隔离的目录

此过程配置为 sftp 传输专门创建的 sftponly 目录。用户无法看见传输目录外的任何文件或目录。

开始之前

您必须承担 root 角色。有关更多信息,请参见在 Oracle Solaris 11.2 中确保用户和进程的安全 中的使用所指定的管理权限

  1. 在安全 Shell 服务器上,创建一个隔离的目录作为 chroot 环境。
    # groupadd sftp
    # useradd -m -G sftp -s /bin/false sftponly
    # chown root:root /export/home/sftponly
    # mkdir /export/home/sftponly/WWW
    # chown sftponly:staff /export/home/sftponly/WWW

    在此配置中,/export/home/sftonly 是只有 root 帐户可以访问的 chroot 目录。用户对 sftponly/WWW 子目录具有写入权限。

  2. 仍然是在该服务器上,配置 sftp 组的匹配块。

    /etc/ssh/sshd_config 文件中找到 sftp subsystem 项,并按如下方式修改此文件:

    # pfedit /etc/ssh/sshd_config
    ...
    # sftp subsystem
    #Subsystem       sftp    /usr/lib/ssh/sftp-server
    Subsystem       sftp    internal-sftp
    ...
    ## Match Group for Subsystem
    ## At end of file, to follow all global options
    Match Group sftp
    ChrootDirectory %h
    ForceCommand internal-sftp
    AllowTcpForwarding no

      可以使用以下变量指定 chroot 路径:

    • %h-指定起始目录。

    • %u-指定经验证用户的用户名。

    • %%-对 % 符号进行转义。

  3. 在客户机上,验证配置可正常运行。

    您的 chroot 环境中的文件可能有所不同。

    root@client:~# ssh sftponly@server
    This service allows sftp connections only.
    Connection to server closed. No shell access, sftp is enforced.
    root@client:~# sftp sftponly@server
    sftp> pwd sftp access granted
    Remote working directory: /chroot directory looks like root directory
    sftp> ls
    WWW             local.cshrc     local.login     local.profile
    sftp> get local.cshrc
    Fetching /local.cshrc to local.cshrc
    /local.cshrc    100%  166     0.2KB/s   00:00user can read contents
    sftp> put /etc/motd
    Uploading /etc/motd to /motd
    Couldn't get handle: Permission denieduser cannot write to / directory
    sftp> cd WWW
    sftp> put /etc/motd
    Uploading /etc/motd to /WWW/motd
    /etc/motd     100%  118     0.1KB/s   00:00user can write to WWW directory
    sftp> ls -l
    -rw-r--r--    1 101  10    118 Jul 20 09:07 motdsuccessful transfer
    sftp>