共计 1427 个字符,预计需要花费 4 分钟才能阅读完成。
生成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 "your_email@example.com"
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 创建完成
SHYour 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 your_email@example.com
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:
SH# start the ssh-agent in the background
$ eval "$(ssh-agent -s)"
Agent pid 59566
7、添加
$ 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 git@github.com
# Attempts to ssh to GitHub
免密登录
1、如果有 ssh-copy-id 命令,可通过:
$ ssh-copy-id -i ~/.ssh/id_ecdsa.pub smylers@compo.example.org
将公钥传到服务器上,当然这里可能需要输入密码。
2、你还可以将id_ecdsa.pub添加到ubuntu系统中,就可以远程免密登录了。
$ cat ~/.ssh/id_ecdsa.pub >> ~/.ssh/authorized_keys
保持长连接:
$ ssh -o ServerAliveInterval=100 root@121.42.162.85 //每一百秒发空包保持连接
正文完