今天在 Linux 上使用公钥推送仓库到 GitHub 上时遇到了问题,完整记录一下如何在 Linux 下使用 ssh 推送仓库到 GitHub

安装 Git

1
apt update && apt install git

配置账户

1
2
3
git config  --global user.name "自行输入名称"

git config --global user.email "自行输入邮箱"

生成秘钥

  • 如果想使用其他平台已经配置好的私钥
1
mkdir  ~/.ssh && vim ~/.ssh/id_rsa

把之前已经在 GitHub 上配置好公钥,对应的私钥文件粘贴进去

修改权限

1
chmod 0600 ~/.ssh/id_rsa
  • 或者没有配置过,使用 rsa 加密生成秘钥对
1
ssh-keygen -t rsa -C "your_email@example.com"

将对应的公钥自行配置到 GitHub 中

修改权限是为了解决权限太大,ssh连接被关闭的问题

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for ‘/root/.ssh/id_rsa’ are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key “/root/.ssh/id_rsa”: bad permissions
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

使用 ssh 代理

1
eval "$(ssh-agent -s)"

将 ssh 私钥添加到 ssh-agent

1
ssh-add ~/.ssh/id_rsa

终端测试连接

这条命令无需改动

1
ssh -T git@github.com

出现如下提示就是连通了

Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.

添加远程仓库地址

1
git remote add origin <远程仓库地址>

这里注意远程仓库地址选择 SSH 链接的地址,即 git@ 开头的地址