1、在源服务器上生成SSH密钥对:执行SSH -keygen命令,根据提示生成新的SSH密钥对。这将创建两个文件:一个私钥(通常名为id_rsa)和一个公钥(通常名为id_rsa.pub)。
2、将公钥复制到目标服务器:使用ssh-copy-id命令将公钥复制到目标服务器。执行ssh-copy-id username@destination_server_ip命令,并根据提示输入目标服务器的密码。这会将您的公钥添加到目标服务器的authorized_keys文件中。
3、测试SSH密钥认证:尝试通过SSH username@destination_server_ip从源服务器SSH到目标服务器。你不需要输入密码就可以登录。
4、将scp与SSH密钥身份验证一起使用:现在SSH密钥身份验证已经建立,您可以使用scp在服务器之间传输文件,而不需要密码。例如,将文件从源服务器复制到目的服务器,执行命令scp /path/to/local/file username@destination_server_ip:/path/to/remote/directory。
附录:英文说明
Generate an SSH key pair on the source server: Run the command ssh-keygen and follow the prompts to generate a new SSH key pair. This will create two files: a private key (usually named id_rsa) and a public key (usually named id_rsa.pub).
Copy the public key to the destination server: Use the ssh-copy-id command to copy the public key to the destination server. Run ssh-copy-id username@destination_server_ip and enter the password for the destination server when prompted. This will add your public key to the destination server's authorized_keys file.
Test SSH key authentication: Try to SSH into the destination server from the source server by running ssh username@destination_server_ip. You should be able to log in without entering a password.
Use scp with SSH key authentication: Now that SSH key authentication is set up, you can use scp to transfer files between the servers without a password. For example, to copy a file from the source server to the destination server, run scp /path/to/local/file username@destination_server_ip:/path/to/remote/directory.