Thursday 16 May 2024

Ssh to linux machine without using password

To SSH connect to a linux machine, a raspberry pi in my example from a PC without using a username and password, you can set up SSH key-based authentication. This method involves generating a public and private key pair on your PC and copying the public key to the Raspberry Pi. Here’s a step-by-step guide to do this:

Step 1: Generate SSH Key Pair on Your PC

  1. Open a terminal on your PC.

    • On Linux or macOS, open the terminal.
    • On Windows, you can use PowerShell or Git Bash.
  2. Generate the key pair:

    ssh-keygen -t rsa -b 4096 -C ""
    • The -t rsa -b 4096 options specify the type of encryption (RSA) and the key size (4096 bits).
    • The -C option adds a comment (usually your email) to the key for identification purposes.
  3. Follow the prompts:

    • When prompted to "Enter file in which to save the key," you can press Enter to accept the default location (usually ~/.ssh/id_rsa).
    • Choose whether to set a passphrase. A passphrase adds an extra layer of security, but for passwordless login, you can leave it empty by pressing Enter.

Step 2: Copy the Public Key to the Raspberry Pi

  1. Transfer the public key:

    • Use the ssh-copy-id command to copy the public key to your Raspberry Pi. Replace pi@raspberrypi with your actual username and Raspberry Pi hostname or IP address:
      ssh-copy-id pi@raspberrypi
    • If you haven't changed the default user, pi is the username, and raspberrypi is the hostname. If you've changed them, use your custom values.
  2. Enter the password:

    • You will need to enter the password for the Raspberry Pi user one last time. After this, the public key will be added to the ~/.ssh/authorized_keys file on the Raspberry Pi.

Step 3: Connect to the Raspberry Pi Using SSH

  1. Initiate the SSH connection:
    ssh pi@raspberrypi
    • You should now be able to connect without being prompted for a password.

Step 4: (Optional) Adjust SSH Configuration for Convenience

  1. Edit your SSH config file:

    • Open the SSH config file in a text editor:
      nano ~/.ssh/config
    • Add the following configuration:
      Host raspberrypi HostName raspberrypi User pi IdentityFile ~/.ssh/id_rsa
    • Adjust HostName and User according to your setup.
  2. Save and exit:

    • Press Ctrl+X to exit, Y to confirm changes, and Enter to save.
  3. Now you can simply connect using:

    ssh raspberrypi

Troubleshooting Tips

  • Ensure SSH is enabled on the Raspberry Pi: You can enable SSH via the Raspberry Pi configuration tool or by placing an empty file named ssh (without any extension) on the boot partition of the SD card.
  • Correct file permissions: Ensure that your .ssh directory and files have the correct permissions:
    chmod 700 ~/.ssh chmod 600 ~/.ssh/id_rsa chmod 644 ~/.ssh/ chmod 600 ~/.ssh/authorized_keys

By following these steps, you should be able to SSH into your Raspberry Pi from your PC without needing to enter a username and password each time.

No comments:

Post a Comment