參考文章:
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 文件爲剛纔生成的 私鑰。