Linux sftp限制用户访问指定目录
一、创建新用户:
[root@controller ~]# useradd test
[root@controller ~]# passwd test
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.
二、修改sshd配置文件:
[root@controller ~]# vi /etc/ssh/sshd_config
#Subsystem sftp /usr/libexec/openssh/sftp-server //注释掉这句,不然报错
添加以下语句
Subsystem sftp internal-sftp
Match User test //设置控制的用户或组
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp
ChrootDirectory /home/sftp //允许用户访问的目录
三、重启ssh服务:
[root@controller ~]# systemctl restart sshd
四、创建目录:
[root@controller ~]# mkdir /home/sftp
[root@controller ~]# chown -R root:test /home/sftp
[root@controller ~]# chmod -R 750 /home/sftp/
五、测试登陆:
[root@controller ~]# sftp test@192.168.31.130
sftp> pwd
Remote wsftp> mkdir 123
Couldn’t create directory: Permission deniedorking directory: /
因为给了750权限所以无法创建目录
在指定目录下新建一个子目录:
[root@controller ~]# mkdir /home/sftp/test
[root@controller ~]# chown test:test /home/sftp/test/
[root@controller ~]# chmod -R 755 /home/sftp/test/
再重新测试下:
[root@controller ~]# sftp test@192.168.31.130
test@192.168.31.130’s password:
Connected to 192.168.31.130.
sftp> cd file/
sftp> mkdir 123
sftp> ls
123
sftp> quit
[root@controller ~]# ls /home/sftp/file/
123
五、注意事项:
sftp用户访问指定目录需要设置用户和组的权限为root,并设置权限为755,但此目录下的文件权限可以自己根据需求设置。