使用 ssh 密钥进行连接但没有 -i 选项

Rob*_*ert 5 ssh rsa

我正在使用 serverals 主机,我不想每次连接到任何主机时都指定密码。然后我生成了一个密钥:

Enter file in which to save the key (/home/robe/.ssh/id_rsa): my_key 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in my_key.
Your public key has been saved in my_key.pub.
The key fingerprint is:
e6:d9:a3:85:38:b1:b2:a9:4d:3b:c1:33:65:df:57:25 robe@computer
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|              E .|
|               o |
|      o       .  |
|   . o..S.   .   |
|    =  *.+. .    |
|    o++ + +.     |
|   o.= . o .     |
|  ..=.  .        |
+-----------------+
Run Code Online (Sandbox Code Playgroud)

然后我将my_key.pub复制到每个主机中的authorized_keys文件,我可以连接:

ssh -i /path/to/my_key  user@host
Run Code Online (Sandbox Code Playgroud)

但这是我的问题,我想在不指定密钥的情况下进行连接。我想找到一种运行方式:

ssh user@host
Run Code Online (Sandbox Code Playgroud)

注意:我想保留我的默认 id_rsa 和 id_rsa.pub 键。my_key 键是管理其他主机的替代选项。

小智 17

您正在寻找~/.ssh/config配置文件。例如,在此文件中添加此条目。

Host host
    HostName host.example.com
    User johndoe
    IdentityFile ~/.ssh/host.key
Run Code Online (Sandbox Code Playgroud)

现在你可以输入

ssh host
Run Code Online (Sandbox Code Playgroud)

并且 ssh 将自动使用您通过IdentityFile参数指定的用户名和密钥。因此,该命令等效于:

ssh johndoe@host.example.com -i ~/.ssh/host.key
Run Code Online (Sandbox Code Playgroud)

有关该文件的所有配置详细信息,请参阅http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man5/ssh_config.5