[最佳实践]配置sshd只允许sftp登录

1,228次阅读
没有评论

sftp 是 Secure File Transfer Protocol 的缩写,即安全文件传送协议,可为传输文件提供一种安全的加密方法。
sftp 为 SSH 的一部分,由于这种传输方式使用了加密/解密技术,所以传输效率比普通的 FTP 要低,如果对网络安全性要求高,可使用 SFTP 代替 FTP。
因为安全需要,有时需要配置sshd以使得只允许sftp登录,而同时不允许ssh直接登录。
以下操作步骤在CentOS 7.6实际测试通过,可放心参考使用。

■ 添加用户组sftpgroup

groupadd sftpgroup

■ 添加用户sftpuser

useradd -g sftpgroup -d /home/sftp -s /sbin/nologin sftpuser
-s 指定不允许ssh登陆

■ 修改用户密码

passwd sftpuser

■ 配置sshd_config文件

vim /etc/ssh/sshd_config
增加或修改如下配置:

Subsystem sftp internal-sftp
Match Group sftpgroup
        ChrootDirectory /home/sftp
        X11Forwarding no
        AllowTcpForwarding no
        ForceCommand internal-sftp

Match Group sftpgroup 指定以下的子行配置是匹配 sftpgroup 用户组的。Match user userA,userB 则是匹配用户
ChrootDirectory /data/sftp/%u 设定用户访问的根文件夹。%h 代表用户 home 目录,%u 代表用户名
ForceCommand internal-sftp 该行强制执行内部 sftp,并忽略任何 ~/.ssh/rc 文件中的命令
AllowTcpForwarding no 是否允许 TCP 转发,默认值为 “yes”
X11Forwarding no 是否允许进行 X11 转发,默认值是 “no”

■ 设置正确的目录权限

要实现 Chroot 功能,目录权限设置非常重要,否则无法登录,给出的错误提示也很奇怪,难以排查、修正,导致踩坑半天,浪费很多时间!
以下为最佳设置实践,务必以此为准!

chown root:sftpgroup /home/sftp
chmod 755 /home/sftp

可以看到,ChrootDirectory的目录属主必须为root,属组必须为sftpgroup,其他用户或用户组为”只读+执行”

■ 重启sshd服务

systemctl restart sshd

■ 此时可以sftp登录

[root@node1:1 /home]# sftp sftpuser@node1
sftpuser@node1's password: 
Connected to node1.
sftp> ls
sftp> 
sftp> 
sftp> pwd
Remote working directory: /

此时可以在sftp的根目录下建立指定用户的读写目录,即可正常使用sftp了,以下操作可参考。

sftp> mkdir test
Couldn't create directory: Permission denied
sftp> exit
[root@node1:1 /home]# cd /home/sftp
[root@node1:1 /home/sftp]# mkdir upload
[root@node1:1 /home/sftp]# chown -R sftpuser:sftpgroup upload
[root@node1:1 /home/sftp]# chmod -R 700 upload
[root@node1:1 /home/sftp]# ll
总用量 0
drwx------ 2 sftpuser sftpgroup 6 12月 22 10:55 upload
[root@node1:1 /home/sftp]# sftp sftpuser@node1               
sftpuser@node1's password: 
Connected to node1.
sftp> pwd
Remote working directory: /
sftp> cd upload
sftp> mkdir test
sftp> ls
test  

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

文心AIGC

2023 年 6 月
 1234
567891011
12131415161718
19202122232425
2627282930  
文心AIGC
文心AIGC
人工智能ChatGPT,AIGC指利用人工智能技术来生成内容,其中包括文字、语音、代码、图像、视频、机器人动作等等。被认为是继PGC、UGC之后的新型内容创作方式。AIGC作为元宇宙的新方向,近几年迭代速度呈现指数级爆发,谷歌、Meta、百度等平台型巨头持续布局
文章搜索
热门文章
清库存!DeepSeek突然补全R1技术报告,训练路径首次详细公开

清库存!DeepSeek突然补全R1技术报告,训练路径首次详细公开

清库存!DeepSeek突然补全R1技术报告,训练路径首次详细公开 Jay 2026-01-08 20:18:...
训具身模型遇到的很多问题,在数据采集时就已经注定了丨鹿明联席CTO丁琰分享

训具身模型遇到的很多问题,在数据采集时就已经注定了丨鹿明联席CTO丁琰分享

训具身模型遇到的很多问题,在数据采集时就已经注定了丨鹿明联席CTO丁琰分享 衡宇 2026-01-08 20:...
「北京版幻方」冷不丁开源SOTA代码大模型!一张3090就能跑,40B参数掀翻Opus-4.5和GPT-5.2

「北京版幻方」冷不丁开源SOTA代码大模型!一张3090就能跑,40B参数掀翻Opus-4.5和GPT-5.2

「北京版幻方」冷不丁开源SOTA代码大模型!一张3090就能跑,40B参数掀翻Opus-4.5和GPT-5.2...
开源“裸考”真实世界,国产具身智能基座模型拿下全球第二!

开源“裸考”真实世界,国产具身智能基座模型拿下全球第二!

开源“裸考”真实世界,国产具身智能基座模型拿下全球第二! 西风 2026-01-08 19:02:20 来源:...
最新评论
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.
热评文章
OpenAI推理第一人离职,7年打造了o3/o1/GPT-4/Codex

OpenAI推理第一人离职,7年打造了o3/o1/GPT-4/Codex

OpenAI推理第一人离职,7年打造了o3/o1/GPT-4/Codex 衡宇 2026-01-06 13:0...
杜比在CES 2026重塑了观影、娱乐的方式

杜比在CES 2026重塑了观影、娱乐的方式

杜比在CES 2026重塑了观影、娱乐的方式 十三 2026-01-07 12:47:06 来源:量子位 树立...
全自主、更好用!北京人形 “干活机器人” 惊艳亮相 CES2026

全自主、更好用!北京人形 “干活机器人” 惊艳亮相 CES2026

全自主、更好用!北京人形 “干活机器人” 惊艳亮相 CES2026 量子位的朋友们 2026-01-06 16...
港科大教授实测AI眼镜“作弊”:30分钟碾压95%的学生,把传统教学评估体系整破防了

港科大教授实测AI眼镜“作弊”:30分钟碾压95%的学生,把传统教学评估体系整破防了

港科大教授实测AI眼镜“作弊”:30分钟碾压95%的学生,把传统教学评估体系整破防了 梦瑶 2026-01-0...