Skip to content

How to Pass Password to ssh/scp Command in Bash Script

How to Pass Password to ssh/scp Command in Bash Script. sshpass is a useful tool used for running ssh authentication in non-interactive mode.

Covers
SSH
On this page

sshpass is a useful tool used for running ssh authentication in non-interactive mode. Using sshpass you can use passwords to ssh or scp command without interactions, which helps to utilize in shell scripts. Instead of using this we recommend using key based authentication.

Install sshpass

The sshpass package is available in default packages repositories for most of the latest operating systems. You can use following commands to install it on your system.

On Debian:

sudo apt install sshpass

On CentOS:

yum --enablerepo=epel -y install sshpass

Using sshpass

sshpass uses SSHPASS environment variable to store user passwords. You need to first SSHPASS variable with password then use this command. Here is a simple shell script using sshpass.

You can also run ssh command as following with sshpass.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top