# SSh-passwdless login

**URL:** <https://forum.ansible.com/t/ssh-passwdless-login/32900>\
**Category:** Ansible Project\
**Created:** [May 11, 2020, 4:45am UTC](https://forum.ansible.com/t/ssh-passwdless-login/32900 "2020-05-11T04:45:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mahendra\_gaddam](https://avatars.discourse-cdn.com/v4/letter/m/aca169/32.png) [@Mahendra\_gaddam](https://forum.ansible.com/u/Mahendra_gaddam)\
**Post date:** [May 11, 2020, 4:45am UTC](https://forum.ansible.com/t/ssh-passwdless-login/32900/1 "2020-05-11T04:45:48Z")

</div>

Hi ,  
I’m trying to setup passwdless authentication for remote machine , when i’m doing manually it’s working fine when i’m doing by shell script it’s not working could you please help me out this. below is the shell script

Thanks

filename=“id\_rsa”  
path=“$HOME/.ssh”

if [$1]  
then  
hostname=$1  
if [$2]  
then  
username=$2  
else  
username=“$USER”  
fi  
else

# Read the host and username to store public key (the host/username accepting passwordless ssh from this computer)

#echo “What host you want to access with passwordless SSH from this computer?”  
#read hostname  
#echo “What is your username on $hostname? ($USER?)”  
#read username  
hostname=new\_hostname  
username=ansible\_user

if [! $username]  
then  
username=“$USER”  
fi  
fi

# Generate rsa files

if [-f $path/$filename]  
then  
echo “RSA key exists on $path/$filename, using existing file”  
else  
ssh-keygen -t rsa -f “$path/$filename”  
echo RSA key pair generated  
fi

echo “We need to log into $hostname as $username to set up your public key (hopefully last time you’ll use password from this computer)”  
cat “$path/$filename.pub” | ssh “$hostname” -l “$username” ‘[-d .ssh] || mkdir .ssh; cat \>\> .ssh/id\_rsa.pub; chmod 700 ~/.ssh; chmod 600 ~/.ssh/\*’  
status=$?

if [$status -eq 0]  
then  
echo “Set up complete, try to ssh to $host now”  
exit 0  
else  
echo “an error has occured”  
exit 255  
fi

Mahi

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 11, 2020, 10:34am UTC](https://forum.ansible.com/t/ssh-passwdless-login/32900/2 "2020-05-11T10:34:38Z")

</div>

What is the purpose of this shell script?

Please read [https://docs.ansible.com/ansible/latest/user\_guide/basic\_concepts.html#ansible-concepts](https://docs.ansible.com/ansible/latest/user_guide/basic_concepts.html#ansible-concepts),  
and then [https://docs.ansible.com/ansible/latest/user\_guide/connection\_details.html#ssh-key-setup](https://docs.ansible.com/ansible/latest/user_guide/connection_details.html#ssh-key-setup).
