开启远程 SFTP 连接服务

开启远程 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

 

posted @ 2020-11-04 17:43  walkersss  阅读(3661)  评论(0编辑  收藏  举报