Centos 7 设置 SFTP

近期要给服务器设置一个SFTP用户,能够上传删除修改的SFTP,可是禁止该用户SSH登陆。这里记录下来vim

先升级


yum update
2
 
1
yum update

fox.风服务器

建立用户组 sftp

用户组名为sftpapp

groupadd sftp
x
 
1
groupadd sftp

建立用户 test

例如这个用户名为 testssh

useradd -G sftp -s /sbin/nologin test
2
 
1
useradd -G sftp -s /sbin/nologin test 

-s 禁止用户ssh登录 
-G 加入sftp 用户组ide

建立密码

passwd test
2
 
1
passwd test 

修改配置文件sshd_config

vim /etc/ssh/sshd_config
2
 
1
vim /etc/ssh/sshd_config

修改成以下url

....
##下面这行注释掉
#Subsystem sftp /usr/libexec/openssh/sftp-server
##后面加入
Subsystem sftp internal-sftp
#注意,如下要 放在 本文件的最后行,不然 root用户没法登录
Match Group sftp
X11Forwarding no
AllowTcpForwarding no
ChrootDirectory %h                      
ForceCommand internal-sftp
x
 
1
....
2
##下面这行注释掉
3
#Subsystem sftp /usr/libexec/openssh/sftp-server
4
##后面加入
5
Subsystem sftp internal-sftp
6
#注意,如下要 放在 本文件的最后行,不然 root用户没法登录
7
Match Group sftp
8
X11Forwarding no
9
AllowTcpForwarding no
10
ChrootDirectory %h                      
11
ForceCommand internal-sftp

说明 
Match Group sftp 匹配sftp用户组中的用户 
ChrootDirectory %h 只能访问默认的用户目录(本身的目录),例如 /home/testspa

设置目录权限

chown root:sftp /home/test
chgrp -R sftp /home/test
chmod -R 755 /home/test
#设置用户能够上传的目录,改目录下容许用户上传删除修改文件及文件夹
mkdir /home/test/upload
chown -R test:sftp /home/test/upload
chmod -R 755 /home/test/upload
x
9
 
1
chown root:sftp /home/test
2
chgrp -R sftp /home/test
3
chmod -R 755 /home/test
4
#设置用户能够上传的目录,改目录下容许用户上传删除修改文件及文件夹
5
mkdir /home/test/upload
6
chown -R test:sftp /home/test/upload
7
chmod -R 755 /home/test/upload

重启ssh

systemctl restart sshd.service
2
 
1
systemctl restart sshd.service

连接

新建一个 终端连接,或者在 FTP 客户端中使用SFTP 模式连接(输入相关的IP用户名及端口).net

sftp test@192.1.1.1
2
 
1
sftp test@192.1.1.1

F&Q

若是报相似如下错误rest

sftp Connection to  closed by remote host.
或者
Write failed: Broken pipe 
Couldn't read packet: Connection reset by peer
 
1
sftp Connection to closed by remote host.
2
或者
3
Write failed: Broken pipe 
4
Couldn't read packet: Connection reset by peer 
相关文章
相关标签/搜索