I want to know how can I connect automatically using sftp with a password as an argument in the same command to get the automatic login an execute once.


I want to put it into a script, and try to make one sentence to do this task.


  • Is using a password essential, or would an authorized key be acceptable? – ams Mar 23 '16 at 14:27
  • Could be both, but, for security reasons, it needed a custom password. – vicdeveloper Mar 23 '16 at 14:27
  • 2
    if you are hard coding a password anywhere, the security goes out the window. Hence I believe it is your duty to convince peole who are hell bent on security to abandon using of passwords and handle authentication using authorized key pairs. – MelBurslan Mar 23 '16 at 14:32
  • The thing, that I need to transfer many files to a remote server, but, I dnt like to that server contains my public_rsa_key, so, I need to run sometimes the script file, and inside it I need to passing the password like as argument or file way to evoid to type it any time that I will use it. – vicdeveloper Mar 23 '16 at 14:32
  • Why would it be a problem that the public key is stored on another system? It's not as if it can be misused. – Jenny D Mar 23 '16 at 14:37

There's four ways to do passwordless ssh/scp:

  1. Create a new pass-phrase-free ssh key on the client, and add it to authorized_keys on the server.

  2. As above, but set a passphrase on the key, and use ssh-agent to unlock it in advance of running scp.

  3. Use ssh connection sharing, perhaps with an appropriate ControlPersist setting, to log into the server in advance of your scp running. The scp will reuse the same connection and not need a password.

  4. Use some dodgy script to enter the password, but ssh/scp actively discourage this so it's not as easy as you'd like.

I strongly suggest you investigate the first two options (option 2 being more secure), and option 3 if authorized_keys are not available. There are plenty of places documenting how to do it.

Option 4 is possible (I see someone else has already posted a solution), but you need to be aware that that will leak your password to other users of the machine and/or anybody who can read your script. The ssh keys are protected from all but the root user, and pass-phrase protected keys are protected even from root (ssh-agent hacks aside).



sshpass -p <password> sftp user@host


Run the following:

ssh-keygen -t rsa -b 2048


touch ~/.ssh/config

Now edit config to look something like:

# host-specific options
Host enigma
    HostName mydomain.com
    Port 2547
    ServerAliveInterval 120
    TCPKeepAlive Yes

Now connect

ssh-copy-id username@server.com

You should be able to connect to the Server using the Hostname in the config file. i.e. ssh enigma

I have about 20 connections in my Config file, and all short referenced so i dont have to type the domain and password each time.

The secuirty flaw to this, is that all SSH connections unless you start creating different Hostfiles. Are in one file. Protected by one password. So if you store 20 Connections to the one file, Then if that one password is compromised then so are all 20 connections.

I use a couple of hosts file, Personal, Work and Misc its not too much of a issue if a password is leaked then

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.