跳到主要内容

SSH推送

提示

要避免每次使用 git fetch/push 等操作时都输入密码,可以通过设置 SSH 密钥来实现。

检查是否已经有 SSH 密钥对

查看是否存在 id_rsaid_rsa.pub 文件,如果已经存在,则跳至步骤3。

ls -al ~/.ssh

生成新的 SSH 密钥对

ssh-keygen -t rsa -C "kxcare"

启动SSH代理

eval "$(ssh-agent -s)"

添加密钥到 SSH 代理

ssh-add ~/.ssh/id_rsa

将公钥添加到 Git 托管服务提供商(如 GitHub、GitLab)

  1. 打开 ~/.ssh/id_rsa.pub 文件,复制其中的内容。

  2. 登录到 Git 托管服务提供商账户,进入设置页面,并添加新的 SSH 公钥。将之前复制的公钥粘贴到相应位置并保存。

提示

现在,在使用 Git 进行操作时,选择使用 SSH URL 而不是 HTTPS URL 来克隆或访问远程仓库即可。这样配置后,你将能够使用 SSH 密钥来进行 Git 操作,而无需每次输入密码。请确保妥善保存你的私钥(~/.ssh/id_rsa),并设置一个强密码来保护它。

踩坑提示

当使用代理时,默认的http传输会报错kex_exchange_identification: Connection closed by remote hostfatal: 无法读取远程仓库。

创建标准 SSH 配置(解决当前问题)

删除现有配置文件(如果有):

rm -f ~/.ssh/config

重新创建配置文件(注意格式):

cat > ~/.ssh/config <<'EOF'
Host github.com
Hostname ssh.github.com
User git
Port 443
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
TCPKeepAlive yes
IdentitiesOnly yes
StrictHostKeyChecking accept-new
ServerAliveInterval 60
EOF

设置正确的权限:

chmod 600 ~/.ssh/config

验证

kylin@kylin-pc:~$ ssh -T [email protected]
Enter passphrase for key '/home/kylin/.ssh/id_rsa':
Enter passphrase for key '/home/kylin/.ssh/id_rsa':
Hi kxcare! You've successfully authenticated, but GitHub does not provide shell access.