Github ssh key生成,免密登录服务器方法

生成sshkey

1、查看是否存在ssh key,如果有会列出key的列表

$ ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist

2、没有,继续创建,下一步

$ ssh-keygen -t ecdsa -b 256 -C "[email protected]"
Generating public/private ecdsa key pair.

3、使用默认配置,点击Enter,继续

Enter file in which to save the key (/Users/you/.ssh/id_ecdsa): [Press enter]

4、输入密码,可以为空,Enter。(如果有密码,以后每次使用到该key都会要求输入密码)

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

5、到这里,ssh key 创建完成

Your identification has been saved in /Users/you/.ssh/id_ecdsa.
Your public key has been saved in /Users/you/.ssh/id_ecdsa.pub.
The key fingerprint is:
SHA256:VSGP++8s1yT9lpTRc12O+6+pxoqa5qeIB/D85nKnMjE [email protected]
The key's randomart image is:
+---[ECDSA 256]---+
|          . o.   |
|           =    .|
|          o .  o+|
|  .      . .  .o=|
|   +    S .    o=|
|    E .   o.  +o.|
|     B . o +. .*.|
|    o.*.o . =o.o=|
|    ooooBo ..=*oo|
+----[SHA256]-----+

6、添加到ssh-agent。首先确定ssh-agent is enabled:

# start the ssh-agent in the background
$ eval "$(ssh-agent -s)"
Agent pid 59566

7、添加到 agent

$ ssh-add ~/.ssh/id_ecdsa

8、说明 id_ecdsa是私钥,id_ecdsa.pub是公钥。私钥是不能外泄的,我们需要用到的是公钥。 读取到公钥的内容,你就可以在github添加key了。

连接github

1、复制公钥

$ pbcopy < ~/.ssh/id_ecdsa.pub
# Copies the contents of the id_ecdsa.pub file to your clipboard

2、将公钥粘贴到github的sshkey库里
3、测试连接

$ ssh -T [email protected]
# Attempts to ssh to GitHub

免密登录

1、如果有 ssh-copy-id 命令,可通过:

$ ssh-copy-id -i ~/.ssh/id_ecdsa.pub [email protected]

将公钥传到服务器上,当然这里可能需要输入密码。

2、你还可以将id_ecdsa.pub添加到ubuntu系统中,就可以远程免密登录了。

$ cat ~/.ssh/id_ecdsa.pub >> ~/.ssh/authorized_keys

保持长连接:

$ ssh -o ServerAliveInterval=100 [email protected] //每一百秒发空包保持连接

本文链接:参与评论 »

--EOF--

提醒:本文最后更新于 1323 天前,文中所描述的信息可能已发生改变,请谨慎使用。

Comments