插件窝 干货文章 gitee配置ssh公钥免密登录

gitee配置ssh公钥免密登录

公钥 SSH Gitee 密钥    来源:    2025-05-11

Gitee 配置 SSH 公钥免密登录指南

1. 检查现有 SSH 密钥

首先检查你的电脑是否已有 SSH 密钥:

ls -al ~/.ssh

如果看到 id_rsa 和 id_rsa.pub 文件,说明已有密钥,可跳过生成步骤。

2. 生成新的 SSH 密钥(如需要)

ssh-keygen -t rsa -C "your_email@example.com"
  • 按提示操作,建议直接回车使用默认路径和空密码
  • 这将生成 id_rsa(私钥)和 id_rsa.pub(公钥)文件

3. 查看并复制公钥

cat ~/.ssh/id_rsa.pub

复制输出的全部内容(以 ssh-rsa 开头,你的邮箱结尾)

4. 在 Gitee 添加 SSH 公钥

  1. 登录 Gitee 账号
  2. 点击右上角头像 → 设置 → SSH 公钥
  3. 点击"添加公钥"
  4. 填写:
    • 标题:可自定义(如"My PC")
    • 公钥:粘贴刚才复制的公钥内容
  5. 点击"确定"

5. 测试连接

ssh -T git@gitee.com

如果看到类似以下欢迎信息,说明配置成功:

Hi YourName! You've successfully authenticated, but GITEE.COM does not provide shell access.

6. 使用 SSH 克隆仓库

现在你可以使用 SSH 方式克隆仓库,无需每次输入密码:

git clone git@gitee.com:username/repository.git

常见问题解决

  1. 权限问题:

    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/id_rsa
    chmod 644 ~/.ssh/id_rsa.pub
    
  2. 多账号配置: 如果需要为不同 Gitee 账号配置不同密钥,可编辑 ~/.ssh/config 文件:

    Host gitee.com
     HostName gitee.com
     User git
     IdentityFile ~/.ssh/id_rsa
    
  3. 连接超时: 检查网络是否可访问 gitee.com,或尝试使用 HTTPS 代理。

通过以上步骤,你就可以实现 Gitee 的 SSH 免密登录了。