How to login to SSH via a bash script

Endrit Qerreti

Endrit Qerreti

In this tutorial you will learn how to connect to your server through SSH without typing username, password or port every time you want to login into your server.

This method saves you time as all you have to do is run a simple bash script. Follow the steps below how to do this

Step 1 - Update your system

Update your system with the command below.

sudo apt update

Step 1 - Install sshpass

Once you have updated your system, proceed by Installing sshpass package using the command below

sudo apt install sshpass


The following NEW packages will be installed:
0 upgraded, 1 newly installed, 0 to remove and 9 not upgraded.
Need to get 11,7 kB of archives.
After this operation, 35,8 kB of additional disk space will be used.
Get:1 jammy/universe amd64 sshpass amd64 1.09-1 [11,7 kB]
Fetched 11,7 kB in 0s (38,6 kB/s)
Selecting previously unselected package sshpass.
(Reading database ... 642296 files and directories currently installed.)
Preparing to unpack .../sshpass_1.09-1_amd64.deb ...
Unpacking sshpass (1.09-1) ...
Setting up sshpass (1.09-1) ...
Processing triggers for man-db (2.10.2-1) ...

Step 2 - Test sshpass

Once sshpass has been installed on your system, you need to check if sshpass is working correctly. The command below will output what each command does

sshpass -h

So far there's no issue when you call help argument, but let's do a real test, let's try to login to our ssh server and see if sshpass is working fine

The correct syntax to login to a SSH server by using username and password is

sshpass -p password ssh user@ip-address
Note: I am connecting to my localhost server that no one but my user has access, it is not recommended to use this method of logging in to a server that multiple users have access, the reason is because password can be seen on logs, I am covering this option to help you understand how sshpass works.

Read password from a file

Instead of using password as plaintext, use the -f argument which allows sshpass to read the password from a file, then set the correct permission to make this file readable only by you.

1)Create the password file

sudo nano password.txt

2)Next, type your password on the file


3)Save changes by pressing CTRL + X

4)Next, make password.txt readable by the owner only

sudo chmod 0400 password.txt

Now you can connect to your server by using the -f argument

sshpass -f password.txt ssh -o StrictHostKeyChecking=no -p "port" user@ip-address

Step 2 - Create bash script

Run the command below to create a bash script called, you can use any name you like, simply replace with

Example: We chose to call the script

sudo nano

This is the content of,

-p : password

ssh -o : Host key will not be checked when you try to connect to your server

-p : port

user : username


sshpass -f password.txt ssh -o StrictHostKeyChecking=no -p "port" user@ip-address

Next, save the content of by pressing CTRL + X

Step 3 - Make Executable

To make executable run

sudo chmod +x

After making executable, test if it runs fine


Step 4 - Run the bash script

After creating the bash script with your server credentials, run the script


To automate this script, you can use crontab. For example: you can create a cronjob that runs ./ and updates your server

1) Run crontab

crontab -e

2) Create task

0 0 * * 0 bash /root/

3) That's it!


In this tutorial you created a bash script that connects to your ssh server without having to type username and password or IP address, by using the sshpass utility.