转载自 Github 帮助文档。
检查现有 SSH 密钥后,您可以生成新 SSH 密钥以用于身份验证,然后将其添加到 ssh-agent。
如果您还没有 SSH 密钥,则必须生成新 SSH 密钥。 如果您不确定是否已有 SSH 密钥,请检查现有密钥。
如果不想在每次使用 SSH 密钥时重新输入密码,您可以将密钥添加到 SSH 代理,让它管理您的 SSH 密钥并记住您的密码。
生成新 SSH 密钥
打开终端。
粘贴下面的文本(替换为您的 GitHub 电子邮件地址)。
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
这将创建以所提供的电子邮件地址为标签的新 SSH 密钥。
> Generating public/private rsa key pair.
提示您“Enter a file in which to save the key(输入要保存密钥的文件)”时,按 Enter 键。 这将接受默认文件位置。
> Enter a file in which to save the key (/home/you/.ssh/id_rsa): [Press enter]
在提示时输入安全密码。 更多信息请参阅“使用 SSH 密钥密码”。
> Enter passphrase (empty for no passphrase): [Type a passphrase] > Enter same passphrase again: [Type passphrase again]
将 SSH 密钥添加到 ssh-agent
将新 SSH 密钥添加到 ssh-agent 以管理密钥之前,应检查现有 SSH 密钥并生成新 SSH 密钥。
在后台启动 ssh-agent。
$ eval "$(ssh-agent -s)" > Agent pid 59566
将 SSH 私钥添加到 ssh-agent。 如果您创建了不同名称的密钥,或者您要添加不同名称的现有密钥,请将命令中的 id_rsa 替换为您的私钥文件的名称。
$ ssh-add ~/.ssh/id_rsa