How do I use my keyfile to sftp transfer data from one server to another

139,586

Solution 1

Try:

sftp -o "IdentityFile=keyname" [email protected]

You can use -o to pass any option that's valid in ~/.ssh/config.

Solution 2

Copy your PUBLIC key to the server using traditional means.

On server:

  • Create .ssh if it doesn't exist:
[[ ! -d "${HOME}/.ssh" ]] && mkdir -p "${HOME}/.ssh"
  • Implement the public key:
cat /path/to/public_key.pub >> "${HOME}/.ssh/authorized_keys"
  • Set appropriate permissions. OpenSSH is VERY ANAL about the permissions of the files in question:
chmod go-rwx "${HOME}" "${HOME}/.ssh/authorized_keys"

After that, you should be able to log in from the client using the PRIVATE key. To automate a transfer, you want to use a batch file, which is just a text file containing a list of commands to execute.

echo "put filename.foo /safe/path/filename.foo" >> /tmp/batchfile.txt
sftp -b /tmp/batchfile.txt -oIdentityFile=/path/to/private_key user@host

Alternatively, feel free to create a ~/.ssh/config file in ssh_config format so you can just type this in the future:

sftp -b /tmp/batchfile.txt host

Sample contents of ~/.ssh/config

Host the_hostname
    User user_name
    IdentityFile /path/to/private_key

Solution 3

If you are looking to setup sftp on ec2, this article might help

Share:
139,586

Related videos on Youtube

Jay
Author by

Jay

Updated on September 18, 2022

Comments

  • Jay
    Jay over 1 year

    I cant scp, the other server only takes sftp connections.

    Currently, I am trying to do

    sftp [email protected]:/files> put -r ~/
    

    -i keyname does not work, just resolves with illegal option -- i.

  • Jay
    Jay over 11 years
    Nailed it. thanks! how do i keep it there normally?
  • Jay
    Jay over 11 years
    Sorry, it really didnt.
  • Jay
    Jay over 11 years
    Ill try this on the next batch, the solution above did the trick though.
  • Celada
    Celada over 11 years
    I don't know what you mean by "keep it there normally". If you mean that you want the IdentityFile option to always automatically be given, check out UtahJarhead's answer about putting it in ~/.ssh/config
  • John Mayor
    John Mayor over 7 years
    This "answer" has nothing to contribute to the question. Worse, it is just a link to a page that will someday disappear.
  • Scott - Слава Україні
    Scott - Слава Україні over 5 years
    Why do you expect this to be helpful when the question said that the -i option is not working?