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

我想构建一个服务器(运行Debian或FreeBSD),通过sshfs从不同的客户端接收备份.每个客户端应该能够读取和写入自己的备份数据,但不能读取和写入任何其他客户端的数据.

我有以下想法:每个客户端通过公钥auth连接到backup@backupserver.local.用户备份有一个特殊的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 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工具执行此操作:

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

设置ProFTPD

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)组参数.

# 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-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中的以下行

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

附:

包含授权密钥(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常用进程通信方式包括管道(pipe)、有名管道(FIFO)、...
Linux性能观测工具按类别可分为系统级别和进程级别,系统级别...
本文详细介绍了curl命令基础和高级用法,包括跳过https的证书...
本文包含作者工作中常用到的一些命令,用于诊断网络、磁盘占满...
linux的平均负载表示运行态和就绪态及不可中断状态(正在io)的...
CPU上下文频繁切换会导致系统性能下降,切换分为进程切换、线...