温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linux中怎么设置用户通过SFTP访问目录的权限

发布时间:2021-08-11 17:05:06 来源:亿速云 阅读:223 作者:Leah 栏目:系统运维

今天就跟大家聊聊有关Linux中怎么设置用户通过SFTP访问目录的权限,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

1.查看openssh软件版本,想sftp服务用户只能访问特定的文件目录,版本需要4.8以上

[root@localhost ftp]# rpm -qa | grep openssh
openssh-server-5.3p1-81.el6_3.x86_64
openssh-5.3p1-81.el6_3.x86_64
openssh-clients-5.3p1-81.el6_3.x86_64


2.新增用户,限制用户只能通过sftp访问

代码如下:


[root@localhost ftp]# useradd -m -d /opt/ftp/dave -s /sbin/nologin dave


3.限制用户通过sftp登录进来时只能进入主目录,修改/etc/ssh/sshd_config文件

代码如下:


[root@localhost ftp]# vim /etc/ssh/sshd_config
#Subsystem  sftp    /usr/libexec/openssh/sftp-server
Subsystem sftp internal-sftp
Match User dave
        ChrootDirectory /opt/ftp/dave
        X11Forwarding no
        AllowTcpForwarding no
        ForceCommand internal-sftp


重启ssh
4.测试访问

代码如下:


root@10.1.1.200:test# sftp -oPort=22 dave@10.1.6.175
Connecting to 10.1.6.175...
dave@10.1.6.175's password:
Read from remote host 10.1.6.175: Connection reset by peer
Couldn't read packet: Connection reset by peer


发现连接不上,查看日志

代码如下:


[root@localhost ftp]# tail /var/log/messages
Jan  6 11:41:41 localhost sshd[4907]: fatal: bad ownership or modes for chroot directory "/opt/ftp/dave"
Jan  6 11:41:41 localhost sshd[4905]: pam_unix(sshd:session): session closed for user dave


解决方法:

目录权限设置上要遵循2点:

ChrootDirectory设置的目录权限及其所有的上级文件夹权限,属主和属组必须是root;

ChrootDirectory设置的目录权限及其所有的上级文件夹权限,只有属主能拥有写权限,权限最大设置只能是755。

如果不能遵循以上2点,即使是该目录仅属于某个用户,也可能会影响到所有的SFTP用户。

代码如下:


[root@localhost ftp]# ll
total 4
drwxr-xr-x 3 dave dave 4096 Jan  5 13:06 dave
[root@localhost ftp]# chown root:root dave
[root@localhost ftp]# chmod 755 dave
[root@localhost ftp]# ll
total 4
drwxr-xr-x 3 root root 4096 Jan  5 13:06 dave


然后在测试通过

代码如下:


root@10.1.1.200:test# sftp -oPort=22 dave@10.1.6.175
Connecting to 10.1.6.175...
dave@10.1.6.175's password:
sftp> ls
test
sftp> cd ..
sftp> ls
test
sftp> cd test
sftp> ls
1.txt  
sftp> get 1.txt
Fetching /test/1.txt to 1.txt
/test/1.txt

看完上述内容,你们对Linux中怎么设置用户通过SFTP访问目录的权限有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI