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

来源:互联网 发布:ar是什么软件 编辑:程序博客网 时间:2024/06/01 08:54
  1. 首先修改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只能用sftpChrootDirectory /home/test#指定用户被锁定到的那个目录,为了能够chroot成功,该目录必须属主是root,并且其他用户或组不能写X11Forwarding noAllowTcpForwarding noForceCommand internal-sftp
  1. 添加用户组和用户

#添加用户组
groupadd sftp

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

#修改密码
passwd test

  1. 重启SSH服务

service sshd restart

或者

/etc/init.d/ssh reload

  1. 测试ssh
[root@localhost etc]# ssh test@172.19.194.30test@172.19.194.30's password: Write failed: Broken pipe

登陆失败,提示Write failed: Broken pipe错误

  1. 再测试sftp
[root@localhost etc]# sftp test@172.19.194.30test@172.19.194.30's password: Write failed: Broken pipeCouldn't read packet: Connection reset by peer

同样提示Write failed: Broken pipe

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

https://my.oschina.net/davehe/blog/100280

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

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

chmod 755 /home/test -R

  1. 再次测试
[root@localhost etc]# ssh test@172.19.194.30test@172.19.194.30's password: Could not chdir to home directory /home/test: No such file or directoryThis service allows sftp connections only.Connection to 172.19.194.30 closed.

和预期一致:ssh尝试连接失败。

[root@localhost etc]# sftp test@172.19.194.30test@172.19.194.30's password: Connected to 172.19.194.30.sftp> lsa                a.log            authorized_keys  mysql.sh         sftp>

sftp测试连接成功!

原创粉丝点击