如何生成SSH key

如何生成SSH key,第1张

1. 检查SSH keys是否存在

输入下面的命令,如果有文件id_rsa.pub 或 id_dsa.pub,则直接进入步骤3将SSH key添加到GitHub中,否则进入第二步生成SSH key

ls -al ~/.ssh

# Lists the files in your .ssh directory, if they exist

2. 生成新的ssh key

第一步:生成public/private rsa key pair

在命令行中输入ssh-keygen -t rsa -C "your_email@example.com"

默认会在相应路径下(/your_home_path)生成id_rsa和id_rsa.pub两个文件,如下面代码所示

ssh-keygen -t rsa -C "your_email@example.com"

# Creates a new ssh key using the provided email

Generating public/private rsa key pair.

Enter file in which to save the key (/your_home_path/.ssh/id_rsa):

第二步:输入passphrase(本步骤可以跳过)

设置passphrase后,进行版本控制时,每次与GitHub通信都会要求输入passphrase,以避免某些“失误”

Enter passphrase (empty for no passphrase): [Type a passphrase]

Enter same passphrase again: [Type passphrase again]

sample result:

Your identification has been saved in /your_home_path/.ssh/id_rsa.

Your public key has been saved in /your_home_path/.ssh/id_rsa.pub.

The key fingerprint is:

#01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com

第三步:将新生成的key添加到ssh-agent中:

# start the ssh-agent in the background

eval "$(ssh-agent -s)"

Agent pid 59566

ssh-add ~/.ssh/id_rsa

3. 将ssh key添加到GitHub中

用自己喜欢的文本编辑器打开id_rsa.pub文件,里面的信息即为SSH key,将这些信息复制到GitHub的Add SSH key页面即可

不同的 *** 作系统,均有一些命令,直接将SSH key从文件拷贝到粘贴板中,如下:

mac

pbcopy <~/.ssh/id_rsa.pub

# Copies the contents of the id_rsa.pub file to your clipboard

windows

clip <~/.ssh/id_rsa.pub

# Copies the contents of the id_rsa.pub file to your clipboard

linux

sudo apt-get install xclip

# Downloads and installs xclip. If you don't have `apt-get`, you might need to use another installer (like `yum`)

xclip -sel clip <~/.ssh/id_rsa.pub

# Copies the contents of the id_rsa.pub file to your clipboard

一般情况下,我们只会生成一个SSH Key,名字叫id_rsa,然后提交到多个不同的网站(如:GitHub、DevCloud、Gitee、GitLab)。

eg:尝试给Github配一个ssh。首先需要做的就是在命令行中输入以下指令

复制id_rsa.pub到对应的Git服务器即可。

但是也存在另一种需要,我们在同一个网站上,注册了两个或者多个用户名,通常网站不会允许我们为这两个用户名,配置同一个SSH Key,这时候就会有些麻烦。

a. 一个gitee,用于A项目的一些开发活动;

b. 一个github,用于B项目的一些开发活动;

c. 一个gitlab,用于C项目的一些开发活动;

针对不同的账户,生成一个不同用途的SSH-Key,然后进行相关的配置即可。

下面以mac 设备为例:

1.生成对应的SSH-Key:

生成一个gitee用的SSH-Key

生成一个github用的SSH-Key

生成一个gitlab用的SSH-Key

2.在 ~/.ssh 目录下新建一个config文件(终端:touch config),添加如下内容(其中Host和HostName填写git服务器的域名,IdentityFile指定私钥的路径)

3.然后进入 ~/.ssh ,拷贝不同的xxx_id_rsa.pub到不同的服务器进行配置,eg:拷贝 github_id_rsa.pub 内容,然后,进入Github,在Your profile里,点击左边SSH keys,再在右边点击Add SSH key按钮,如图:

其中,Title随便填,将id_rsa.pub里的内容复制到Key中,点击下方的Add key按钮。

4.用ssh命令分别测试

这里以gitee为例,成功的话会返回下图内容:

github的ssh配置如下:

一 、

设置git的user name和email:

$ git config --global user.name "xuhaiyan"

$ git config --global user.email "haiyan.xu.vip@gmail.com"

二、生成SSH密钥过程:

1.查看是否已经有了ssh密钥:cd ~/.ssh

如果没有密钥则不会有此文件夹,有则备份删除

2.生存密钥:

$ ssh-keygen -t rsa -C “haiyan.xu.vip@gmail.com”

按3个回车,密码为空。

Your identification has been saved in /home/tekkub/.ssh/id_rsa.

Your public key has been saved in /home/tekkub/.ssh/id_rsa.pub.

The key fingerprint is:

………………

最后得到了两个文件:id_rsa和id_rsa.pub

3.添加密钥到ssh:ssh-add 文件名

需要之前输入密码。

4.在github上添加ssh密钥,这要添加的是“id_rsa.pub”里面的公钥。

打开https://github.com/ ,登陆xuhaiyan825,然后添加ssh。

5.测试:ssh git@github.com

The authenticity of host ‘github.com (207.97.227.239)’ can’t be established.

RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added ‘github.com,207.97.227.239′ (RSA) to the list of known hosts.

ERROR: Hi tekkub! You’ve successfully authenticated, but GitHub does not provide shell access

Connection to github.com closed.


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11549838.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-16
下一篇2023-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存