on  it road.com

测试 Chrooted SFTP:

使用 sftp 命令连接到 fileserver-01.onitroad.com。

[root@fileserver-01 etc]# sftp jackli@localhost
jackli@localhost's password:
Connected to localhost.
sftp>

我们已使用 SFTP 协议成功连接到我们的服务器。

检查工作目录和根目录。

sftp> pwd
Remote working directory: /common
sftp> ls -al /
dr-xr-xr-x    3 0        0              20 Sep  9 07:13 .
dr-xr-xr-x    3 0        0              20 Sep  9 07:13 ..
drwxrwsr-x    2 0        1501          163 Sep  9 07:56 common
sftp>

我们可以看到用户会话现在处于 chroot jail 环境中,并且用户无法从这里访问实际的文件系统。

让我们上传一个文件到服务器。

sftp> put hosts
Uploading hosts to /common/hosts
hosts                                         100%  158   244.7KB/s   00:00
sftp> ls -al
drwxrwsr-x    2 0        1501          176 Sep  9 08:10 .
dr-xr-xr-x    3 0        0              20 Sep  9 07:13 ..
-rw-rw----    1 1001     1501          158 Sep  9 08:10 hosts
sftp>

上传不同用户的各种文件后,目录的状态为:

[root@fileserver-01 etc]# ls -al /chroot/sftp/common/
total 32
drwxrwsr-x. 2 root    dev   176 Sep  9 13:10 .
dr-xr-xr-x. 3 root    root   20 Sep  9 12:13 ..
-rw-rw----. 1 mansoor dev  1409 Sep  9 12:50 anaconda-ks1.cfg
-rw-rw----. 1 jackli   dev  1409 Sep  9 12:48 anaconda-ks.cfg
-rw-rw----. 1 mansoor dev     0 Sep  9 12:10 exports
-rw-rw----. 1 jackli   dev   506 Sep  9 12:16 fstab
-rw-rw----. 1 jackli   dev   158 Sep  9 13:10 hosts
-rw-rw----. 1 jackli   dev  1452 Jun  2 14:56 ldapserver.pem
-rw-rw----. 1 jackli   dev   925 Sep  9 12:09 passwd
-rw-rw----. 1 danish  dev  2885 Sep  9 12:54 vmware-vgauthsvc.log.0
-rw-rw----. 1 jackli   dev   813 Sep  9 12:53 yum.conf

还要检查我们的用户是否可以使用 ssh 连接。

[root@fileserver-01 etc]# ssh mansoor@localhost
mansoor@localhost's password:
This service allows sftp connections only.
Connection to localhost closed.
[root@fileserver-01 etc]#

我们已经成功地为 CentOS 7 中的 SFTP 用户配置了一个 chroot 协作目录,带有 chroot jail 和受限的 Shell 访问。

在 RHEL/CentOS 7 上配置 Chroot SFTP 服务器

Chroot 是一种更改当前正在运行的进程及其子进程的明显根目录的操作。
该环境称为 chroot jail。
chroot jail 中的用户无法访问指定目录外的文件。

在本文中,我们将为我们的用户配置一个协作目录,以便通过 SFTP 协议安全地向文件服务器上传/下载文件,并使用 chroot jail 环境限制用户对协作目录的访问。

|

在 RHEL/CentOS 7 上配置 Chroot SFTP 服务器

sftp 是 openssh-clients 包的一部分,几乎所有的 linux 发行版都安装了它。
因此,我们不必在我们的机器上显式安装它,而是只根据我们的要求配置它。

为协作用户创建一个组。

[root@fileserver-01 ~]# groupadd -g 1501 dev

创建 3 个协作用户,补充组 dev 和登录 shell 为 /sbin/nologin 以限制用户对 shell 的访问。

[root@fileserver-01 ~]# useradd -u 1001 -G dev -s /sbin/nologin jackli
[root@fileserver-01 ~]# useradd -u 1002 -G dev -s /sbin/nologin mansoor
[root@fileserver-01 ~]# useradd -u 1003 -G dev -s /sbin/nologin danish

将这些用户的主目录设置为 /common 。

[root@fileserver-01 ~]# usermod -d /common jackli
[root@fileserver-01 ~]# usermod -d /common mansoor
[root@fileserver-01 ~]# usermod -d /common danish

为用户设置密码。

[root@fileserver-01 ~]# echo 123 | passwd jackli --stdin
Changing password for user jackli.
passwd: all authentication tokens updated successfully.
[root@fileserver-01 ~]# echo 123 | passwd mansoor --stdin
Changing password for user mansoor.
passwd: all authentication tokens updated successfully.
[root@fileserver-01 ~]# echo 123 | passwd danish --stdin
Changing password for user danish.
passwd: all authentication tokens updated successfully.
[root@fileserver-01 ~]#

创建协作目录并根据需要调整其权限。

[root@fileserver-01 ~]# mkdir -p /chroot/sftp
[root@fileserver-01 ~]# chmod 555 /chroot/sftp
[root@fileserver-01 ~]# mkdir /chroot/sftp/common/
[root@fileserver-01 ~]# chgrp dev /chroot/sftp/common/
[root@fileserver-01 ~]# chmod 2775 /chroot/sftp/common/

配置 sshd 服务来处理协作用户。

[root@fileserver-01 ~]# vi /etc/ssh/sshd_config

搜索并注释以下行。

#Subsystem       sftp    /usr/libexec/openssh/sftp-server

在 /etc/ssh/sshd_config 的末尾添加以下几行。

Subsystem       sftp    internal-sftp
Match Group dev
 X11Forwarding no
 AllowTCPForwarding no
 ChrootDirectory /chroot/sftp/
 ForceCommand internal-sftp -u 007

我们将用户掩码设置为 007 以限制其他用户访问我们的文件。
但是,我们可以根据自己的要求调整 umask。
(例如,如果我们要求组成员之间不能互相更改文件,则可以将 umask 设置为 027 )。

保存并退出 vi 编辑器。

重新启动 sshd 服务使更改生效。

[root@fileserver-01 ~]# systemctl restart sshd
日期:2020-09-17 00:12:28 来源:oir作者:oir