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

前端之家收集整理的这篇文章主要介绍了linux – SFTP与chroot取决于连接用户的公钥前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想构建一个服务器(运行Debian或FreeBSD),通过sshfs从不同的客户端接收备份.每个客户端应该能够读取和写入自己的备份数据,但不能读取和写入任何其他客户端的数据.

我有以下想法:每个客户端通过公钥auth连接到backup@backupserver.local.用户备份有一个特殊的authorized_keys文件,如下所示:@H_403_3@

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文件.@H_403_3@

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

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

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

解决方法

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

是的,你可以使用proftpd@H_403_3@

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

# useradd -m -d /vhosts/backup/user1/ -s /sbin/nologin user1
# passwd --lock user1
Locking 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工具执行此操作:@H_403_3@

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

设置ProFTPD@H_403_3@

ServerName "ProFTPD Default Installation"
ServerType standalone
DefaultServer off

LoadModule mod_tls.c
LoadModule mod_sftp.c
LoadModule mod_rewrite.c

TLSProtocol TLSv1 TLSv1.1 TLSv1.2

# Disable default ftp server
Port 0

UseReverseDNS off
IdentLookups 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 off

MaxInstances 30

# Set the user and group under which the server will run.
User nobody
Group nobody

# Normally,we want files to be overwriteable.
AllowOverwrite                  on

TimesGMT off
SetEnv 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)组参数.@H_403_3@

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

配置任何SFTP客户端.我用过FileZilla@H_403_3@

@H_403_3@

如果在调试模式下运行ProFPTD@H_403_3@

# proftpd -n -d 3

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

2016-02-21 22:12:48,275 sftp.example.net proftpd[50511]: using PCRE 7.8 2008-09-05
2016-02-21 22:12:48,279 sftp.example.net proftpd[50511]: mod_sftp/0.9.9: using OpenSSL 1.0.1e-fips 11 Feb 2013
2016-02-21 22:12:48,462 sftp.example.net proftpd[50511] sftp.example.net: set core resource limits for daemon
2016-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 STARTUP
2016-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 capabilities
2016-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()ed
2016-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中的以下行@H_403_3@

2016-02-21 22:12:48,735 mod_sftp/0.9.9[50309]: sending acceptable userauth methods: publickey
2016-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:6c
2016-02-21 22:12:48,735 mod_sftp/0.9.9[50309]: sending publickey OK
2016-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:6c
2016-02-21 22:12:59,790 mod_sftp/0.9.9[50309]: sending userauth success
2016-02-21 22:12:59,790 mod_sftp/0.9.9[50309]: user 'user1' authenticated via 'publickey' method

附:@H_403_3@

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

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.@H_403_3@

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

<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>

并创建一个虚拟帐户@H_403_3@

# 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@H_403_3@

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

猜你在找的Linux相关文章