尝试将 SFTP 用户 ChrootDirectory 到他们的主目录

尝试将 SFTP 用户 ChrootDirectory 到他们的主目录

我按照几个例子来做这件事,最后都修改sshd_config成了

Subsystem sftp internal-sftp

Match User chubbyninja
    ChrootDirectory %h
    AllowTCPForwarding no
    X11Forwarding no
    ForceCommand /usr/lib/openssh/sftp-server

当我这样做时,我sshd -t会确保没有错误service sshd restart

重启后,我尝试使用 SFTP(使用 filezilla),但一直

Response:   fzSftp started
Command:    open "[email protected]" 22
Command:    Pass: ********************
Error:  Network error: Software caused connection abort
Error:  Could not connect to server

如果我将配置恢复到原始状态,我可以正常使用 SFTP,但随后我可以浏览任何目录。我只需要用户在他们的主目录中

我的默认配置中有这一行:

Subsystem sftp /usr/lib/openssh/sftp-server

这就是我替换具有上述详细信息。

尽管我有 root 访问权限,但我只能通过 ssh 访问这台机器。

更新 按照 sam_pan_mariusz 的建议后,似乎有了进一步的进展,但现在我得到了

Response:   fzSftp started
Command:    open "[email protected]" 22
Error:  Network error: Connection refused
Error:  Could not connect to server

更新2

我也听从了 Froggiz 的建议,将我的配置更改为如下:

Subsystem sftp internal-sftp -u 0007 -f AUTH -l VERBOSE 
Match Group chubbyninja
     ChrootDirectory /home/chubbyninja
     ForceCommand internal-sftp -u 0007
     AllowTcpForwarding no
     GatewayPorts no
     X11Forwarding no

但我得到了原版软件导致连接中止

我进行了监控/var/syslog,但没有显示任何内容来表明为什么会出现此错误

更新 3-添加了 sshd_config

# Package generated configuration file
# See the sshd_config(5) manpage for details

# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes

# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 1024

# Logging
SyslogFacility AUTH
LogLevel INFO

# Authentication:
LoginGraceTime 120
#PermitRootLogin without-password
PermitRootLogin yes
StrictModes yes

RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile %h/.ssh/authorized_keys

# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes

# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Change to no to disable tunnelled clear text passwords
PasswordAuthentication yes

# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no

#MaxStartups 10:30:60
#Banner /etc/issue.net

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

Subsystem sftp internal-sftp -u 0007 -f AUTH -l VERBOSE

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes



Match Group chubbyninja
        ChrootDirectory /home/chubbyninja
        AllowTCPForwarding no
        X11Forwarding no
        GatewayPorts no
        ForceCommand internal-sftp -u 0007

答案1

这是我为特定用户设置 sftp 的方式

1]创建用户

 adduser {USER}

2]编辑/etc/ssh/sshd_config

 PasswordAuthentication yes 

 Subsystem sftp
 internal-sftp -u 0007 -f AUTH -l VERBOSE 
 Match Group {USER}
     ChrootDirectory {FOLDER}
     ForceCommand internal-sftp -u 0007
     AllowTcpForwarding no
     GatewayPorts no
     X11Forwarding no

3]设置用户权限

 chmod -R 777 {FOLDER}

4]重启ssh

service ssh restart

将 {USER} 替换为您的用户,将 {FOLDER} 替换为您的文件夹,它应该可以工作!;)

您可以放出您的完整 ssh 配置吗?

答案2

我不太明白问题中的“替换”部分,但是......在 SSH 中进行 chroot 时,部队指挥应该引用内部子系统名称。对于您的情况,整行应为:ForceCommand sftp

如果你想使用可执行路径根目录,您必须准备适当的 chroot 目录(包含可执行文件链接到的库、配置等)。

我的工作配置中的相关行:

Subsystem sftp /usr/lib/openssh/sftp-server

Match User testuser1
    ChrootDirectory /one/dir/path/
    ForceCommand internal-sftp

相关内容