手机
当前位置:查字典教程网 >操作系统 >unix linux >Linux上设置用户通过SFTP访问目录的权限的方法
Linux上设置用户通过SFTP访问目录的权限的方法
摘要:sftp和ftp是两种协议是不同的,sftp是ssh内含的协议,只要sshd服务器启动了,它就可用,它本身不需要ftp服务器启动。1.查看o...

sftp和ftp是两种协议是不同的,sftp是ssh内含的协议,只要sshd服务器启动了,它就可用,它本身不需要ftp服务器启动。

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访问目录的权限的方法】相关文章:

Linux下挂载硬盘分区的几种方法

Linux下清除系统日志的方法

Linux系统下获取主板信息的方法

Linux无盘工作站的做法

真正实现限制SSH单用户登录的方法

Linux下重启apache的方法

在Linux下生成crypt加密密码的方法

Linux下批量Kill多个进程的方法

linux下远程重启apache的方法

linux下配置jdk环境变量的三种方法总结

精品推荐
分类导航