centos 切换用户_centos 7 搭建sftp服务,限制用户访问目录,共享目录

第一步,切换为root

su

第二步,配置/etc/ssh/sshd_config

vi /etc/ssh/sshd_config

输入G,直达配置文件底部寻找是否有

Subsystem sftp  /usr/libexec/openssh/sftp-server

有则注释掉

# Subsystem sftp  /usr/libexec/openssh/sftp-server

输入

Subsystem sftp internal-sftp
Match Group grouptest
ForceCommand internal-sftp
ChrootDirectory /var/report/grouptest/jack
PermitTunnel no
AllowAgentForwarding no
AllowTcpForwarding no
X11Forwarding no

这里可以输入变量%u和%h。%u可以自动获得用户名/var/report/grouptest/%u,适合跳转用户比较多时使用,%h则为直接跳转到home该用户目录下。这里务必注意ChrootDiectory

ChrootDirectory

Specifies the pathname of a directory to chroot(2) to after authentication. All components of the pathname must be root-owned directories that are not writable by any other user or group. After the chroot, sshd(8) changes the working directory to the user's home directory.

根据官方文档说明可知,该跳转目录必须为root作为owner,如果不是,用FileZilla以sftp登录会弹出出Network error Software caused connection abort的错误。可以用如下命令更改

chown root:grouptest /var/report/grouptest/jack 

Match 可以组Group,也可以是用户User,取决于具体需求。

第三步,设置目录,分配用户名,为用户设置密码

name="jack"
groupname="grouptest"
groupadd $groupname
useradd -g  $groupname -s /sbin/nologin $name
mkdir -p /var/report/$groupname
chmod g+rx /var/report/$groupname
mkdir -p /var/report/$groupname/$name
chgrp -R $groupname /var/report/$groupname
chmod g+rwx /var/report/$groupname/$name
chown root:$groupname /var/report/$groupname/$name
chmod 755 /var/report/$groupname/$name
service sshd restart

这里useradd -s 、sbin/nologin是禁止用户ssh登录,可以把以上代码保存为useradd.sh,直接执行bash useradd.sh即可

第四部,绑定数据文件夹

mkdir  -p /var/report/grouptest/jack/data
mount --bind /home/anotherusename/data /var/report/grouptest/jack/data

这里共享数据我们不能用symbolic link和hard link,因为我们用Chroot限制了jack的根目录为/var/report/grouptest/jack, 用上面的link会访问不了,Jack的根由/ 变为了/var/report/grouptest/jack。因此只能用mount --bind

原文解释如下

root@server:/home/username# ln -s /var/www www
root@server:/home/username# cd www
root@server:/home/username/www# chown username:username *

Now, with filezilla, I can see www folder like this:

de9315a0a71bc2bfbca7f279805c9795.png

But when I try to open it, I get this:

3f05fc67944d5ff7fa1a320221cac33f.png

It's likely the SFTP is being chrooted, so that the directory /var/www is not available to the user in the chroot jail.
Look in /etc/ssh/sshd_config and examine the sftp directives. Do you see something like:
Match group sftp ChrootDirectory /home/%u AllowTcpForwarding no ForceCommand internal-sftp
The sshd_config man page is here.
Basically, once the user is in /home/username in SFTP, that directory becomes / and references outside of /home/username are not available. In fact, a symlink like ln -s /var/www /home/username/www will look like you're trying to reach /home/username/var/www (i.e., /home/username is now / so any link that references /var/www must also be a subdirectory of /home/username in the context of the chroot).
As a solution, you can turn off the chroot (but this will have other security implications, mainly with SFTP users having full rein over your filesystem). You can do a loop mount of /var/www into /home/username/www (something like mount --bind /var/www /home/username/www (check your documentation for mount) which should work as you'd expect under chroot). You can also muck with the sshd_config file to exclude that one particular user from chroot (though, again, with security implications).
I would try the bind mount first.

第五步,用FileZilla登录查看

附:

-rwxrw-r‐-1 root root 1213 Feb 2 09:39 abc

- 第一个字符代表文件(-)、目录(d),链接(l)

- 其余字符每3个一组(rwx),读(r)、写(w)、执行(x)

- 第一组rwx:文件所有者的权限是读、写和执行

- 第二组rw-:与文件所有者同一组的用户的权限是读、写但不能执行

- 第三组r--:不与文件所有者同组的其他用户的权限是读不能写和执行

也可用数字表示为:r=4,w=2,x=1 因此rwx=4+2+1=7

Reference:

centos 安装sftp服务 - Jack Deng - 博客园​www.cnblogs.com groupdel: cannot remove user's primary group​ask.apelearn.com
08f2fcd4255f2968dd5c18b95bb9e2d9.png
Linux系列教程(十六)--Linux权限管理之ACL权限 - YSOcean - 博客园​www.cnblogs.com
b39a987fce66e7f014a3537ef72620cc.png
Linux文件权限与属性详解 之 ACL​www.cnblogs.com
db5ec97a463a99771b6f134f352429f7.png
https://www. cnblogs.com/xqzt/p/5432 612.html Linux chmod命令详解 - adolfmc - 博客园​www.cnblogs.com 每天一个linux命令:chown - 小强斋太 - 博客园​www.cnblogs.com
f0e6eb857dcf7ad03ec2597628ac9cb1.png
Linux chown command help and examples​www.computerhope.com
8fe99c9470a3a084cb3cbfff0d0e10e1.png
How to restrict a user to one folder and not allow them to move out his folder​unix.stackexchange.com
0db7318bf6adde3a5fb47274abdccce7.png
Linux下用户组、文件权限详解 - 打伞的鱼 - 博客园​www.cnblogs.com
5a8aa8f0e3b14ffaa1070b672799878d.png
How to Create Hard and Symbolic Links in Linux​www.tecmint.com
c8f46cfae530cc7a137d2ece4d714d90.png
Unix Create a Symbolic Link - nixCraft​www.cyberciti.biz
f36cd57a52b9ab48debcf413c3747ba4.png
What is the difference between a hard link and a symbolic link?​askubuntu.com
8be71f73161db1b395d8c19e808f659d.png
Invalid cross-device link while Hardlinking in the same file system​unix.stackexchange.com
0db7318bf6adde3a5fb47274abdccce7.png
Symbolic link and filezilla over sftp​serverfault.com
317c0cc70cdccd0a45ea05c470a6ef8c.png
https://serverfault.com/questions/440426/sftp-file-symlinks-in-a-jailed-chrooted-directory​serverfault.com
317c0cc70cdccd0a45ea05c470a6ef8c.png
SFTP: file symlinks in a jailed (chrooted) directory SFTP: file symlinks in a jailed (chrooted) directory​serverfault.com
317c0cc70cdccd0a45ea05c470a6ef8c.png
How to restrict a user to one folder and not allow them to move out his folder​unix.stackexchange.com
0db7318bf6adde3a5fb47274abdccce7.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值