Using SCP or SFTP with my ssh config file?

44

9

Perhaps my google-foo is failing me here... I'd like to connect and upload a mysql dump file via terminal using SFTP or SCP to my remote server using my ssh config file. According to documentation I've found, I should be able to do this:

sftp -F db.sql.gz webost@staging2.example.com /tmp

I have also tired the alias in my config:

sftp -F db.sql.gz myalias /tmp

When I do the two above, I simply get a print out of possible commands, -F being one of them.

I can already connect via ssh using the shortcut in my local config just fine so I know that works:

ssh myalias

**Note: I am connecting using a private / public key pair so I never need to enter a password. The key pair does have a passphrase associated with it but OS X Keychain remembered that the first time I connected.

... so I am not sure what I am doing wrong.

Danny Englander

Posted 2012-05-07T14:54:09.267

Reputation: 545

Answers

47

  1. From the help text: "... [-F ssh_config] ..."

    According to the above, -F expects one argument: path to an OpenSSH configuration file, ~/.ssh/config or similar. But you are giving it a gzipped SQL dump instead.

    Since plain ssh myalias is already working, you don't even need the -F option here. Just sftp myalias would connect to the server.

  2. However, the OpenSSH sftp client does not support uploading files like you are trying to; it can only download files (using the syntax host:path) or work in interactive mode. For uploading, you need to use either the interactive mode...

    $ sftp myalias
    sftp> cd /tmp
    sftp> put db.sql.gz
    

    ...or the scp tool:

    scp db.sql.gz myalias:/tmp
    

    or

    scp db.sql.gz webost@staging2.example.com:/tmp
    

(sftp does have a batch mode in which it can read commands from a file, using -b, but it is simpler to use scp for single uploads.)

There are other SFTP clients as well – lftp is good for interactive use, while curl can be easier to automate. For backups and such, you could also use rsync (which runs its own protocol but still inside SSH).

user1686

Posted 2012-05-07T14:54:09.267

Reputation: 283 655

wow this is perfect, worked great! I am just getting familiar with terminal so this will really come in handy in the future. – Danny Englander – 2012-05-07T15:23:30.103

0

Problem caused by ForwardAgent config as below,

Host dockervm
   HostName x.x.x.x
   User root
   Preferredauthentications publickey
   ForwardAgent yes

My case, it was due to the 'ForwardAgent' on the ssh config causing the scp to hang. Once commenting it out, worked for me.

Also, you could have an copy of the file with the ForwardAgent line commented and use scp -F to get it work.

MohanBabu

Posted 2012-05-07T14:54:09.267

Reputation: 43