温馨提示×

linux gitlab怎么用ssh

小樊
69
2026-09-07 13:15:41
栏目: 智能运维

在 Linux 上使用 GitLab 并通过 SSH 提交/拉取代码,一般步骤如下:


一、生成 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

复制输出的内容(以 ssh-ed25519 开头)。


三、把公钥添加到 GitLab

  1. 登录 GitLab
  2. 右上角头像 → Preferences
  3. 左侧 SSH Keys
  4. 粘贴公钥 → 点击 Add key

四、测试 SSH 连接

ssh -T git@gitlab.com

如果是自建 GitLab:

ssh -T git@your.gitlab.domain

看到类似:

Welcome to GitLab, @username!

说明成功。


五、使用 SSH 地址克隆仓库

在 GitLab 项目页面:

  • 点击 Clone
  • 选择 SSH
git clone git@gitlab.com:username/project.git

六、已有仓库改为 SSH(可选)

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

查看:

git remote -v

七、常见问题

1. 权限错误

chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub

2. 多个 GitLab 账号

可配置 ~/.ssh/config:

Host gitlab-work
    HostName gitlab.com
    User git
    IdentityFile ~/.ssh/id_ed25519_work

然后使用:

git clone git@gitlab-work:username/project.git

如果你用的是 自建 GitLab / 公司内网 / Docker GitLab,可以告诉我具体环境,我可以给你更精确的配置。

0 踩