如何无密码登录ssh

· linux, ssh

每次用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