每次用 ssh 登陆远程服务器都要输入一次密码太过于麻烦,可以使用密钥来实现无密码登陆 ssh. 我使用的本地客户端是 WSL, 也可以使用 PowerShell 或者 CMD 这里我本地的客户端为主机 A,想要本地连接远程主机 B.
本地创建密钥
首先在本地主机上创建一个 ssh 的密钥,-t 后加密类型, -b 指定密钥的大小
$ ssh-keygen -t rsa -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/home/username/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/username/.ssh/id_rsa.
Your public key has been saved in /home/username/.ssh/id_rsa.pub.
导入到远程主机
然后导入这个密钥到你的远程~/.ssh/authorized_keys 里, 可以直接复制 id_rsa.pub 中的密钥添加到远程的 authorized_keys,也可以通过命令导入
$ ssh-copy-id [email protected]
[email protected]'s password:
然后就可以登陆服务了
$ ssh [email protected]
参考地址: https://serverfault.com/questions/241588/how-to-automate-ssh-login-with-password