linux下禁止某一用户使用ssh登陆但可使用sftp登陆

首先修改sshd的配置文件:
在root用户下
输入:vi /etc/ssh/sshd_config
#该行(上面这行)注释掉
#Subsystem sftp /usr/lib/openssh/sftp-server
 
# 添加以下几行
Subsystem sftp internal-sftp 
Match group sftp
#Match user test
#匹配sftp组,如为单个用户可用:Match user 用户名;  设置此用户登陆时的shell设为/bin/false,这样它就不能用ssh只能用sftp
ChrootDirectory /home/test
#指定用户被锁定到的那个目录,为了能够chroot成功,该目录必须属主是root,并且其他用户或组不能写
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp
添加用户组和用户
#添加用户组
groupadd sftp

#添加用户
useradd -d /home/test -m -s /bin/false -g sftp test

#修改密码
passwd test

重启SSH服务
service sshd restart

或者

/etc/init.d/ssh reload

测试ssh
[root@localhost etc]# ssh test@172.19.194.30
test@172.19.194.30's password: 
Write failed: Broken pipe
登陆失败,提示Write failed: Broken pipe错误

再测试sftp
[root@localhost etc]# sftp test@172.19.194.30
test@172.19.194.30's password: 
Write failed: Broken pipe
Couldn't read packet: Connection reset by peer
同样提示Write failed: Broken pipe

按理说此方法应该是靠谱的为什么会提示失败呢,通过查找发现是目录权限配置导致的:
https://my.oschina.net/davehe/blog/100280

目录权限设置上要遵循2点:
ChrootDirectory设置的目录权限及其所有的上级文件夹权限,属主和属组必须是root;
ChrootDirectory设置的目录权限及其所有的上级文件夹权限,只有属主能拥有写权限,权限最大设置只能是755。

修改/home/test 目录权限为755

chmod 755 /home/test -R

再次测试
[root@localhost etc]# ssh test@172.19.194.30
test@172.19.194.30's password: 
Could not chdir to home directory /home/test: No such file or directory
This service allows sftp connections only.
Connection to 172.19.194.30 closed.
和预期一致:ssh尝试连接失败。

[root@localhost etc]# sftp test@172.19.194.30
test@172.19.194.30's password: 
Connected to 172.19.194.30.
sftp> ls
a                a.log            authorized_keys  mysql.sh         
sftp>
sftp测试连接成功!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值