问题答案 12026年5月27日 08:16
How to ssh to localhost without password?
To connect to localhost via SSH without a password, configure SSH key-based authentication. Below are the detailed steps:Step 1: Generate SSH KeysFirst, generate a pair of SSH keys (a public key and a private key) on your machine. This can be done using the command, which by default generates an RSA key pair.When prompted for the file save path, press Enter to accept the default path (typically ). If asked whether to enter a passphrase, press Enter to skip it for passwordless login.Step 2: Add Public Key to Authorization FileNext, add the generated public key (located by default at ) to the file for the same user. This can be done with the following command:Ensure the permissions of the file are correct. Use the following command to set them:Step 3: Verify SSH Server ConfigurationEnsure your SSH server configuration (typically in the file) allows key-based authentication. Locate the following lines and confirm they are set as shown:If you make changes, restart the SSH service:Step 4: Use SSH Keys for LoginNow you should be able to log in to localhost via SSH without a password. Test it with:The system should not prompt for a password and log you in directly.ExampleI once needed to automate certain tasks in my development environment, including file transfers between servers. With the above setup, I was able to achieve passwordless SSH login in scripts, greatly simplifying the automation process.This method is not only applicable to local environments but also to any remote server, provided you have permission to edit the file on the remote server. This setup is particularly useful for automated deployment and managing multiple servers.