参考文章:
https://wiki.archlinux.org/title/SFTP_chroot
https://wiki.archlinux.org/title/SCP_and_SFTP
创建用户
groupadd sftponly
useradd -g sftponly -s /usr/bin/nologin -d /srv/sftp jojo # /srv/sftp 是 sftp 的根目录,登录的用户名是 jojo
配置sshd
修改配置文件 /etc/ssh/sshd_config
...
AuthorizedKeysFile .ssh/authorized_keys /etc/ssh/authorized_keys/%u
...
Subsystem sftp /usr/lib/ssh/sftp-server
Match Group sftponly
ChrootDirectory %h
ForceCommand internal-sftp -f AUTH -l INFO -d /uploads
AllowTcpForwarding no
X11Forwarding no
PasswordAuthentication no
配置 sftp 根目录
cd /srv/sftp
mkdir uploads
chown -R :sftponly uploads
chmod 774 uploads
配置密钥认证
生成密钥对:
ssh-keygen -t rsa # 生成一对公私钥
新增配置文件
mkdir /etc/ssh/authorized_keys
chown root:root /etc/ssh/authorized_keys
chmod 755 /etc/ssh/authorized_keys
# 注:此步需要将公钥 id_rsa.pub 的内容保存到服务器的 /etc/ssh/authorized_keys/jojo 文件中( jojo 是登录的用户名)
chmod 644 /etc/ssh/authorized_keys/jojo
使用 filezilla 登录
登录方式使用 密钥登录,key 文件为刚才生成的 私钥。