Think before you speak, read before you think.

ssh的key的生成

生成密钥对

使用ssh-keygen来生成密钥对
(具体参数请参阅man ssh-keygen)

ssh-keygen -b 1024 -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
5c:81:74:f3:00:97:65:8a:09:ce:0e:43:ee:d7:66:72 root@debian
The key’s randomart image is:
+–[ RSA 1024]—-+
| . ..oo*oo |
| o o ..=.B |
| + o o o . |
| . + o . |
| . + E |
| . = |
| |
| |
| |
+—————–+

无交互生成密钥对

ssh-keygen -b 2048 -t rsa -f ~/.ssh/id_rsa -q -N ""

密钥分发

方法1:

刚才生成了一对密钥,把私钥放在自己的机器上的~/.ssh/目录下并保证访问权限是“-rw——-”(即600)。私钥永远不要给别人。

再把生成的公钥放在要连接的远程主机的~/.ssh/目录下并改名为authorized_keys,并且保证文件除了属主外没有被人修改的权限“-rw——-”(即600)。公钥无需保密

方法2:

linux内置了ssh-copy-id命令,直接使用

ssh-copy-id [email protected]

然后按提示输入密码之后,此脚本会自动将公钥copy到目标机器用户目录下的authorized_keys中并修改权限。对于一个key管理大批量主机时自动化分发。其实就是方法1的脚本实现。

如果目标主机ssh端口不是22,公钥也不是id_rsa.pub,可以指定端口和公钥

ssh-copy-id -i /home/secure.pub "-p 3322 [email protected]"

测试

ssh,scp使用密钥登陆(-i指定私钥路径)

ssh -i jpuyy-key [email protected]
scp -i jpuyy-key filename [email protected]:/home/

更改key的passphrase

上面在生成密钥对的时候会提示输入密钥,需要更改id_rsa文件的密码时,使用如下命令即可更改。

ssh-keygen -f id_rsa -p

判断一对公私钥是不是一对

diff <( ssh-keygen -y -e -f "~/.ssh/id_rsa" ) <( ssh-keygen -y -e -f "ec2.demo.pub" )

如果 diff 有输出则不是一对

验证 cert 是不是合法

ssh-keygen -L -f ~/.ssh/id_rsa-cert.pub

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *