Ssh

無法允許 SFTP 但不允許使用公鑰身份驗證的 SSH

  • May 14, 2020

我有一個在雲中執行的 Ubuntu Server 20.04 實例。我已將我的帳戶配置為使用公鑰身份驗證,並且在驗證一切正常後將禁用密碼身份驗證。我現在正在嘗試配置組sftponly中的帳戶能夠連接到 SFTP,但不能用於 shell 訪問。我已按照這篇文章中概述的步驟進行操作,但它似乎沒有按我預期的那樣工作。

我希望通過我的配置,我的使用者poe應該能夠登錄並與 SFTP 子系統互動,但他們應該是 CHRoot’d 到他們的主目錄,並且如果通過像 PuTTY 這樣的終端連接,則無法訪問 shell。設置poe的 shell 以/usr/sbin/nologin防止他們訪問 SSH 或 SFTP,因此他們的 shell 是/usr/bin/bash. poe的 ID 是:

root@server:/# id poe
uid=1000(poe) gid=1000(poe) groups=1000(poe),1003(sftponly)

我的etc/ssh/sshd_config文件應該是 20.04 的預設文件:

#       $OpenBSD: sshd_config,v 1.103 2018/04/09 20:41:22 tj Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

Include /etc/ssh/sshd_config.d/*.conf

#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none

# Logging
#SyslogFacility AUTH
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#RSAAuthentication yes

PubkeyAuthentication yes

# Expect .ssh/authorized_keys2 to be disregarded by default in future.
AuthorizedKeysFile      .ssh/authorized_keys

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

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

# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication yes
#PermitEmptyPasswords no

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

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

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no

# 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

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
PrintMotd no
#PrintLastLog yes
#TCPKeepAlive yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

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

# override default of no subsystems
Subsystem sftp  internal-sftp

# Example of overriding settings on a per-user basis
#Match User anoncvs
#       X11Forwarding no
#       AllowTcpForwarding no
#       PermitTTY no
#       ForceCommand cvs server

/etc/ssh/sshd_config.d/match-group-sftponly.conf的是:

Match Group sftponly
ChrootDirectory %h
X11Forwarding no
AllowTcpForwarding no
AllowAgentForwarding no
PermitTTY no
PermitTunnel no
ForceCommand internal-sftp

當執行 sshd 來測試更改時,這是它給我的輸出。IP 和密鑰雜湊已被編輯,埠 23 用於測試。

伺服器日誌在 pastebin 上,因為 ServerFault 認為它們是垃圾郵件:(

在日誌的第 150 行,它確實有趣地說:

debug3: checking match for 'Group sftponly' user poe host <office ip> addr <office ip> laddr <server ip> lport 23
debug1: user poe matched group list sftponly at line 1

但我的規則在match-group-sftponly.conf. 我確定我缺少的是一些簡單的東西,但我找不到它。

我已經設法解決了我的問題,將 match 命令放在主配置文件中,而不是包含的 conf 文件中。配置文件是否包含在主文件的內容之前或之後都沒有關係。

將其放在單獨的文件中不起作用,但將匹配組移動到sshd_config文件底部是可行的,現在它會應用組中的規則。

引用自:https://serverfault.com/questions/1016998