31

Is there an option to put the password on the line as well with sftp?

linux~ $ sftp [email protected]:/DIRECTORY_TO_GO_TO/ 

Like this

linux~ $ sftp [email protected]:/DIRECTORY_TO_GO_TO/ -p PASSWORD? 

7 Answers 7

15

Generally including a password in a command line is considered a security risk because it will show up to anyone else who can run ps/top, and it may be saved in your shell's history.

It would be a much better idea to setup key-based authentication if you are able.

Also, I don't believe it is going to be possible with sftp. It is meant to be used for secure transfers. If you really had to do something like this and you have no other choice then you probably need to be looking at automating with expect.

3
  • It would be a much better idea to setup key-based authentication if you are able. That's make sense.
    – Net Runner
    Jun 8, 2017 at 17:48
  • This expect url is broken Oct 31, 2018 at 15:44
  • 1
    I came here because I'm trying to figure out how to sftp my public key onto my target in order to set up key-based auth... :'(
    – weberc2
    Nov 10, 2020 at 18:26
61

As others have mentioned, a command-line password should be the last resort.

However, if nothing else is possible; one can go for ssh pass

sshpass -p <password> sftp user@host
7
  • 5
    This should be massively upvoted. It is really useful to have sshpass. Some providers don't allow you to create a .ssh folder and scp is also disallowed. Jul 5, 2013 at 1:38
  • 3
    good that some tools have your back: brew search sshpass -- "We won't add sshpass because it makes it too easy for novice SSH users to ruin SSH's security."
    – oliver
    Apr 4, 2017 at 9:22
  • 9
    This should be the accepted solution. The OP asked how to do this specific thing - not for security advice (albeit well-intentioned and correct).
    – ebr
    Jun 8, 2017 at 20:33
  • 1
    The better option would be to use SSHPASS="MY_PASSWORD" sshpass -e sftp user@host this prevents the password to be visible for other user by looking into ps -axf
    – meles
    Aug 20, 2021 at 15:48
  • 1
    Further, if you must enter a password on the command line, if you use bash include a space in export HISTIGNORE=' ...' and prefix your command by a single space so it isn't captured in your history file. Sep 17, 2021 at 6:31
7

Just use perl, ruby or python to script what you are trying to do. In case of ruby it's just (taken from the net-sftp API docs):

require 'net/sftp'

Net::SFTP.start('host', 'username', :password => 'password') do |sftp|

  # upload a file or directory to the remote host

  sftp.upload!("/path/to/local", "/path/to/remote")

end

For more info http://net-ssh.rubyforge.org/sftp/v2/api/index.html

1
  • paramiko is pure python (no extra libs) and works really well
    – qwertzguy
    Oct 31, 2012 at 10:52
4

Don't do that - setup SSH public key authentication for automatic login.

2
  • Can sftp do this the same is scp?
    – Daniel
    Oct 13, 2009 at 19:32
  • 1
    @Daniel, both sftp and scp are built on SSH which allows for key-based authentication.
    – Zoredache
    Oct 13, 2009 at 23:19
2

For searchers that don't care that the password can be seen in the command-line command:

sftp userid:password@remoteHost is how to include the password in the sftp connect command.

UPDATE: this turned out to be incorrect... see comments

3
  • OpenSSH v5.3p1 and v4.3p2, both on Red Hat Enterprise...
    – ashnazg
    Dec 13, 2018 at 15:06
  • 1
    This turned out to be incorrect... this method appeared to work for me because PHP and its ssh2 extension were being used earlier in my code to do actual standalone authentication... and the sftp command was being run later. It was this later piece where the syntax appeared to succeed.
    – ashnazg
    Dec 13, 2018 at 15:25
  • 1
    Could not resolve hostname user: No such host is known. It is considering ":" in username which is not the case in FileZilla CLI Jun 4, 2019 at 15:10
2

As the other answers have stated, use public key authentication. There is a great, although a little dated, IBM developerWorks series that should explain everything you want to know about it, as well as some useful supplemental tools such as keychain.

1
  • The link above is no longer exist. May 5, 2021 at 10:04
1

I suspect that there are as many answers as there are FTP clients. An SFTP server should not accept authentication information until encryption is established, so that the user and password are protected.

I believe the FileZilla client will allow for command line passing of the user and password .. see the documentation here. Given the reputation of the FileZilla project, I would expect it to operate securely.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .