无法允许 SFTP 但禁止使用公钥身份验证的 SSH

无法允许 SFTP 但禁止使用公钥身份验证的 SSH

我在云中运行了一个 Ubuntu Server 20.04 实例。我已将我的帐户配置为使用公钥身份验证,一旦所有验证工作正常,我将禁用密码身份验证。我现在正在尝试配置组中的帐户,sftponly使其能够连接 SFTP,但不能连接 shell 访问。我已按照帖子,但它似乎没有像我预期的那样工作。

我希望我的配置poe能让我的用户 能够登录并与 SFTP 子系统交互,但他们应该被 CHRoot 到他们的主目录,并且如果通过 PuTTY 等终端连接则无法访问 shell。将poe的 shell 设置为 可/usr/sbin/nologin阻止他们访问 SSH 或 SFTP,因此他们的 shell 是/usr/bin/bashpoe的 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。我确信我缺少的是一些简单的东西,但我找不到它。

答案1

我已经设法解决了这个问题,将 match 命令放在主配置文件中,而不是包含的 conf 文件中。配置文件是否包含在主文件内容之前或之后都没有关系。

将其放在单独的文件中不起作用,但将匹配组移动到文件底部却sshd_config起作用,并且现在它会应用组中的规则。

相关内容