Understand the SSH key (Z)
我们知道使用ssh的keygen来创建private 和 public key, 之后通过拷贝public key到指定的host,之后当我们需要从创建key的主机上登录指定的host时我们就可以不再需要输入password了。步骤如下:
首先在source machine(可以是local machine, 也可以是已经远程等率或者ssh到的machine)执行:
ssh-keygen -t rsa
这样在用户目录下的.ssh folder 就会创建这么几个文件:
id_rsa 里面有刚刚产生的private key
id_rsa.pub 里面有用来copy到其他host的public key
然后我们执行:
ssh-copy-id remoteuser@hostmachine
或者
cat ./ssh/id_rsa.pub | ssh remoteuser@hostmachine "cat >> ~/.ssh/authorized_keys"
把刚刚产生的public key复制到target host. 执行时我们需要输入remote user在host machine的密码。
这样我们就建立了一个从source machine到target machine的秘密约定:
local machine remote machine
user A's private key ---------------------------- remote user B's ssh knows
about user A's public key
有意思把,这就是为什么当我们再次从source machine,即user A的machine上ssh到remote user B的target machine时我们不再需要输入密码了,因为remote user B的 machine的 ssh曾经已经问过这个密码了!oh yeah
评论
发表评论