2024年5月30日发(作者:)

Sftp服务器搭建手册

一 用途及应用场景

提供sftp服务,scp服务,可以精确控制权限。

不提供给用户shell,chroot 用户在主目录

用于和外部之间的文件交换,防止安全事件。同时也可以结合sshterm,来实现对文件的操作记录。

目前广泛应用在运营上传下载文件,和银行物流公司等外部实体进行对账文件交换等

二 chroot的方法:

Sftp有个缺点,默认用户可以四处转悠,同时可以下载权限为022的文件,比较不安全。

实现chroot的方法很多。

方法1: Openssh 在4.8之前,是不提供 chroot的功能的,有通过给代码打补丁的做法,使

sftp-server 支持chroot,不过不是很方便。

方法2:4.8之后,代码加入了chroot支持,可以通过在配置文件里定义规则:

Match group sftponly

ChrootDirectory /home/%u

X11Forwarding no

AllowTcpForwarding no

ForceCommand internal-sftp

但是升级和配置起来有点繁琐,同时,openssh新版本也可能面临一些安全问题。

方法3:第三方工具rssh等

三 通过rssh工具来实现限制:

Rssh的功能:限制shell登录,对scp和sftp实现帐号规则控制。

已知的绕过rssh限制执行系统命令的安全问题,在2.3.2的版本中已经得到修复

1 下载

wget /sourceforge/rssh/

2 安装

./configure --prefix=/usr --sysconfdir=/etc

缺省的几个比较重要的文件应该是安装在

/usr/bin/rssh

/usr/lib/rssh/rssh_chroot_helper

/etc/

/etc/shells 文件里面也应该包含有 /usr/bin/rssh

3 修改用户的 shell 为 /usr/bin/rssh , 这样这个用户就不能 ssh 登录了

usermod -s /usr/bin/rssh test

创建用户 sftp 的 chroot 目录

sh /home/test/local test 0755

最后修改 /etc/:

首先确认 allowsftp 被设置

再添加一行配置:user=test:077:00010:/home/rssh_chroot/test

077 是给 sftp 上来的用户分配缺省 umask

00010 是 access bits, 倒数第二个 '1' 表示给该用户 sftp 权限

这个脚本的作用,主要是通过ldd命令,精确定位sftp所需要的系统依赖文件

复制到用户的主目录下,构建一个独立封闭的运行环境,将该用户完全和系统分离开来。

RHEL 3的环境下,这个脚本是运行正常的。

RHEL4 和5的换将下,会有一个基本依赖的lib没有复制过去。

这时,手工执行一下复制即可:

cp /lib/.2 /用户目录/lib/

四 debug

在非red hat的linux版本上,上述的脚本工作可能会有点问题。

这时,按照下面的做法,手工排查一下依赖关系即可

There are too many possibilities to cause "connection closed".

Some files you need to check: (Some of them may not be necessary, but they are

worth trying.)

1, /chroot/lib/.2 or .2

2, /chroot/lib/libnss_.2

3, /chroot/dev/null

4 /chroot/lib64 and /chroot/usr/lib64, if you are using amd64

In the real root, /usr/lib/rssh/rssh_chroot_helper has to be setuid root.

run rssh -v, make sure the path it prints out is right. (Otherwise you need to

do some hard link.) Also make sure that the path "Subsystem sftp " you specified

in sshd_config is correct.