强制使用 gpg-key 作为给定服务器的 ssh-key

Mik*_*cre 6 ssh gpg pgp ssh-agent

我将 ssh 配置为使用 GPG 作为我的 ssh 代理,如果我删除了该~/.ssh文件夹,我可以使用我的 gpg 密钥通过 ssh 进入我的服务器。但是,我的~/.ssh文件夹中有十多个不同的 ssh 密钥,如果我在它存在时尝试 ssh,我会收到权限被拒绝错误,因为我的 ssh 客户端在尝试密钥之前提供了目录中的每个私钥gpg SSH 代理。

使用常规 ssh 密钥,我只使用文件中的IdentityFile配置~/.ssh/config,但我不能这样做,因为我的身份是 gpg cardno。我很困惑为什么 ssh 更喜欢密钥文件而不是代理。有没有办法强制 ssh 使用代理而不是文件?或者更好的是,有没有办法在~/.ssh/config文件中指定gpg 密钥必须用于给定服务器?

我已经确认ssh-agent没有运行并且gpg-agent正在运行并ssh-add -L显示我的 gpg 密钥存在,以及一个其他 ssh 样式的私钥。

anx*_*anx 7

我不能这样做,因为我的身份是 gpg 卡号。

可以使用IdentityFileIdentitiesOnly,甚至可以使用 gnupg 提供的身份。

  • 如果您有该卡,请从您的代理处导出公钥:

    $ ssh-add -L | grep "cardno:.*789$" | tee ~/.ssh/smartcard.pub
    ssh-rsa AAAA[..]== cardno:023456000789
    
    Run Code Online (Sandbox Code Playgroud)
  • 如果不这样做,但请记住它与哪个键关联,请从 gnupg 导出:

    $ gpg2 --export-ssh-key ronnie.smart@card.example | tee ~/.ssh/smartcard.pub
    ssh-rsa AAAA[..]== openpgp:0xDEADBEEF
    
    Run Code Online (Sandbox Code Playgroud)

然后告诉 ssh 使用该导出来识别正确的密钥:

Host *.host.example
    IdentityFile ~/.ssh/smartcard.pub
    IdentitiesOnly yes
    PasswordAuthentication no
    PubkeyAuthentication yes
Run Code Online (Sandbox Code Playgroud)

当 gnupg 检测到正确的智能卡时,这将按照预期为您提供一次登录尝试:

$ ssh -v smart.host.example
[..]
debug1: Next authentication method: publickey
debug1: Offering public key: /home/home/.ssh/smartcard.pub RSA SHA256:a1337[..] explicit
Run Code Online (Sandbox Code Playgroud)

不幸的是,每当您忘记插入卡时,您都会得到相当无用的输出,因为gnupg ssh 代理不会像gpg 代理那样要求插入正确的卡。这很烦人,但不会影响您的实际使用。