轉載自 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