開啟遠程 SFTP 連接服務:
1、創建用戶組 sftp
groupadd sftp
2、創建用戶 xinxin02
useradd -G sftp -s /sbin/nologin xinxin02
-s 禁止用戶ssh登錄
-G 加入sftp用戶組
3、創建密碼
passwd xinxin02
4、修改配置文件 sshd_config
vim /etc/ssh/sshd_config
將下面這行注釋
# Subsystem sftp /usr/libexec/openssh/sftp-server
然后在末行加入:
Subsystem sftp internal-sftp
Match Group sftp
ChrootDirectory /home/xinxin02
X11Forwarding no
AllowTcpForwarding no
#ChrootDirectory %h
ForceCommand internal-sftp
systemctl restart sshd
說明:
Match Group sftp 匹配sftp用戶組中的用戶
ChrootDirectory %h 只能訪問默認的用戶目錄(自己的目錄), 例如 /home/test
5、設置目錄權限
chown root:sftp /home/xinxin02
chgrp -R sftp /home/xinxin02
chmod -R 755 /home/xinxin02
# 設置用戶可以上傳的目錄, 該用戶下允許用戶上傳刪除修改文件及文件夾
mkdir /home/xinxin02/prod
mkdir /home/xinxin02/test
chown -R xinxin02:sftp /home/xinxin02/prod
chown -R xinxin02:sftp /home/xinxin02/test
chmod -R 755 /home/xinxin02/prod
chmod -R 755 /home/xinxin02/test
6、解決報錯問題
修改 /etc/selinux/config 中SELINUX 為 disabled
7、使用sftp客戶端連接服務器
使用第三方客戶端連接sftp服務器, 或者使用Python中的paramiko模塊連接
linux下測試:
sftp -P 66992 xinxin02@10.10.11.11
ls
cd ..
put /root/test.txt
rm test.txt
?
bye
多用戶,多目錄的權限配置:
vi /etc/ssh/sshd_config
Subsystem sftp internal-sftp
Match User test01
ChrootDirectory /home/test01
X11Forwarding no
AllowTcpForwarding no
#ChrootDirectory %h
ForceCommand internal-sftp
#Subsystem sftp internal-sftp
Match User test02
ChrootDirectory /home/test02
PasswordAuthentication yes
#X11Forwarding no
#AllowTcpForwarding no
#ChrootDirectory %h
#ForceCommand internal-sftp
systemctl restart sshd
測試:
sftp test01@1.1.1.1
sftp test02@1.1.1.1