且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何生成无需密码即可登录服务器的ssh密钥

更新时间:2023-12-05 14:00:04

在要从中登录 的客户端计算机上,运行ssh-keygen.要快速,轻松地进行操作,只需在所有问题上按回车即可.这将在〜/.ssh中创建一个密钥对.具体来说,〜/.ssh/id_rsa是您的私钥(保持这一安全),而〜/.ssh/id_rsa.pub是您的公钥(可以分发).

On the client machine you wish to login from, run ssh-keygen. For a quick and easy key, just hit enter on all of the questions. This will create a key pair in ~/.ssh. Specifically, ~/.ssh/id_rsa is your private key (keep this one safe), and ~/.ssh/id_rsa.pub is your public key (okay to distribute).

将您的公钥(〜/.ssh/id_rsa.pub)复制到您要登录的服务器上(例如scp ~/.ssh/id_rsa.pub me@myserver:.在服务器上,运行cat id_rsa.pub >> .ssh/authorized_keys.确保它具有正确的权限,您可以运行chmod 644 ~/.ssh/authorized_keys.此​​外,您现在还可以删除复制过来的id_rsa.pub文件.

Copy your public key (~/.ssh/id_rsa.pub) onto the server that you wish to login to (e.g. scp ~/.ssh/id_rsa.pub me@myserver:. On the server, run cat id_rsa.pub >> .ssh/authorized_keys. To make sure that it has the correct permissions, you can run chmod 644 ~/.ssh/authorized_keys. Also, you can now delete the id_rsa.pub file that you copied over.

就是这样!您应该具有从客户端到服务器的无密码登录.如果要从服务器到客户端的无密码登录,则必须重复客户端和服务器的交换过程.

That's it! You should have password-less login from client to server. You must repeat the process with client and server swapped if you want password-less login from server to client.

注释:

  1. 如果服务器上不存在〜/.ssh目录,则创建该目录的***方法是从服务器SSH到其他计算机(例如客户端).这将确保它具有正确的权限.
  2. 如果您对有人可以访问客户端感到不安,可以使用密码保护密钥(运行ssh-keygen时提示之一),但是每次登录时都必须输入该密码.解决方案解决这个问题的方法是使用ssh-agent.
  1. If the ~/.ssh directory does not exist on your server, the best way to create it is to ssh from the server to some other machine (e.g. the client). This will ensure that it has the correct permissions.
  2. If you are paranoid about someone getting access to the client, you can password protect the key (one of the prompts when running ssh-keygen), but then you will have to enter that password every time you log in. The solution to this problem is to use ssh-agent.