本文主要介绍如何使用终端,生成ssh key。 SSH只是一种协议,用于计算机之间的加密登录,存在多种实现,既有商业实现,也有开源实现。本文针对的实现是OpenSSH,它是自由软件,应用非常广泛。
生成SSH KEY:
$ ssh-keygen -t rsa -C "your_email@example.com" ,
然后会要求你输入公钥的名字:
Enter file in which to save the key (/Users/LaoTao/.ssh/id_rsa): ssh_rsa
接下来要求输入密码,以及确认密码:
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
以下是整个终端命令流程:
$ ssh-keygen -t rsa -C "your_email@example.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/LaoTao/.ssh/id_rsa): ssh_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in ssh_rsa.
Your public key has been saved in ssh_rsa.pub.
The key fingerprint is:
SHA256:m4+0RNpNoAgjjpmtIrQhmwvQHsGnOIoikVkJ/tVqHXM your_email@example.com
The key's randomart image is:
+---[RSA 2048]----+
|. |
|.o . . |
|..B .. +.E |
|o@.*..o.+. |
|%+=..o..S . |
|*Bo.. + = |
|%.. . * . |
|*. o + |
|. o . |
+----[SHA256]-----+
可以使用cat命令查看公钥:
$ cat ~/.ssh/ssh_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCuX2Zbre+4kPLkolvWXXtmvehFoO/LYVe+/EDA3j4NLX+6GZYXqAG6IEDAKzDKUzaUoxfuWZFk8ZyHhjfViD3FC90Yh9xuIXGYua4xyy6/7CC/HK2mS3Ha0SGW/QKMJ+QrLyeCpUeNiGDbH12DvUJpgp1vwTIIi0Pkykz6SP2mWkgpAKsBYExg+PkY2aLzopw2GpW0JApqQ9Jz/yMZQ5T7yh/k7iYZNWQ/ch9UxmM58gEQjw1qHiC7QZU9JvJsbh+qusNcIjcKLjMrwhw6q21jeGtn+7It4hKo9YL76OR/QRi1OWUJzqOaTY4ssUBEU15f3OGxVT03emJ556hFyVLn your_email@example.com
拷贝命令:
Windows:
clip < ~/.ssh/id_rsa.pub
Mac:
pbcopy < ~/.ssh/id_rsa.pub
GNU/Linux (requires xclip):
xclip -sel clip < ~/.ssh/id_rsa.pub
然后将复制的ssh_rsa.pub 公钥粘贴到git服务器的SSH配置中。例如github: