温馨提示×

debian gitlab怎么用ssh

小樊
53
2026-09-02 11:20:00
栏目: 智能运维

在 Debian 上自建的 GitLab 中使用 SSH 拉取/推送代码,一般步骤如下:


一、在 Debian 客户端生成 SSH 密钥

在你要操作的机器(不一定是服务器)上执行:

ssh-keygen -t ed25519 -C "your_email@example.com"

如果系统较老不支持 ed25519,可用:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

一路回车即可,默认生成:

  • 私钥:~/.ssh/id_ed25519
  • 公钥:~/.ssh/id_ed25519.pub

查看公钥内容:

cat ~/.ssh/id_ed25519.pub

二、把公钥添加到 GitLab

  1. 登录 GitLab Web 页面
  2. 右上角头像 → Preferences
  3. 左侧 SSH Keys
  4. 粘贴刚才的 id_ed25519.pub 内容
  5. 点击 Add key

三、确认 GitLab SSH 地址

在项目页面点击 Clone → Clone with SSH

地址类似:

git@gitlab.example.com:group/project.git

gitlab.example.com 是你 GitLab 的域名或 IP


四、测试 SSH 连接

ssh -T git@gitlab.example.com

成功会看到类似:

Welcome to GitLab, @username!

五、使用 SSH 操作仓库

克隆仓库

git clone git@gitlab.example.com:group/project.git

已有仓库改 SSH

git remote set-url origin git@gitlab.example.com:group/project.git

推送代码

git push origin main

六、常见问题(Debian 常见)

1. SSH 端口不是 22

如果 GitLab 用了其他端口(如 2222):

编辑 ~/.ssh/config

Host gitlab.example.com
    HostName gitlab.example.com
    Port 2222
    User git

2. 权限问题

确保:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519

3. GitLab 是 Docker 部署

  • 映射端口例如:22:22 或 2222:22
  • SSH 地址端口要对应

如果你愿意,可以告诉我:

  • GitLab 是 源码安装 / Omnibus / Docker
  • SSH 是否用 默认端口
  • 报错信息(如果有)

我可以直接按你的环境给你具体命令。

0 踩