Thursday, February 02, 2023

Passwordless Login Setup Script

 Rewrite the setup passwordless login to Ubuntu from Mac OS using ssh keys by checking if ssh keys are already present


Here is a script in Bash that sets up passwordless login from a Mac OS laptop to an Ubuntu server using SSH keys, checking if the SSH keys are already present:


#!/bin/bash


# Variables for the remote server login

user=<username>

server=<server_ip_or_hostname>


# Check if the SSH keys are already present

if [ ! -f ~/.ssh/id_rsa.pub ]; then

  # Generate a new SSH key pair

  ssh-keygen -t rsa

fi


# Copy the public key to the Ubuntu server

ssh-copy-id $user@$server


# Log in to the Ubuntu server using SSH to confirm passwordless login

ssh $user@$server


Replace the placeholders <username> and <server_ip_or_hostname> with the appropriate values for your setup. Then, save the script to a file and make it executable with the chmod +x command. You can then run the script from the terminal.


This script checks if the SSH key pair is already present in the ~/.ssh directory on the Mac OS laptop. If the key pair is not present, it generates a new one. Then, it copies the public key to the Ubuntu server using ssh-copy-id, and logs in to the server using SSH to confirm passwordless login.