如果应用了另一条规则,则不要匹配 ssh 配置中的 Host *

如果应用了另一条规则,则不要匹配 ssh 配置中的 Host *

我有以下 SSH 配置:

Host work.github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/work
    IdentitiesOnly yes

Host *
    IdentityFile ~/.ssh/personal

我想通过将其主机设置为来使用单个存储库的工作 SSH 密钥ssh://[email protected]:...

但我认为正在发生的事情是,工作 SSH 密钥已应用,主机已更改,但随后重新解析配置Host github.comHost *现在匹配并重新设置 SSH 密钥为我的个人密钥

以下是摘录ssh -v [email protected]

OpenSSH_8.8p1, OpenSSL 3.0.8 7 Feb 2023
debug1: Reading configuration data /var/home/me/.ssh/config
debug1: /var/home/me/.ssh/config line 1: Applying options for *
// sets the correct SSH key
debug1: /var/home/me/.ssh/config line 4: Applying options for work.github.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Reading configuration data /etc/ssh/ssh_config.d/50-redhat.conf
debug1: Reading configuration data /etc/crypto-policies/back-ends/openssh.config
debug1: configuration requests final Match pass
debug1: re-parsing configuration
debug1: Reading configuration data /var/home/me/.ssh/config
// Re-pases the file with Host: github.com and re-sets the SSH key
debug1: /var/home/me/.ssh/config line 1: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Reading configuration data /etc/ssh/ssh_config.d/50-redhat.conf
debug1: Reading configuration data /etc/crypto-policies/back-ends/openssh.config
debug1: Connecting to github.com [140.82.121.4] port 22.
debug1: Connection established.
...
Hi <PERSONAL>! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Connection to github.com closed.
Transferred: sent 2340, received 2404 bytes, in 0.4 seconds
Bytes per second: sent 5866.3, received 6026.7
debug1: Exit status 1

我该如何处理这个案子?

答案1

您可以使用“!”符号规则。例如:

Host work.github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/work
    IdentitiesOnly yes

Host * !work.github.com
    IdentityFile ~/.ssh/personal

相关内容