跳转到内容

SSH 推送

时光2025/8/290 0 m

建议

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

检查是否已经有 SSH 密钥对

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

bash
ls -al ~/.ssh

生成新的 SSH 密钥对

bash
ssh-keygen -t rsa -C "kxcare"

启动 SSH 代理

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

添加密钥到 SSH 代理

bash
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),并设置一个强密码来保护它。

::: warning[踩坑提示]

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

:::

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

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

bash
rm -f ~/.ssh/config

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

bash
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

设置正确的权限:

bash
chmod 600 ~/.ssh/config

验证

bash
kylin@kylin-pc:~$ ssh -T git@github.com
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.

VitePress Algolia Twikoo EdgeOne Copyright