bea*_*ane 5 ssh scp account-restrictions
我有一个远程服务器my.server.com
,我想允许另一个用户 ( stranger
) 将文件 scp 到盒子上。
stranger
用户stranger
根本无法登录我怎样才能做到这一点?
编辑:我已经删除了所有的交叉帖子。这个问题只存在于这里。请帮忙!
编辑 2:我最终接受了正常scp
转移。选择@Lambert 的答案,因为它是最完整的。感谢所有的帮助!
如果您想要所有设置所有您提到的限制性内容,我建议使用 ProFTPd。
使用sftp_module
您只能允许安全会话。有关该功能的详细信息,请参阅http://www.proftpd.org/docs/contrib/mod_sftp.htmlsftp
。页面底部附近列出了示例配置。
使用该DefaultRoot
指令,您可以将授予的用户隔离到他/她自己的目录中
使用该<LIMIT>
结构,您可以限制您想要允许的 FTP 命令,READ
即用户无法检索文件。有关详细信息,请参阅http://www.proftpd.org/docs/howto/Limit.html。
当您在 ProFTPd 中设置sftp
配置时,您可能希望让它侦听除ssh
2222 之外的另一个端口。配置您的防火墙和/或路由器以允许来自stranger
您为 ProFTPd 选择的端口的流量。另一种可能性是在端口 22 上运行 ProFTPd 的sftp
模块并重新配置ssh
为侦听另一个端口。
示例配置如下所示:
<IfModule mod_sftp.c>
<VirtualHost a.b.c.d>
# The SFTP configuration
Port 2222
SFTPEngine on
SFTPLog /var/log/proftpd_sftp.log
# Configure the RSA, DSA, and ECDSA host keys, using the same host key
# files that OpenSSH uses.
SFTPHostKey /etc/ssh_host_rsa_key
SFTPHostKey /etc/ssh_host_dsa_key
SFTPHostKey /etc/ssh_host_ecdsa_key
<Limit READ>
DenyAll
</Limit>
DefaultRoot ~ users,!staff
</VirtualHost>
</IfModule>
Run Code Online (Sandbox Code Playgroud)
注意:这不是 ProFTPd 的完整配置,您应该查看并修改 ProFTPd 默认配置以使其适合您的需要。
还有另一种可能,只需使用 OpenSSH 即可:
创建用户stranger
并为用户设置密码:
useradd -m -d /home/stranger -s /bin/true stranger
passwd stranger
Run Code Online (Sandbox Code Playgroud)
编辑文件/etc/ssh/sshd_config
并检查以下行是否存在,如果不存在则添加:
Subsystem sftp internal-sftp
Run Code Online (Sandbox Code Playgroud)
接下来在 的底部添加一个 Match 块/etc/ssh/sshd_config
:
Match User stranger
ChrootDirectory %h
ForceCommand internal-sftp -P read,remove
AllowTcpForwarding no
Run Code Online (Sandbox Code Playgroud)
注意:用户将能够覆盖现有文件。
重新启动sshd
守护进程。
将目录的所有者设置/home/stranger
为root
:
chown root:stranger /home/stranger
Run Code Online (Sandbox Code Playgroud)
注意:root
必须是所有者,并且如果使用 ChrootDirectory,则可能是唯一拥有写入权限的人。另一种方法可能是添加-d %u
到ForceCommand internal-sftp
行并设置ChrootDirectory /home
,但用户将能够使用cd /
以下命令查看其他用户名ls
为用户创建上传目录:
mkdir /home/stranger/upload; chown stranger:stranger /home/stranger/upload
Run Code Online (Sandbox Code Playgroud)
现在您可以使用以下命令以用户身份登录stranger
:
sftp stranger@somehost
Run Code Online (Sandbox Code Playgroud)
当你上传文件时应该没问题:
sftp> put myfile
Uploading myfile to /upload/myfile
myfile 100% 17 0.0KB/s 00:00
sftp> get myfile
Fetching /upload/myfile to myfile
/upload/myfile 0% 0 0.0KB/s --:-- ETA
Couldn't read from remote file "/upload/myfile" : Permission denied
Run Code Online (Sandbox Code Playgroud)