验证 sshd 配置

验证 sshd 配置

我如何验证配置sshd

例如,我想确保设置并应用这些设置:

AllowUsers user1 user2 
PasswordAuthentication no
PermitRootLogin no

唯一的方法是手动验证文件的内容吗sshd_config,或者我可以探测sshd以确保吗?

答案1

有一个扩展测试模式,使用命令行选项 调用-T,可以执行此操作。例如:

% sudo sshd -T | egrep -i 'allowusers|passwordauth|permitroot'
permitrootlogin yes
passwordauthentication yes

该选项自 2008 年起就存在于 Portable OpenSSH 中,参见 提交 e7140f2。这是随 5.1p1 一起发布的,制作于 2008 年 7 月,参见。5.1 发行说明,因此它存在于目前支持的几乎所有 OpenSSH 服务器安装中。

答案2

sshd 的配置通常位于以下文件:/etc/ssh/sshd_config

要查询运行时配置,您可以使用扩展测试模式sshd -T,它还允许您测试客户端设置的匹配。

答案3

虽然这不会转储所有服务器定义,但您可以尝试使用详细调试标志连接到服务器:ssh -v user@server。这将为您提供大量信息,这些信息将反映 sshd 配置中启用的选项。

例如,使用以下代码查看此连接的输出-v开关(密钥签名、域名和 IP 地址故意伪装):

OpenSSH_6.0p1, OpenSSL 0.9.8w 23 Apr 2012
debug1: Reading configuration data /home/claudio/.ssh/config
debug1: /home/claudio/.ssh/config line 13: Applying options for serv01
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to somedomain.com [185.113.29.221] port 22.
debug1: Connection established.
debug1: identity file /home/claudio/.ssh/id_dsa type 2
debug1: identity file /home/claudio/.ssh/id_dsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.9
debug1: match: OpenSSH_5.9 pat OpenSSH_5*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.0
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ECDSA 3a:0d:b8:18:ca:67:4c:54:0f:c8:b2:1e:48:53:69:28
debug1: Host '[somedomain.com]:22' is known and matches the ECDSA host key.
debug1: Found key in /home/claudio/.ssh/known_hosts:7
Warning: Permanently added the ECDSA host key for IP address '[185.113.29.221]:22' to the list of known hosts.
debug1: ssh_ecdsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Offering DSA public key: /home/claudio/.ssh/id_dsa
debug1: Server accepts key: pkalg ssh-dss blen 433
debug1: Authentication succeeded (publickey).
Authenticated to somedomain.com ([185.113.29.221]:22).
debug1: channel 0: new [client-session]
debug1: Requesting [email protected]
debug1: Entering interactive session.

从中你可以看到允许的身份验证方法是:publickey、password、keyboard-interactive。你还可以看到此服务器不允许漫游,并且该用户克劳迪奥可以使用他的公钥进行连接。

您可以通过指定更多“v”字母来增加信息输出的级别,但您可能会获得比您想要的更多的低级信息。

答案4

我认为,如果您指的是 openssh 服务器,则没有已知的方法可以查询正在运行的 sshd 实例的配置。根据您想要执行的操作,您可以使用 -t 标志来测试配置文件,以确保它在重新启动服务器之前有效,这样您就不会被踢出,特别是如果您对服务器没有任何带外访问权限。

相关内容