1. 检查本机已有 SSH key
执行:
bash
ls -al ~/.ssh
看看有没有:
text
id_rsa
id_rsa.pub
或者:
text
id_ed25519
id_ed25519.pub
推荐使用 ed25519。
2. 生成新的 SSH Key
执行:
bash
ssh-keygen -t ed25519 -C "你的GitLab邮箱"
例如:
bash
ssh-keygen -t ed25519 -C "gegewu@xxx.com"
一路回车:
text
Enter file in which to save the key:
默认:
text
/Users/gegewu/.ssh/id_ed25519
即可。
如果提示:
text
Overwrite (y/n)?
说明已有 key:
- 不想覆盖 → 输入
n - 想更新 → 输入
y
3. 启动 ssh-agent
执行:
bash
eval "$(ssh-agent -s)"
输出类似:
text
Agent pid 12345
4. 添加 SSH key
执行:
bash
ssh-add ~/.ssh/id_ed25519
成功:
text
Identity added: /Users/gegewu/.ssh/id_ed25519
5. 复制公钥
Mac:
bash
pbcopy < ~/.ssh/id_ed25519.pub
现在 SSH key 已复制到剪贴板。
6. 添加到 GitLab
进入你的 GitLab:
User Settings
↓
SSH Keys
↓
Add an SSH key
填写:
Title
例如:
MacBook-Air
或者:
gegewu-mac
Key
粘贴刚复制的内容:
类似:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIxxxxxx gegewu@xxx.com
点击:
Add key
7. 测试 SSH
你的地址:
ind-gitlab.dreame.tech
测试:
bash
ssh -T git@ind-gitlab.dreame.tech
成功会类似:
text
Welcome to GitLab, @gegewu!