记一次通过ssh密钥无法推送代码问题排查

今天,我突然发现在命令行推送代码没有仓库权限,仔细思考过后我懵了,我只是重启了一下电脑啊,并没有其他任何操作,怎么回事呢?

再经过简单排查后,成功解决了。可能使用MacOS的人会和我一样遇到这个问题,因此这里记录一下解决过程。

现象

jeyrcelu@JEYRCELU-MC1 newstore-svr % git push origin bugfix/global-search
no such identity: /Users/jeyrcelu/.ssh/id_*: No such file or directory
git@git.woa.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

背景

  • 前段时间apifox投毒事件,我重置了本地的密钥。
  • 昨天我给电脑做了升级,系统更新到 Tahoe 26.4.1

解决

  • 1、首先我怀疑公钥是不是被不小心移除了,去git页面看了一下,是正常的,并且1天前还是活跃的。
  • 2、本地进行调试 `ssh -vT git.woa.com`,发现自动扫描了一些密钥文件,但这些文件在我电脑上其实并不存在。
debug1: Local version string SSH-2.0-OpenSSH_10.2
debug1: Remote protocol version 2.0, remote software version Tencent Code
debug1: compat_banner: no match: Tencent Code
...
debug1: get_agent_identities: ssh_fetch_identitylist: agent contains no identities
debug1: Will attempt key: /Users/jeyrcelu/.ssh/id_rsa
debug1: Will attempt key: /Users/jeyrcelu/.ssh/id_ecdsa
debug1: Will attempt key: /Users/jeyrcelu/.ssh/id_ecdsa_sk
debug1: Will attempt key: /Users/jeyrcelu/.ssh/id_ed25519
debug1: Will attempt key: /Users/jeyrcelu/.ssh/id_ed25519_sk
debug1: Trying private key: /Users/jeyrcelu/.ssh/id_rsa
debug1: Trying private key: /Users/jeyrcelu/.ssh/id_ecdsa
debug1: Trying private key: /Users/jeyrcelu/.ssh/id_ecdsa_sk
debug1: Trying private key: /Users/jeyrcelu/.ssh/id_ed25519
debug1: Trying private key: /Users/jeyrcelu/.ssh/id_ed25519_sk
debug1: No more authentication methods to try.
jeyrcelu@git.woa.com: Permission denied (publickey).
  • 3、查看自己本地的密钥,发现ssh连接时默认使用的确实和实际不符。
jeyrcelu@JEYRCELU-MC1 ~ % ls -al .ssh
total 72
drwx------@ 12 jeyrcelu  staff   384  4月 17 15:01 .
drwxr-x---+ 68 jeyrcelu  staff  2176  4月 17 15:32 ..
drwx------@  3 jeyrcelu  staff    96  4月 17 14:24 agent
-rw-r--r--@  1 jeyrcelu  staff   430  4月 17 15:01 config
-rw-------@  1 jeyrcelu  staff   411  3月 26 18:05 id_aliyun
-rw-r--r--@  1 jeyrcelu  staff   103  3月 26 18:05 id_aliyun.pub
-rw-------@  1 jeyrcelu  staff   411  3月 26 17:40 id_github
-rw-r--r--@  1 jeyrcelu  staff   103  3月 26 17:40 id_github.pub
-rw-------@  1 jeyrcelu  staff   411  3月 26 17:40 id_gitwoa
-rw-r--r--@  1 jeyrcelu  staff   103  3月 26 17:40 id_gitwoa.pub
-rw-------@  1 jeyrcelu  staff  1312  3月 26 17:50 known_hosts
-rw-r--r--@  1 jeyrcelu  staff   576  3月 26 17:47 known_hosts.old
  • 4、使用 `ssh-add -l` 查看加载的密钥文件直接为空,果然佐证了3的结果。
  • 5、这个时候就明白了,为了提高安全性,我将密钥 `专钥专用`,为每个平台生成了各自的密钥对:
    • id_aliyun: 本机连接阿里云服务器用的
    • id_github: 在github提交代码用的
    • id_gitwoa: 内网提交代码用的

而ssh默认只会扫描几个常见的默认密钥文件(默认名称和算法生成的):

    • id_rsa
    • id_ecdsa
    • id_ed25519
  • 6、问题清楚就好了,直接在ssh配置中给我实际密钥自动加载就可以解决了:
jeyrcelu@JEYRCELU-MC1 ~ % vim .ssh/config

Host git.woa.com
StrictHostKeyChecking no
Port 22

Host *
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/id_aliyun
    IdentityFile ~/.ssh/id_github
    IdentityFile ~/.ssh/id_gitwoa
  • 7、重新测试 `ssh -vT git.woa.com` 成功,再次推送代码也成功了。

QA

  • `~/.ssh.config` 中加入的配置是在每次尝试建立ssh连接时生效,因此修改后无需重启sshd服务.