温馨提示×

linux gitlab怎么免密

小樊
38
2026-09-16 16:18:09
栏目: 智能运维

在 Linux 上使用 GitLab 实现免密操作(拉取/推送代码不需要输入账号密码),常见有 3 种方式,最推荐的是 SSH 方式


一、SSH 密钥方式(✅ 最推荐)

1. 生成 SSH 密钥

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

(如果系统旧,可用 rsassh-keygen -t rsa -b 4096

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

~/.ssh/id_ed25519.pub

2. 查看公钥并复制到 GitLab

cat ~/.ssh/id_ed25519.pub

复制输出内容,在 GitLab 中: User → Preferences → SSH Keys → Add Key

3. 测试连接

ssh -T git@gitlab.com

(如果是自建 GitLab,替换为你的域名,如 git@gitlab.yourcompany.com

看到欢迎信息即成功。

4. 使用 SSH 地址克隆

git clone git@gitlab.com:username/project.git

✅ 之后 push / pull 都不再需要密码


二、Git 凭据缓存(HTTPS 方式)

如果你必须用 HTTPS 地址:

1. 使用 credential 缓存

git config --global credential.helper cache

默认缓存 15 分钟

2. 长期保存(不推荐服务器)

git config --global credential.helper store

账号密码会明文保存在:

~/.git-credentials

三、Personal Access Token(HTTPS + Token)

GitLab 已逐步禁止密码登录 HTTPS

1. 生成 Token

GitLab: User → Preferences → Access Tokens

2. 使用方式

git clone https://<token>@gitlab.com/username/project.git

或第一次输入:

  • 用户名:你的用户名
  • 密码:Token

推荐方案总结

方式 推荐度 说明
SSH ⭐⭐⭐⭐⭐ 最安全、最方便
Token + HTTPS ⭐⭐⭐ 必须 HTTPS 时用
密码 GitLab 已不支持

如果你告诉我:

  • gitlab.com 还是自建 GitLab
  • SSH 还是 HTTPS
  • 服务器 / 容器 / 桌面 Linux

我可以给你更精确的命令。

0