我有我的公钥/私钥设置,但我不知道它应该去哪里。应该~/.ssh/
为我的用户放置这两个文件吗?
私钥留在家里。公钥旅行:
~/.ssh
(本地主机)中。它通常会~/.ssh/id_rsa
。~/.ssh/authorized_keys
(远程主机)。虽然第一步通常在您创建密钥时自动完成,但第二步可以通过以下方式完成:
$ ssh-copy-id ~/.ssh/id_rsa.pub user@remotehost
Run Code Online (Sandbox Code Playgroud)
如果您有关于远程主机的任何残留配置,您可能会遇到一些麻烦(身份验证失败,或“身份验证失败太多”)。为了摆脱这个问题,你可以强制ssh-copy-id
使用密码认证:
$ ssh-copy-id -o"PubkeyAuthentication no" ~/.ssh/id_rsa.pub user@remotehost
Run Code Online (Sandbox Code Playgroud)
当然,一旦您正确放置了密钥,就不再需要密码:
$ ssh user@remotehost
Run Code Online (Sandbox Code Playgroud)
您可能还想添加一些 SSH 配置~/.ssh/config
以使其更简单:
Host [custom name for the remote machine]
Hostname [remote hostname or IP]
User [remote username]
IdentityFile /home/[your local user]/.ssh/id_rsa
Run Code Online (Sandbox Code Playgroud)
多亏了这一点,您只需键入以下内容即可登录远程主机:
$ ssh [custom name for the remote machine]
Run Code Online (Sandbox Code Playgroud)
编辑:如果两台机器(本地和远程)相同,您可以使用以下方法简化程序:
$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
Run Code Online (Sandbox Code Playgroud)
这会将您的公钥附加到authorized_keys
文件中。当然,另一种在本地机器上以其他人身份登录的方法是使用:
$ su [another user]
Run Code Online (Sandbox Code Playgroud)
这将保存您可能不需要的 SSH 事务。可以使用以下su
方式设置无密码sudo
:
$ sudo -iu [another user]
Run Code Online (Sandbox Code Playgroud)