This tutorial explains how to automatically login with a password when connecting to ssh.

After reading this tutorial, you’ll know how to implement a script to automatically login with passwords to connect to ssh. Additionally, you’ll find instructions for automatic ssh password login using the sshpass tool. Finally, this tutorial explains how to connect without a password with public key authentication.

How to script ssh login with passwords:

To begin, install expect by running the command below.

sudo apt install expect -y

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image1-34.png" data-lazy- height="539" src="data:image/svg xml,” width=”1140″>

Create a shell script by running the command below. You can use any arbitrary name for your script.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image3-29.png" data-lazy- height="108" src="data:image/svg xml,” width=”665″>

Copy the following code within the file, replacing [email protected] with your username and server. Also, replace passwordhere with your actual password.

#!/usr/bin/expect -f


spawn ssh linuxhint@192.168.1.103


expect “Password:*”


send “passwordherer


expect “$ “


interact

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image2-34.png" data-lazy- height="286" src="data:image/svg xml,” width=”744″>

Give the script execution rights by running the command shown in the screenshot below, replace sshscript.sh with your script name.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image5-24.png" data-lazy- height="145" src="data:image/svg xml,” width=”697″>

Then, run the script, and you’ll connect automatically without needing to type your password, as shown in the following image.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image4-27.png" data-lazy- height="588" src="data:image/svg xml,” width=”1052″>

How to automatically ssh login with passwords using sshpass:

Another option to connect through ssh with automatic password login is the sshpass tool. Although you can install sshpass using apt, yum, or any other packages manager, it is recommended to download its last version and install from sources. Some users reported problems with old versions found in some package managers.

To download the sshpass current version, run the command below.

wget https://sourceforge.net/projects/sshpass/files/sshpass/1.08/sshpass-1.08.tar.gz

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image7-20.png" data-lazy- height="489" src="data:image/svg xml,” width=”1168″>

Extract the .tar.gz package using the command below.

tar xvzf sshpass-1.08.tar.gz

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image6-25.png" data-lazy- height="459" src="data:image/svg xml,” width=”763″>

Enter the installation directory.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image9-15.png" data-lazy- height="279" src="data:image/svg xml,” width=”643″>

Run the following command to install sshpass.

./configure && make && make install

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image8-16.png" data-lazy- height="519" src="data:image/svg xml,” width=”1033″>

Once installed, run the command below to connect to your server. Replace the passwordhere with your actual password. Also, replace [email protected] with your username and server IP.

sudo sshpass -p “passwordhere” ssh linuxhint@192.168.1.103

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image12-10.png" data-lazy- height="542" src="data:image/svg xml,” width=”1142″>

As you can see, the connection was done properly.

Connect to ssh without password using public key authentication:

A better and more secure way to connect without needing to type your password is using public keys.

Generate public and private keys from the client you want to connect from by running the command below. When requested to type a passphrase, leave the field empty and press ENTER.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image10-13.png" data-lazy- height="512" src="data:image/svg xml,” width=”995″>

Now you need to copy the public key to the server you want to connect to. To copy the public key to the server, run the command below, replacing linuxhint with your actual username and 192.168.1.103 with your server IP address.

ssh-copy-id linuxhint@192.168.1.103

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image11-10.png" data-lazy- height="495" src="data:image/svg xml,” width=”1055″>

Once the public key is copied to the server, you can connect by running the following command. Replace the username and the IP address with yours.

ssh linuxhint@192.168.1.103

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image13-7.png" data-lazy- height="564" src="data:image/svg xml,” width=”1029″>

Conclusion:

I hope this tutorial explaining how to script ssh login with passwords was useful. Keep following Linux Hint for additional Linux tips and tutorials.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/linuxinstitute_icono-150×150.png61245ece07a0f.jpg" height="112" src="data:image/svg xml,” width=”112″>

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.