与 SSH 连接
tip
关于SSH 使用 SSH 协议可以连接远程服务器和服务并向它们验证。 利用 SSH 密钥可以连接 GitHub,而无需在每次访问时都提供用户名和个人访问令牌。
参考 GitHub 文档
检查现有的 SSH 密钥
$ ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist
检查目录列表以查看是否已经有 SSH 公钥。 By default, the filenames of supported public keys for GitHub are one of the following.
- id_rsa.pub
- id_ecdsa.pub
- id_ed25519.pub
生成新 SSH 密钥
粘贴下面的文本(替换为您的 GitHub 电子邮件地址)。
ssh-keygen -t ed25519 -C "your_email@example.com"
提示您“Enter a file in which to save the key(输入要保存密钥的文件)”时,按 Enter 键。 这将接受默认文件位置。
# 输入SSH连接密码,要记住
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]
将 SSH 公钥复制到剪贴板
pbcopy < ~/.ssh/id_ed25519.pub
# Copies the contents of the id_ed25519.pub file to your clipboard
在个人 GitHub 设置中添加新的 SSH
- 在任何页面的右上角,单击您的个人资料照片,然后单击 Settings(设置)。
- 在用户设置侧边栏中,单击 SSH and GPG keys(SSH 和 GPG 密钥)。
- 单击 **New SSH key(新 SSH 密钥)**或 Add SSH key(添加 SSH 密钥)。
- 在 "Title"(标题)字段中,为新密钥添加描述性标签。 例如,如果您使用的是个人 Mac,此密钥名称可能是 "Personal MacBook Air"。
- 将密钥粘贴到 "Key"(密钥)字段。
- 单击 Add SSH key(添加 SSH 密钥)
测试 SSH 连接
输入以下内容:
ssh -T git@github.com
# Attempts to ssh to GitHub