Linux 搭建 sftp 服务器详解

发布于:2024-07-02 ⋅ 阅读:(10) ⋅ 点赞:(0)

(首发地址:学习日记 https://www.learndiary.com/2024/07/sftp/

朋友们,大家好。我是淘宝网“学习日记小店”从事 Linux 服务的 learndiary。今天给大家分享 Linux 搭建 sftp 服务器的一些经验,对其中初学者可能会犯错的一些地方作了详细的讲解。视频演示:【Linux 搭建 sftp 服务器详解】 https://www.bilibili.com/video/BV1Cy411z7fc/

Linux 搭建 sftp 服务器详解

一、背景

前段时间我接到一个订单,设置 sftp 服务器,要求使用 /ftp01 顶层目录作为 sftp 存放文件的目录。但是,当同时限制了用户活动根目录为 /ftp01 后,我发现对应用户不能登录 sftp 服务器了。由此,我对 sftp 服务器作了一下深入的学习研究。后面再说可以解决这个特殊要求订单的方法。下面是一些相关的详细过程,供大家参考。

二、通用 sftp 服务器搭建

这里设置一个最简单的通用 sftp 服务器,然后介绍一下涉及的5个知识点。

1、软硬件平台介绍
在 Deepin 20.9 Linux 宿主机上使用 VirtualBox 虚拟机,在虚拟机上安装 openEuler 24.03 LTS 作为示范,OpenSSH Server 版本为 9.3。其它版本的 Linux 和 OpenSSH 也可参考。

2、sftp 服务器设置步骤
1)、创建使用 sftp 服务的用户 ftp01

sudo su -
useradd -m -s /sbin/nologin ftp01
passwd ftp01

2)、创建 ftp01 保存文件的目录 /sftp/ftp01

mkdir /sftp/ftp01 -pv
chmod ftp01:ftp01 /sftp/ftp01 -v

3)、修改 /etc/ssh/sshd_config 配置文件
先注释掉“Subsystem sftp”开头的这一行,即在行首加个#
再在配置文件的最后面加上

Subsystem sftp internal-sftp -d %u
Match User ftp01
ChrootDirectory /sftp

保存配置文件后退出,执行 service sshd reload 重载服务配置就完成了。
可以使用 sftp 客户端以用户 ftp01 登录试试,可以上传下载文件,活动范围限制在 /sftp 中,一切正常。同样用户使用 ssh 不能登录。达到预期效果。

3、知识点讲解
1)、internal-sftp 与 sftp-server 的区别
经过我的测试,使用原默认的sftp-server,当前用户的 shell 必须是像 /bin/bash 这样可以使用的普通 shell,而不能是像 /sbin/nologin 这种禁止 ssh 登录的 shell。否则,sftp 用户不能登录。另外,原默认的 sftp-server 不能chroot,所以不能限定 sftp 用户登录后的活动范围。有可能是因为 chroot 环境找不到 shell。但据说原默认的 sftp-server 对旧版 openssh 的兼容性较好,而且可以记录日志。

而 internal-sftp 简单性、性能、对 ChrootDirectory 的兼容性是它的优点。另外,我这里还给它加一个安全性优于 sftp-server。

总之,通常情况下,我们建立 sftp 服务器应该总是选择 internal-sftp。

参考网址:OpenSSH: Difference Between internal-sftp and sftp-server https://www.baeldung.com/linux/openssh-internal-sftp-vs-sftp-server

2)、Match 条件指令
根据 man 文档,Match 条件指令块必须后面接另一个 Match 行(如 Match All),或者放在配置文件最后。用 “Match All” 来结束 Match 块的用法我没有看到正式的明确的文档,故我这里采用的方法是直接把 Match 块放到配置文件的最后。
参考联机 man 文档摘录:

man ssh_config
Match Introduces a conditional block. If all of the criteria on the Match line are satisfied, the keywords on the following lines override those set in the
global section of the config file, until either another Match line or the end of the file. If a keyword appears in multiple Match blocks that are
satisfied, only the first instance of the keyword is applied.

3)、ChrootDirectory 限定活动范围指令
根据 man 文档,ChrootDirectory 限定活动范围(Chroot)的目录必须是全路径每个目录都只能被 root 拥有,且不能被其他用户和组写入。这就决定了 Chroot 的目录不能直接作为普通用户 sftp 存放目录,除非存放在里面新建的二级目录中。而 root 用户通常不会作为 sftp 的直接用户。
参考联机 man 文档摘录:

man ssh_config
ChrootDirectory
Specifies the pathname of a directory to chroot(2) to after authentication. At session startup sshd(8) checks that all components of the pathname are
root‐owned directories which are not writable by any other user or group. After the chroot, sshd(8) changes the working directory to the user’s home
directory. Arguments to ChrootDirectory accept the tokens described in the “TOKENS” section.

4)、sftp 用户家目录及免密码登录
sftp 用户的家目录和文件保存目录是两个概念。可以在家目录下设置密钥自动登录。方法跟 ssh 密钥登录一样。

5)、SELinux 属性对 sftp 密钥登录影响
在基于红帽或类似红帽的 Linux 系统中,一般都默认开启了 SELinux。在做 sftp 密钥登录时,如果 ~/.ssh/authorized_keys 的 SELinux 属性不是 ssh_home_t 属性(如 default_t),可能会由于 SELinux 属性不对而拒绝密钥自动登录。这种情况通常可以通过恢复相关文件默认的 SELinux 属性解决(restorecon -R -v ~/.ssh)。
参考链接:
SELinux preventing ssh via public key https://unix.stackexchange.com/questions/136877/selinux-preventing-ssh-via-public-key

三、解决顶层目录为 sftp 目录的问题

通过上述几个要点尤其是 ChrootDirectory 指令的讲解,可以看出,如果要把像 /ftp01 这样的顶层目录作为 sftp 直接保存文件的目录,一个方法就是不限制 sftp 登录用户的活动范围,允许他登录后可以在系统中自由活动,与最前面的 /etc/ssh/sshd_config 设置不同的地方就是去掉最后 Match 和 ChrootDirectory 开头的 2 行,Subsystem 开头的那行改为“Subsystem sftp internal-sftp -d /ftp01”,保存并重载服务设置即可。

四、扩展知识

sftp 公共目录、工作组目录设置。
参考链接:
林哥讲运维:一分钟学会:部署sftp服务器 https://www.bilibili.com/video/BV1yx4y147Ki