在 Mac 或者 Linux (控制电脑) 上生成一个public/private/ keypair(共锁和私锁),
然后将共锁(public key) 复制到要被远程的 Linux 上.
在 mac 的 Terminal 上输入指令ssh-keygen创建共锁和私锁, 用它默认的地方比较好. 回车确定.
$ ssh-keygenGenerating public/private rsa key pair.Enter file in which to save the key (/Users/MorvanZhou/.ssh/id_rsa):
确定后, 会确定是否想要一个保障密码,直接回车.
Enter passphrase (empty for no passphrase):
然后会要求再次确认, 回车
Enter same passphrase again:
最后, 会显示锁都已经生成好
Your identification has been saved in/Users/MorvanZhou/.ssh/id_rsa.Your public key has been saved in/Users/MorvanZhou/.ssh/id_rsa.pub.The key fingerprint is:SHA256:yVr3PAPmxVO1lBd7KvqBsBCZSE8mdYce8mjBiUfRDVE MorvanZhou@MorvanThe key's randomart image is:+---[RSA 2048]----+| o=*++*E o+|| ..**++...o+|| ..=*..oo|| ooo... ..|| .. S +=.||+* B o ||..+*||.+||.|+----[SHA256]-----+
将这个生成好的 “公锁” 复制去被控制的 Linux:
$ ssh-copy-id[被控制的用户名]@[它的ip]
回车后会要求输入一次被控制端电脑的密码:
$ ssh-copy-id morvan@192.168.0.114/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed:"/Users/MorvanZhou/.ssh/id_rsa.pub"/usr/bin/ssh-copy-id: INFO: attempting to log inwith the new key(s), to filter out any that are already installed/usr/bin/ssh-copy-id: INFO:1key(s) remain to be installed -- if you are prompted now it is to install the new keysmorvan@192.168.0.114's password:
密码正确后, 它将输出, 并告诉你怎么用 ssh 登录被控制端的电脑:
Number of key(s) added:1Now try logging into the machine,with:"ssh 'morvan@192.168.0.114'"and check to make sure that only the key(s) you wanted were added.