在ubuntu上生成ssh key
首先使用
ls -al ~/.ssh
查看本地是否已经有key
如果没有会显示如下:
~$ ls -al ~/.ssh ls: cannot access '/home/liuxueneng/.ssh': No such file or directory
使用ssh-keygen 生成新的key
提示
Enter file in which to save the key (/home/liuxueneng/.ssh/id_rsa):
的时候直接回车按照默认名字就可以
提示
Enter passphrase (empty for no passphrase):
的时候最好输入passphrase并记住后面会用的到
liuxueneng@airfly-dev:~$ ssh-keygen -t rsa -C "liuxueneng@iairfly.com" Generating public/private rsa key pair. Enter file in which to save the key (/home/liuxueneng/.ssh/id_rsa): Created directory '/home/liuxueneng/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/liuxueneng/.ssh/id_rsa. Your public key has been saved in /home/liuxueneng/.ssh/id_rsa.pub. The key fingerprint is: SHA256:aXmIOOJDa+H1Urcz6KAJBvg1Zu97S0OpDhAu1beseXQ liuxueneng@iairfly.com The key's randomart image is: +---[RSA 2048]----+ | | | . | | o . . | |.o . + o = | |+ * O * E . | |.* X X B o | |..B * * * | |.o + O ..+ | | o =o.. | +----[SHA256]-----+
再次查看生成的key,可以看到id_rsa和 id_rsa.pub两个文件
ls -al ~/.ssh/ total 16 drwx------ 2 liuxueneng liuxueneng 4096 Apr 24 13:53 . drwx------ 15 liuxueneng liuxueneng 4096 Apr 24 13:55 .. -rw------- 1 liuxueneng liuxueneng 1766 Apr 24 13:53 id_rsa -rw-r--r-- 1 liuxueneng liuxueneng 404 Apr 24 13:53 id_rsa.pub
接下来ssh-add
如果出现提示“Could not open a connection to your authentication agent.”
则执行ssh-agent bash启动agent
再次ssh-add就可以了
ssh-add ~/.ssh/id_rsa Could not open a connection to your authentication agent. liuxueneng@airfly-dev:~$ ssh-agent bash liuxueneng@airfly-dev:~$ ssh-add ~/.ssh/id_rsa Enter passphrase for /home/liuxueneng/.ssh/id_rsa: Identity added: /home/liuxueneng/.ssh/id_rsa (/home/liuxueneng/.ssh/id_rsa)
提示
Enter passphrase
的时候输入第一次设置的passphrase即可