SSH 配置

SSH 配置,第1张

由于本地Git仓库和GitHub仓库之间的传输是通过SSH加密的,所以必须要让github仓库认证你SSH key,在此之前,必须要生成SSH key。

第1步:创建SSH Key。在windows下查看[c盘->用户->自己的用户名->.ssh下是否有id_rsa、id_rsa.pub文件如果没有需要手动生成。

在开始菜单中打开git下的git bash(当然,在其他目录下打开git bash也是一样的):

然后执行:

cd ~/.ssh

如果能进入到.ssh文件目录下 ,则证明,之前生成过.ssh秘钥,可以直接使用里面的秘钥。

如果不能进入到.ssh文件目录下,则:

检测下自己之前有没有配置:git config user.name和git config user.email(直接分别输入这两个命令)

一、如果之前没有配置过

1.配置

git config –global user.name ‘xxxxx’

git config –global user.email ‘ xxx@xx.xxx ’

2.生成密钥

ssh-keygen -t rsa -C ‘上面的邮箱’

接着按3个回车 则:

Generating public/private rsa key pair.

Enter file in which to save the key (/c/Users/Mr.Yang/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /c/Users/Mr.Yang/.ssh/id_rsa.

Your public key has been saved in /c/Users/Mr.Yang/.ssh/id_rsa.pub.

The key fingerprint is:

SHA256:zA6wNJrFB6NcqS6eBog/AHlzQuvFjYpG759Yhh1lWGI xxxxxx@xxxxx.xxx (上面自己的邮箱)

The key's randomart image is:

+---[RSA 2048]----+

|+E . |

| ..+oo+ |

| oo+ +.o |

|o. ===+o |

|==+*... S|

|B.+.o .o |

|++o. + .|

| +o.+ . |

|. o.o |

+----[SHA256]-----+

最后在.ssh目录下得到了两个文件:id_rsa(私有秘钥)和id_rsa.pub(公有密钥)

如果想登陆远端,则需要将rsa.pub里的秘钥添加到远端,打开setting->SSH keys,点击右上角 New SSH key,把生成好的公钥id_rsa.pub放进 key输入框中,再为当前的key起一个title来区分每个key,title名称随便命名。

SSH密钥生成结束后,你可以在SSH目录下看到私钥id_rsa和公钥id_rsa.pub这两个文件,不要把私钥文件id_rsa的信息透露给任何人。我们可以通过文本编辑器或cat命令来查看id_rsa.pub公钥信息。

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


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

原文地址: http://outofmemory.cn/tougao/9953091.html

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

发表评论

登录后才能评论

评论列表(0条)

保存