git避免每次都需要添加ssh key

在 Ubuntu 系统中,使用git时出现’[email protected]: Permission denied (publickey)’,这种情况有可能因为:

  1. 生成了 SSH 密钥,若未将其添加到 SSH 代理,Git 在尝试连接 GitHub 时也无法使用该密钥进行认证。 解决方法:
#启动 SSH 代理:
eval "$(ssh-agent -s)"
#将 SSH 密钥添加到 SSH 代理中。
ssh-add ~/.ssh/id_rsa

开机启动,在~/.bashrc 文件的末尾添加以下内容:

gedit ~/.bashrc 

# 启动 SSH 代理
eval "$(ssh-agent -s)"
# 将 SSH 密钥添加到 SSH 代理中
ssh-add ~/.ssh/id_rsa

#保存并关闭文件,然后运行
source ~/.bashrc  # 或者 source ~/.zshrc
  1. SSH 配置文件有误:~/.ssh/config 文件中的配置可能存在错误,影响了 SSH 连接的正常进行。

解决方法: vim ~/.ssh/config


Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa.pub

通过ssh -T [email protected]测试是否成功。