linux – SFTP与chroot取决于连接用户的公钥

linux – SFTP与chroot取决于连接用户的公钥,第1张

概述我想构建一个服务器(运行Debian或FreeBSD),通过sshfs从不同的客户端接收备份.每个客户端应该能够读取和写入自己的备份数据,但不能读取和写入任何其他客户端的数据. 我有以下想法:每个客户端通过公钥auth连接[email protected].用户备份有一个特殊的authorized_keys文件,如下所示: command="internal-sftp" chro 我想构建一个服务器(运行Debian或FreeBSD),通过sshfs从不同的客户端接收备份.每个客户端应该能够读取和写入自己的备份数据,但不能读取和写入任何其他客户端的数据.

我有以下想法:每个客户端通过公钥auth连接到[email protected].用户备份有一个特殊的authorized_keys文件,如下所示:

command="internal-sftp" chroot="/backup/clIEnt-1/data" ssh-rsa (key1)command="internal-sftp" chroot="/backup/clIEnt-2/data" ssh-rsa (key2)command="internal-sftp" chroot="/backup/clIEnt-3/data" ssh-rsa (key3)etc...

这样做的好处是我不需要为每个客户端使用单独的用户,并且我可以使用脚本轻松地自动生成authorized_keys文件.

只有一个问题:chroot = …不起作用. OpenSSH的authorized_keys文件似乎没有ChrootDirectory的等效文件(在/ etc / ssh / sshd_config中可以在全局或匹配用户块中使用).

有没有一种相当简单的方法来实现我想要的OpenSSH?也许以聪明的方式使用command = …指令?
或者,是否有其他SFTP服务器可以做我想要的?

编辑:为了更清楚地说明我想要实现的目标:我希望几个客户端能够在我的服务器上存储文件.每个客户端都不应该能够看到任何其他客户端的文件.而且我不想用几十个用户帐户丢弃我的服务器,所以我想要一个易于管理的解决方案,让客户共享一个用户帐户,但仍然无法访问彼此的文件.

解决方法

Alternatively,are there other SFTP servers that can do what I want?

是的,你可以使用proftpd

准备用户环境.使用ProFTPD,无需向用户提供有效的shell.

# useradd -m -d /vhosts/backup/user1/ -s /sbin/nologin user1# passwd --lock user1Locking password for user user1.passwd: Success# mkdir /vhosts/backup/user1/.sftp/# touch /vhosts/backup/user1/.sftp/authorized_keys# chown -R user1:user1 /vhosts/backup/user1/# chmod -R 700 /vhosts/backup/user1/

要在SFTPAuthorizedUserKeys中使用OpenSSH公钥,必须将它们转换为RFC4716格式.您可以使用ssh-keygen工具执行此 *** 作:

# ssh-keygen -e -f user1.public.key > /vhosts/backup/user1/.sftp/authorized_keys

设置ProFTPD

Servername "ProFTPD Default Installation"ServerType standaloneDefaultServer offLoadModule mod_tls.cLoadModule mod_sftp.cLoadModule mod_rewrite.cTLSProtocol TLSv1 TLSv1.1 TLSv1.2# disable default ftp serverPort 0UseReversednS offIDentLookups off# Umask 022 is a good standard umask to prevent new dirs and files# from being group and world writable.Umask 022# PersistentPasswd causes problems with NIS/LDAP.PersistentPasswd offMaxInstances 30# Set the user and group under which the server will run.User nobodyGroup nobody# normally,we want files to be overwriteable.AllowOverwrite                  onTimesGMT offSetEnv TZ :/etc/localtime<VirtualHost sftp.example.net>    Servername "SFTP: Backup server."    DefaultRoot ~    Umask 002    Port 2121    RootRevoke on    SFTPEngine on    SFTPLog /var/log/proftpd/sftp.log    SFTPHostKey /etc/ssh/ssh_host_rsa_key    SFTPHostKey /etc/ssh/ssh_host_dsa_key    SFTPDHParamfile /etc/pki/proftpd/dhparam_2048.pem    SFTPAuthorizedUserKeys file:~/.sftp/authorized_keys    SFTPCompression delayed    SFTPAuthMethods publickey</VirtualHost><Global>    RequireValIDShell off    AllowOverwrite yes    DenyFilter \*.*/    <limit SITE_CHMOD>        DenyAll    </limit></Global>LogFormat default "%h %l %u %t \"%r\" %s %b"LogFormat auth    "%v [%P] %h %t \"%r\" %s"ExtendedLog /var/log/proftpd/access.log read,write

创建DH(DiffIE-Hellman)组参数.

