[Ubuntu] Two linux in the local area network realize ssh password-free login

To set up password-free login on two Linux hosts in the LAN, you can follow the steps below:

  1. Generate an SSH key pair on the first Linux host: Open a terminal, and execute the following command to generate an SSH key pair:

    ssh-keygen -t rsa
  2. When generating a key pair, you can choose to accept the default file paths and names, or customize them as needed.

  3. Copy the public key to the second Linux host: Execute the following command to copy the public key of the first host to the second host. Replace <user>and <hostname>with the username and hostname (or IP address) of the second host:

    ssh-copy-id <user>@<hostname>

    When you execute this command for the first time, you will be asked to enter the password for the second host. After entering the passphrase, the public key will be copied to a file on the second host ~/.ssh/authorized_keys.

  4. Test passwordless login: You can now try to log in from the first host to the second without entering a password. Execute the following command:

    ssh <user>@<hostname>

    If all goes well, you should be able to log in directly to the second host without entering a password.

  5. Optional step: Repeat the above steps to copy the public key of the second host to the first host to realize two-way password-free login.

Note that the above steps assume that you have the same user on both Linux hosts and that you have the appropriate permissions to perform the relevant operations. If you run into any issues, make sure you are executing the command with the correct username, hostname (or IP address), and with appropriate permissions.

Also, if you have customized or have special requirements for your SSH configuration, you may need to /etc/ssh/sshd_configmake additional settings in the configuration file (usually). Always be careful and back up related files when making configuration changes.

Guess you like

Origin blog.csdn.net/Holenxr/article/details/131624454