Introduction
In this tutorial, we will walk you through the process of configuring passwordless SSH login in a Debian-based virtual machine (VM). Passwordless SSH login allows you to securely connect to your VM without the need for entering a password each time. This not only enhances convenience but also improves the overall security of your system. We will cover all the necessary steps to enable this feature in your Debian-based VM.
Step 1: Enabling SSH Authentication in the VM
The first step is to enable SSH authentication in your virtual machine. Open a terminal and execute the following command to edit the SSH server configuration file:
sudo nano /etc/ssh/sshd_config
Within the editor, locate the following lines:
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
Ensure that these lines match the configuration shown above. These settings enable public key authentication while disabling password authentication for SSH.
Step 2: Creating the SSH Key Pair
To establish password less SSH login, we need to generate an SSH key pair on your local machine. If you have already generated an SSH key pair, you can skip this step.
- Open a terminal on your local machine.
- Execute the following command to generate an SSH key pair:
ssh-keygen -t rsa -b 4096
- You will be prompted to enter a file in which to save the key. Press Enter to accept the default location (
~/.ssh/id_rsa
). - Next, you will be prompted to enter a passphrase. For password less login, leave it blank and press Enter.
Note: Using an empty passphrase makes the key vulnerable if someone gains unauthorized access to it. Ensure that your local machine is secure and protected.
- The SSH key pair has been generated. You will see a message indicating the location of the public and private keys.
Step 3: Copying the Public Key to the VM
Now that we have generated the SSH key pair, we need to copy the public key to the VM.
- In the terminal on your local machine, execute the following command to navigate to the .ssh directory:
cd ~/.ssh
- Execute the following command to open the authorized_keys file in a text editor:
sudo nano authorized_keys
- Copy the contents of the public key file (
id_rsa.pub
) from your local machine and paste them into the authorized_keys file on the VM. - Save the file and exit the text editor.
Step 4: Restarting the SSH Service
To apply the changes and enable password less SSH login, we need to restart the SSH service in the VM.
- In the terminal of the VM, execute the following command:
sudo systemctl restart sshd
- The SSH service will be restarted, and your VM is now configured for password less SSH login.
Step 5: Testing Password less SSH Login
Now that we have configured password less SSH login in your Debian-based virtual machine, let's test the setup to ensure everything is working correctly.
- Open a terminal on your local machine.
- Execute the following command to initiate an SSH connection to your VM:
ssh username@vm_ip_address
Replace username
with your VM's username and vm_ip_address
with the IP address of your VM.
- If everything is configured correctly, you should be able to establish a password less SSH connection to your VM without being prompted for a password.
Note: If you encounter any issues during this step, make sure you have followed all the previous steps accurately, including copying the public key to the correct location (~/.ssh/authorized_keys
) in the VM.
- Once connected to your VM via SSH, you can execute commands, transfer files, or perform any other operations as needed.
Step 6: Optional - Disabling Password Authentication
To further enhance the security of your VM, you can disable password authentication for SSH completely. This ensures that only SSH key-based authentication is allowed.
- Open a terminal on your VM.
- Execute the following command to edit the SSH server configuration file:
sudo nano /etc/ssh/sshd_config
- Locate the line
PasswordAuthentication
and change its value tono
:
PasswordAuthentication no
- Save the file and exit the text editor.
- Restart the SSH service in your VM to apply the changes:
sudo systemctl restart sshd
From now on, SSH access to your VM will be exclusively through SSH key authentication.
Conclusion
In this tutorial, we have walked you through the step-by-step process of configuring password less SSH login in a Debian-based virtual machine. By following these instructions, you have eliminated the need for entering a password each time you connect to your VM via SSH. This not only streamlines the login process but also enhances the security of your system by relying on SSH key-based authentication. Remember to keep your SSH private key secure and follow best practices to safeguard your VM and data. Enjoy the convenience and security of password less SSH login in your Debian-based virtual machine!