Linux Commands

How to Script ssh Login with Passwords

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

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


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@
expect "Password:*"
send "passwordhere\r"
expect "$ "

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

chmod +x

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

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.


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

tar xvzf sshpass-1.08.tar.gz

Enter the installation directory.

cd sshpass-1.08

Run the following command to install sshpass.

./configure && make && make install

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@

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.


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 with your server IP address.

ssh-copy-id linuxhint@

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@


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

David Adams

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