Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass password to scp?

I know it is not recommended, but is it at all possible to pass the user's password to scp?

I'd like to copy a file via scp as part of a batch job and the receiving server does, of course, need a password and, no, I cannot easily change that to key-based authentication.

like image 952
Argelbargel Avatar asked Sep 08 '08 16:09

Argelbargel


People also ask

Does scp ask for password?

You can publish your docs via SSH through a Terminal window or more likely, via a shell script that you simply execute as part of the publishing process. However, you will be prompted for your password with each file transfer unless you configure passwordless SSH.

Can I use Sshpass with scp?

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.


3 Answers

Use sshpass:

sshpass -p "password" scp -r [email protected]:/some/remote/path /some/local/path

or so the password does not show in the bash history

sshpass -f "/path/to/passwordfile" scp -r [email protected]:/some/remote/path /some/local/path

The above copies contents of path from the remote host to your local.

Install :

  • ubuntu/debian
    • apt install sshpass
  • centos/fedora
    • yum install sshpass
  • mac w/ macports
    • port install sshpass
  • mac w/ brew
    • brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb
like image 197
KevinS Avatar answered Oct 13 '22 13:10

KevinS


just generate a ssh key like:

ssh-keygen -t rsa -C "[email protected]"

copy the content of ~/.ssh/id_rsa.pub and lastly add it to the remote machines ~/.ssh/authorized_keys

make sure remote machine have the permissions 0700 for ~./ssh folder and 0600 for ~/.ssh/authorized_keys

like image 24
mustafaturan Avatar answered Oct 13 '22 13:10

mustafaturan


If you are connecting to the server from Windows, the Putty version of scp ("pscp") lets you pass the password with the -pw parameter.

This is mentioned in the documentation here.

like image 55
gWay Avatar answered Oct 13 '22 14:10

gWay