SSH Server – SFTP only + Chroot

參考網站:
CentOS 7 : SSH Server : SFTP only + Chroot : Server World

以前的作法都是不開放一般使用者使用 ssh,讓使用者使用 ftp 來傳輸檔案,不過 FTP Server 算是比較不安全的通訊協定而且還要安裝建置 FTP Server,所以改用 SFTP,因為帳號密碼及傳輸資料都有經過加密,會比較安全。
以前的作法:
# useradd test -d /home/test -m -s /bin/nologin
改用 SFTP 的作法:
1. 建立 sftp 群組
# groupadd sftp_users
2. 新增使用者
# useradd test -G sftp_users -d /home/test -m
如果使用者已經建立完成
# usermod -G sftp_users test
3. 設定帳號密碼
# passwd test
Changing password for user test.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[@more@]4. 修改 SSH Server 設定
# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.$(date +%F)
# vim /etc/ssh/sshd_config
#Subsystem      sftp    /usr/libexec/openssh/sftp-server
Subsystem       sftp    internal-sftp

Match Group sftp_users
        X11Forwarding no
        AllowTcpForwarding no
        ChrootDirectory /home
        ForceCommand internal-sftp

5. 重新啟動 SSH Server
# systemctl restart sshd.service

6. 進行測試
# ssh test@192.168.1.17
test@192.168.1.17’s password:
Could not chdir to home directory /home/test: No such file or directory
This service allows sftp connections only.
Connection to 192.168.1.17 closed.

# sftp test@192.168.1.17
Connecting to 192.168.1.17…
test@192.168.1.17’s password:
sftp> ls -l
drwx–x–x    2 1000     1001         4096 Mar 11 02:05 test
sftp> bye

一般使用者可以使用 FileZilla 或是 WinSCP