教你如何配置linux用户实现禁止ssh登陆机器但可用sftp登录

1,245次阅读
没有评论

构想和目标
最近有个这样的诉求:基于对线上服务器的保密和安全,不希望开发人员直接登录线上服务器,因为登录服务器的权限太多难以管控,如直接修改代码、系统配置,并且也直接连上mysql。因此希望能限制开发人员ssh登录机器,但是通过ftp/sftp上传代码文件。
在网上找个各种各样的方法,经过试验做个汇总:

方法一
https://segmentfault.com/q/1010000000722462

这篇帖子的方法是搜索到的最通用的方法,方法是否可能呢,直接做个测试。

创建禁止登陆的用户:

useradd test -M -s /sbin/nologin
试试ssh登陆:登陆失败

[root@localhost app]# ssh test@172.19.194.30
test@172.19.194.30’s password:
Last login: Tue Oct 11 15:28:07 2016 from 172.18.135.185
This account is currently not available.
Connection to 172.19.194.30 closed.
试试sftp登陆:同样也提示登陆失败!!!

[root@localhost app]# sftp test@172.19.194.30
test@172.19.194.30’s password:
Received message too long 1416128883
[root@localhost app]#
可以看到,方法一,虽然限制了ssh登陆,但是同时也限制了sftp的连接,结论是此方法行不通。

方法二
http://jin771998569.blog.51cto.com/2147853/1067247

首先修改sshd的配置文件:

vim /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测试连接成功!

总结
这个东西虽然没有太大的技术含量,但是通过网上查找的大多是雷同且行不通的,又或者是不完整的,过程中耽误和浪费了不少时间,希望写这篇博客做个验证和汇总的作用。

参考:
如何关闭linux用户的SSH权限,但还能登录vsftpd
Linux openSSH 只能够使用SFTP 不能使用ssh登陆
sftp服务限制用户登录家目录
Linux_ftp_命令行下下载文件get与上传文件put的命令应用

正文完
可以使用微信扫码关注公众号(ID:xzluomor)
post-qrcode
 0
评论(没有评论)

文心AIGC

2023 年 6 月
 1234
567891011
12131415161718
19202122232425
2627282930  
文心AIGC
文心AIGC
人工智能ChatGPT,AIGC指利用人工智能技术来生成内容,其中包括文字、语音、代码、图像、视频、机器人动作等等。被认为是继PGC、UGC之后的新型内容创作方式。AIGC作为元宇宙的新方向,近几年迭代速度呈现指数级爆发,谷歌、Meta、百度等平台型巨头持续布局
文章搜索
热门文章
潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026 Jay 2025-12-22 09...
面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25

面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25

面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25 鹭羽 2025-12-13 22:37...
商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1

商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1

商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1 十三 2025-12-15 14:13:14 ...
跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026 一水 2025-1...
10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了

10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了

10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了 一水 2025-12-12 13:56:19 ...
最新评论
ufabet ufabet มีเกมให้เลือกเล่นมากมาย: เกมเดิมพันหลากหลาย ครบทุกค่ายดัง
tornado crypto mixer tornado crypto mixer Discover the power of privacy with TornadoCash! Learn how this decentralized mixer ensures your transactions remain confidential.
ดูบอลสด ดูบอลสด Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
Obrazy Sztuka Nowoczesna Obrazy Sztuka Nowoczesna Thank you for this wonderful contribution to the topic. Your ability to explain complex ideas simply is admirable.
ufabet ufabet Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
ufabet ufabet You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!
ufabet ufabet Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
热评文章
跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026 一水 2025-1...
10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了

10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了

10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了 一水 2025-12-12 13:56:19 ...
IDC MarketScape: 容联云位居“中国AI赋能的联络中心”领导者类别

IDC MarketScape: 容联云位居“中国AI赋能的联络中心”领导者类别

IDC MarketScape: 容联云位居“中国AI赋能的联络中心”领导者类别 量子位的朋友们 2025-1...