SFTP key authentication not working from bash script

Posted on

Problem :

I am trying to set up a very simple bash script on Ubuntu that SFTPs to a specific server using public key authentication. When I run the command directly on the command line, it works like a charm:

debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/xxx/.ssh/id_rsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Server accepts key: pkalg ssh-rsa blen 279

When I place the same command in a bash script and then run that script, the public key is not accepted and it goes to next authentications.

debug1: Offering RSA public key: /home/xxx/.ssh/id_rsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: password,publickey,keyboard-interactive

The command I am trying to run is very straightforward, nothing fancy about it:

sftp -vvv user@domain.com

Any ideas of what is causing this?

Solution :

Try to specify explicitly the path to private key file, using the -oIdentityFile=/path/to/id_rsa parameter

example :

sftp -oBatchMode=no -oIdentityFile=/home/username/.ssh/id_rsa -b - username@192.168.1.2 << !
   cd /directory
   put filename.txt
   bye
!

Leave a Reply

Your email address will not be published. Required fields are marked *