Linux system Admins normally login to the linux servers either supplying a password,
or using keybased authentication. sshpass is a tool which allows us to automatically
supply password to the command prompt so that automated scripts can be run as desired
by users. sshpass supplies password to ssh prompt using dedicated tty , fooling ssh to
believe that a interactive user is supplying password.
Install sshpass under Debian / Ubuntu Linux
Type the following command:
$ sudo apt-get install sshpass
Install sshpass under RHEL/CentOS Linux
$ sudo yum install sshpass
If you are using Fedora Linux, type:
$ sudo dnf install sshpass
Install sshpass under Arch Linux
$ sudo pacman -S sshpass
Install sshpass under OpenSUSE Linux
$ sudo zypper install sshpass
Install sshpass under FreeBSD Unix
To install the port, enter:
# cd /usr/ports/security/sshpass/ && make install clean
To add the package, run:
# pkg install sshpass
Getting Help :
# sshpass -h
Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters
- -f filename Take password to use from file
- -d number Use number as file descriptor for getting password
- -p password Provide password as argument (security unwise)
- -e Password is passed as env-var "SSHPASS" With no parameters – password will be taken from stdin
- -h Show help (this screen)
- -V Print version information
At most one of -f, -d, -p or -e should be used
How do I use sshpass in Linux or Unix?
Login to ssh server called example.com with password called redhat@1234
$ sshpass -p 'redhat@1234' ssh username@example.com
For shell script you may need to disable host key checking:
$ sshpass -p 'redhat@1234' ssh -o StrictHostKeyChecking=no username@example.com
TO RUN SOME COMMAND ON THE REMOTE SERVER TO CHECKING UPTIME
$sshpass -p 'redhat@1234' ssh username@example.com "uptime"
Sample output
01:04:35 up 126 days, 3:34, 2 users, load average: 0.50, 0.54, 0.55
Reading password from file
Another option is to read password from file using the -f option.
The syntax is:
sshpass -f fileNameHere ssh user@server