导读 大多数 Git 服务器都会选择使用 SSH 公钥来进行授权。系统中的每个用户都必须提供一个公钥用于授权,没有的话就要生成一个。
查看之前是否生成过密钥

SSH 公钥默认储存在账户的主目录下的 ~/.ssh 目录。

$ cd ~/.ssh
$ ls  # 查看有用 something 和 something.pub 来命名的一对文件没
authorized_keys2  id_dsa       known_hosts
config            id_dsa.pub

如果有 something 和 something.pub 来命名的一对文件,这个 something 通常就是 id_dsa 或 id_rsa。有 .pub 后缀的文件就是公钥,另一个文件则是密钥。假如没有这些文件,或者干脆连 .ssh 目录都没有,可以用 ssh-keygen 来创建。该程序在 Linux/Mac 系统上由 SSH 包提供,而在 Windows 上则包含在 MSysGit 包里。

生成 SSH 密钥
$ ssh-keygen -t rsa -C "youremail@email.com"  # 引号中换成你自己的邮箱地址

Generating public/private rsa key pair.  # 生成密钥对 
Enter file in which to save the key (/root/.ssh/id_rsa):  # 保存路径 (默认用户家目录.ssh下)
Enter passphrase (empty for no passphrase):   # 密码,默认空 (如果不想在使用公钥的时候输入密码,可以留空)
Enter same passphrase again:   # 重复密码
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
92:41:73:6d:ba:03:bf:36:f8:ab:a2:90:0c:9c:a1:85 youremail@email.com
The key's randomart image is:
+--[ RSA 2048]----+
|      o ..       |
| .   . o  o      |
|E..   .  o       |
|o.o   .o.        |
|oo    ooS.       |
|o.     .+        |
|o.     . o       |
| .  . . +        |
|  .. ..+oo       |
+-----------------+
提交公钥

找到.ssh文件夹,用文本编辑器打开“id_rsa.pub”文件,复制内容到剪贴板。
将剪贴板的公钥内容添加到git托管服务器。

原文来自:https://mounui.com/171.html

本文地址:https://www.linuxprobe.com/git-generate-ssh.html编辑:薛鹏旭,审核员:逄增宝

Linux命令大全:https://www.linuxcool.com/

Linux系统大全:https://www.linuxdown.com/

红帽认证RHCE考试心得:https://www.rhce.net/