When automating tasks in Linux through bash scripts, you frequently need to execute commands on remote servers using SSH. The massive problem with standard SSH connections is that they are inherently interactive; the system will physically pause the script execution and wait for a human being to manually type the password on a keyboard. This completely breaks unattended automation. While setting up SSH key pairs (passwordless authentication) is the standard and most secure solution, there are legacy situations where you are strictly forced to authenticate using a plain-text password. In these specific scenarios, the sshpass utility is the tool you need.
Installing sshpass
Because sshpass intentionally subverts standard security practices by handling passwords in plain text, it is almost never installed by default on modern Linux distributions.
To install it on Ubuntu or Debian, run:
sudo apt update
sudo apt install sshpass
On Red Hat, CentOS, or Fedora, use:
sudo dnf install sshpass
Running a Non-Interactive SSH Command
The syntax for sshpass is straightforward: you call the sshpass command, provide the password, and then append the exact ssh command you would normally use.
sshpass -p 'MySecretPassword123!' ssh [email protected] 'df -h'
In this example, sshpass takes the password provided via the -p flag and automatically feeds it into the standard SSH login prompt behind the scenes. The remote server authenticates the connection, executes the df -h command to check disk space, prints the results to your terminal, and immediately disconnects—all without ever pausing to ask for human input.
The Massive Security Risk (And How to Mitigate It)
Typing your password directly into a script or on the command line using the -p flag is incredibly dangerous. Any user on the same system who runs the ps aux or history commands can easily see your plain-text password while the script is running.
To mitigate this, you should never use the -p flag in a production script. Instead, you should store the password in a highly restricted, hidden text file and use the -f (file) flag to tell sshpass to read the password from that secure file.
- Create a file named
.secret_passand type only your password into it. - Lock down the file permissions so only the owner can read it:
chmod 600 .secret_pass - Modify your script to use the file instead of plain text:
sshpass -f /home/user/.secret_pass ssh [email protected] 'reboot'
Bypassing Strict Host Key Checking
If you are using sshpass to connect to a brand-new server for the very first time, standard SSH will pause to ask if you want to permanently accept the new server’s cryptographic footprint (the “Are you sure you want to continue connecting (yes/no)?” prompt). sshpass does not know how to answer “yes,” so the script will freeze and fail.
To completely automate first-time connections, you must instruct the SSH command to automatically accept the host key by appending the StrictHostKeyChecking=no option:
sshpass -f .secret_pass ssh -o StrictHostKeyChecking=no [email protected] 'ls -la'