# openssl dhparam -out /etc/pki/proftpd/dhparam_2048.pem 2048

配置任何SFTP客户端.我用过fileZilla

如果在调试模式下运行ProFPTD

# proftpd -n -d 3

在控制台中,您将看到类似以下内容的内容

2016-02-21 22:12:48,275 sftp.example.net proftpd[50511]: using PCRE 7.8 2008-09-052016-02-21 22:12:48,279 sftp.example.net proftpd[50511]: mod_sftp/0.9.9: using OpenSSL 1.0.1e-fips 11 Feb 20132016-02-21 22:12:48,462 sftp.example.net proftpd[50511] sftp.example.net: set core resource limits for daemon2016-02-21 22:12:48,462 sftp.example.net proftpd[50511] sftp.example.net: ProFTPD 1.3.5a (maint) (built Sun Feb 21 2016 21:22:00 UTC) standalone mode STARTUP2016-02-21 22:12:59,780 sftp.example.net proftpd[50512] sftp.example.net (192.168.1.2[192.168.1.2]): mod_cap/1.1: adding CAP_SETUID and CAP_SETGID capabilitIEs2016-02-21 22:12:59,780 sftp.example.net proftpd[50512] sftp.example.net (192.168.1.2[192.168.1.2]): SSH2 session opened.2016-02-21 22:12:59,863 sftp.example.net proftpd[50512] sftp.example.net (192.168.1.2[192.168.1.2]): Preparing to chroot to directory '/vhosts/backup/user1'2016-02-21 22:12:59,863 sftp.example.net proftpd[50512] sftp.example.net (192.168.1.2[192.168.1.2]): Environment successfully chroot()ed2016-02-21 22:12:59,863 sftp.example.net proftpd[50512] sftp.example.net (192.168.1.2[192.168.1.2]): USER user1: Login successful

以及/var/log/sftp.log中的以下行

2016-02-21 22:12:48,735 mod_sftp/0.9.9[50309]: sending acceptable userauth methods: publickey2016-02-21 22:12:48,735 mod_sftp/0.9.9[50309]: public key MD5 fingerprint: c2:2f:a3:93:59:5d:e4:38:99:4b:fd:b1:6e:fc:54:6c2016-02-21 22:12:48,735 mod_sftp/0.9.9[50309]: sending publickey OK2016-02-21 22:12:59,789 mod_sftp/0.9.9[50309]: public key MD5 fingerprint: c2:2f:a3:93:59:5d:e4:38:99:4b:fd:b1:6e:fc:54:6c2016-02-21 22:12:59,790 mod_sftp/0.9.9[50309]: sending userauth success2016-02-21 22:12:59,790 mod_sftp/0.9.9[50309]: user 'user1' authenticated via 'publickey' method

附:

包含授权密钥(SFTPAuthorizedUserKeys)的文件的已配置路径可以使用%u变量,该变量将使用要进行身份验证的用户的名称进行插值.此功能支持使用位于中央位置的授权密钥的每用户文件,而不是要求(或允许)用户管理自己的授权密钥.例如:

SFTPAuthorizedUserKeys file:/etc/sftp/authorized_keys/%u

I want several clIEnts to be able to store files on my server. Each clIEnt should not be able to see any other clIEnt’s files. And I do not want to litter my server with doZens of user accounts,so I’d like an easily manageable solution for the clIEnts to share a user account and still have no access to eachother’s files.

使用ProFTPD也是可能的.你只需要稍微修改我的初始配置

<VirtualHost sftp.example.net>    ...       SFTPAuthorizedUserKeys file:/etc/proftpd/sftp_authorized_keys    AuthUserfile /etc/proftpd/sftp_users.passwd    CreateHome on 0700 dirmode 0700 uID 99 gID 99    RewriteHome on    RewriteEngine on    RewriteLog /var/log/proftpd/rewrite.log    RewriteCondition %m REWRITE_HOME    RewriteRule (.*) /vhosts/backup/%u</VirtualHost>

并创建一个虚拟帐户

# ftpasswd --passwd --file /etc/proftpd/sftp_users.passwd --sha512 --gID 99 --uID 99 --shell /sbin/nologin --name user1 --home /vhosts/backup

就这样.对于每个额外的帐户,您只需将其公钥添加到/ etc / proftpd / sftp_authorized_keys

注意:文件最后必须包含新行!这一点很重要.

总结

以上是内存溢出为你收集整理的linux – SFTP与chroot取决于连接用户的公钥全部内容,希望文章能够帮你解决linux – SFTP与chroot取决于连接用户的公钥所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/1041281.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存