嘗試將 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

相關內容