You can use the sshpass command to provide the password for ssh based login. It is a non-interactive ssh password auth tool. From the man page:
sshpass is a utility designed for running ssh using the mode referred to as “keyboard-interactive” password authentication, but in non-interactive mode.
ssh uses direct TTY access to make sure that the password is indeed issued by an interactive keyboard user. Sshpass runs ssh in a dedicated tty, fooling it into thinking it is getting the password from an interactive user.
The command to run is specified after sshpass’ own options. Typically it will be “ssh” with arguments, but it can just as well be any other command. The password prompt used by ssh is, however, currently hardcoded into sshpass.
Install sshpass under Debian / Ubuntu Linux
Type the following command:
$ sudo apt-get install sshpass
Sample outputs:
Fig.01: Installing sshpass on Debian/Ubuntu Linux
Install sshpass under RHEL/CentOS Linux
First, enable EPEL repo and type the following yum command:
$ 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
How do I use sshpass in Linux or Unix?
Login to ssh server called server.example.com with password called t@uyM59bQ:
$ sshpass -p 't@uyM59bQ' ssh username@server.example.com
For shell script you may need to disable host key checking:
$ sshpass -p 't@uyM59bQ' ssh -o StrictHostKeyChecking=no username@server.example.com
Security unwise warning: The -p option should be considered the least secure of all of sshpass’s options. I recommend that you use ssh’s public key authentication.
A bash shell script example with SSHPASS
The syntax is:
SSHPASS='t@uyM59bQ' sshpass -e ssh vivek@server42.cyberciti.biz SSHPASS='t@uyM59bQ' sshpass -e ssh vivek@server42.cyberciti.biz date SSHPASS='t@uyM59bQ' sshpass -e ssh vivek@server42.cyberciti.biz w SSHPASS='t@uyM59bQ' sshpass -e ssh -o StrictHostKeyChecking=no vivek@server42.cyberciti.biz
The password is passed as environment variable called SSHPASS.
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
Create a file as follows:
$ echo 'myPassword' > myfile $ chmod 0400 myfile $ sshpass -f myfile ssh vivek@server42.cyberciti.biz
How do I backup /var/www/html using rsync?
Run rsync over SSH using password authentication, passing the password on the command line:
$ rsync --rsh="sshpass -p myPassword ssh -l username" server.example.com:/var/www/html/ /backup/
OR
$ SSHPASS='yourPasswordHere' rsync --rsh="sshpass -e ssh -l username" server.example.com:/var/www/html/ /backup/
How do I use sshpass with gpg encrypted file?
First, create a file as follows:
$ echo 'mySshPasswordHere' > .sshpassword
Now, encrypt a file using gpg command:
$ gpg -c .sshpassword
$ rm .sshpassword
Finally, use it as follows:
$ gpg -d -q .sshpassword.gpg > fifo; sshpass -f fifo ssh vivek@server1.cyberciti.biz
If you just type sshpass, you will see help screen as follows:
Fig.02: sshpass command in action
Further readings:
- keychain: Set Up Secure Passwordless SSH Access For Backup Scripts
- sshpass man page
- Non-interactive ssh password auth – project home page.
🐧 37 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
I don’t agree to this trick, better use an ssh-agent and ssh-add. Check out this howto: http://meinit.nl/ssh-agent-trick
Regards, Robert de Bock.
Wow… this is scary. I would never recommend this method to anyone! If you want to ssh using command line with no password prompt, just create your ssh key without a password.
Not only would ps reveal the password for your ssh key, but also it is stored in your history on the filesystem!
For history, just store the password in a var..
read pass
sshpass -p “$pass” ssh root@wherever
history | grep sshpass
1028 sshpass -p “$pass” ssh root@localhost
for ps, modern linux blanks out the pasword.
0 S root 28468 27445 0 80 0 – 1554 poll_s 13:16 pts/3 00:00:00 sshpass -p zzzzzzzz ssh root@localhost
I’ve always done it using ssh-copy-id or the good ol manual way.
$ mkdir -p ~/.ssh If it doesn’t already exist
$ chmod 700 ~/.ssh
$ cd ~/.ssh
$ ssh-keygen -t rsa
$ cat ~/.ssh/id_rsa.pub | ssh ‘mkdir .ssh; chmod 700 .ssh; cat>>.ssh/authorized_keys’
Web input filter changed the last line of my comment.
$ cat ~/.ssh/id_rsa.pub | ssh hostname ‘mkdir .ssh; chmod 700 .ssh; cat>>.ssh/authorized_keys’
it is recommended for sererrs it may used in scripts,that is not comes under system history and you may given password for that script.
I use this tool and it is safe. Here is my scenario
I’ve a central backup server and I’m the only person who logs in. Server is connected to internet and no services are running except ssh on vpn interface. I need to login to over 20 boxes collocated or leased in 5 data centers. With this tool I don’t have to upload ssh keys to those boxes. So if any one of the production box got hacked, my backup server remain intact.
Personally, I find keychain to be the best alternative. At least I know I’m save from any possible cracking!
http://www.gentoo.org/proj/en/keychain/
On an extra note, one can clear the keychain (keychain –clear) on every login using .bash_profile just in case. ;)
This is what exactly I looking for.
All the servers I used ssh-copy-id. But recently one server replaced by 3rd party and they manage it and I do not have any write access login (no home directory). I run a script to rsync just two directories and last two weeks I had to do this manually since cron job filling.
My problem fixed by this solution.
Sometimes it is not possible to add the keys or advisable to do so. sshpass is an excellent solution for large deployments of secure systems that prevents the innate issues of unauthenticated access.
Agreed. Not always the “preferred” solution but sometimes when SSH keys aren’t an option, this solution will get you going.
I was wondering how to implement this on different port not default port 22…
Just put :[portnumber] after the location
Ex: $ sshpass -p ‘t@uyM59bQ’ ssh username@server.example.com:2400
Nice post
Is it really working on your distribution?
Only -p port is working on Opensuse.
ssh: Could not resolve hostname some.server.com:2222: Name or service not known
for me this is not working, but sshpass -p ‘password’ ssh username@server.example.com -p 12345
it’s
sshpass -p 'P@$$W0RD' ssh -p 12345 username@server.example.com
doesn’t work
You can always write a bash script and secure the credentials in an include file:
# Include the Login credentials:
. /path/to/credentials
# rsync using vars defined in credentials, e.g.:
rsync -r -a -v -e “sshpass -p $SSH_PASSWORD ssh -l $SSH_LOGIN” –delete /path/to/local/dir $SSH_HOST:/path/to/remotedir/
IT world is complex and there are situations where you simply can’t use rsa keys.
Ii’s stupid that ssh developers think just one way and not letting users chose what thay need to use.
I’m now adding second account to dd-wrt router and since rsa keys are global there for all users I have to use password in bash to create reverse ssh tunnel.
My second account has /bin/false shell.
And finally this sshpass is not working for me. Not sure why.
“It’s not recommended”, bla bla bla bla bla. Sometimes you need these solutions, even if they are risk.
Thanks for giving this option. I F*UCKING KNOW the best way is using ssh keys. But if you F*CKING CANT use them for some F*CKING REASON, this solution fits like a glove.
I tested with scp, and it works as well.
Yes! I really wanted to scream this to all those “but it’s not secure!” people.
Sometimes you have good reasons not to worry about security but on the other hand you need automation and you just can’t use keys. SSH’es insistence on “securing” the ssh by preventing this drives me crazy.
*NIX used to be about giving people all the rope they need to hang themselves. Now you have all those ‘rm – are you sure? [yes/no]’. Just give me my damn rope please.
Sir my laptop is stolen
Create Repos under: /etc/yum.repos.d as epel.repo with following contents:
vim /etc/yum.repos.d/epel.repo
File:
Also create file /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL with following contents
vim /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL
File:
and then run
yum -y install sshpass
Thanks for this solution. Where I work they don’t have home directories installed on remote hosts so I can’t set up public keys. I would like a solution like ssh-agent where I typed in my password once (so it’s held in memory), and the agent supplied it when I ssh somewhere.
Seems like an improvement might be to store the password in a file in the local home-dir. The password could be encrypted in a variety of ways and the file 600 protected. The password for the encryption could be passed on the command line or set in an environment variable.
This would protect from ps, potentially allow repeated access without having to specify the password each time and allow for a few other things like requiring local encryption password rotation independent of the remote password.
Still not as good as the proper ways of doing things but better than straight password-on-the-commandline and potentially some benefits over interactive or outhorized_keys.
Note that sshpass does have -f (file) and -e (environment) options. These don’t do all that I mentioned but would be a good first step in guarding against ps revealing your password.
Thanks for this! I doesn’t work here. I get the message “debug1: Next authentication method: password” and after the process waits forever. Any suggestions?
Hi All
I am in progress of building a syslog and configuration management server. I would like to schedule an automatic backup of Cisco running configurations and that has to be stored as device name and each day a new folder has to be created as mm/dd/year.
By so, all backup’s that happened yesterday should be under 07/04/2013 directory and the one for today should be under 07/05/2013.
Need your assistance on this.
Thanks much…
Thanks a lot buddy, you saved a lot of my time.
I couldn’t resist commenting. Perfectly written!
Great article.
I want to schedule auto file copy with crontab and sshpass. The file is order than 3 days. Here is the code but not working:
00 00 * * * find /home/username/folder -mtime +3 -exec scp {} sshpass -p 0000 remotesrver@myipaddress \;
00 00 * * * sshpass -p 0000 find/home/username/folder -mtime +3 -exec {} scp remoteserver@ipaddress \;
Both commands are not working. Help
This is exactly what I was looking for. Oh! and thanks for mentioning the “StrictHostKeyChecking” part, that was helpful as well.
Saw the security risks too, will keep that in mind.
Is there a way to set a remote dir when using sshpass, so that after login I cd into a specified directory, please?
Does it works for the cert’s passphrase too ?
$ ssh -i key.txt user@host