I need to upload files from a headless raspberry pi to a remote server.

A simpfied example:

The file to transfer /tmp/test.html

Remote location: something.com Port 22 Username: user1 Password: pass:?word

Remote directory /www/fileadmin/subdir/

I can access the remote location from windows using the WinSCP with the SFTP protocoll. Note: The real password contains a colon and questionmark just like in the example, which may cause extra problems.

What do I need to do to upload a file from raspbian/debian?

  • have you tried to make an ssh connection from your RPi to the remote server? Were you able to connect? – Seamus Jan 9 at 23:39

scp /tmp/test.html user1@something.com:/www/fileadmin/subdir/

System will then prompt you for a password.

If you want this to work without a password prompt you'll need to set up ssh key based authentication.

  • Thanks, the problem is, this is a remote service I have no control over, so switching to ssh key based authentification is not an option because I have to work with what's available. – 576i Jan 10 at 8:53
  • @576i There is a tool called sshpass that you can install on RPi (apt-get/aptitude install sshpass) but I recommend against it. It is very bad habit to put ssh pasword anywhere else except your own head. Also check out this answer: stackoverflow.com/questions/50096/how-to-pass-password-to-scp – Mariusz Zieliński Jan 10 at 9:00
  • @576i Typically if you can ssh to your host with a password then ssh-copy-id user1@something.com should be able to set-up public key authentication on the host for you. Of course, you need to have prepared a password-less public key beforehand. – Roger Jones Jan 10 at 10:34

If you have a real sftp server on the remote side then you should be able to login and execute commands:

rpi ~$ sftp -P 22 user1@something.com   # you can omit default -P 22
user1@something.com's password:
Connected to something.com.
sftp> mkdir www/
sftp> mkdir www/fileadmin/
sftp> mkdir www/fileadmin/subdir/
sftp> cd www/fileadmin/subdir/

sftp> put /tmp/test.html
Uploading /tmp/test.html to /home/user1/www/fileadmin/subdir/test.html
/tmp/test.html                                100%   12    10.9KB/s   00:00

sftp> ls -l
-rw-r--r--    1 user1    user1          12 Jan 10 23:53 test.html
sftp> exit
rpi ~$

To upload the file in one batch you can execute:

rpi ~$ sftp user1@something.com:www/fileadmin/subdir/ <<< $'put /tmp/test.html'
user1@something.com's password:
Connected to something.com.
Changing to: /home/user1/www/fileadmin/subdir/
sftp> put /tmp/test.html
Uploading /tmp/test.html to /home/user1/www/fileadmin/subdir/test.html
/tmp/test.html                                100%   12    15.7KB/s   00:00
rpi ~$

The opposite for downloading is:

rpi ~$ sftp user1@something.com:www/fileadmin/subdir/test.html /tmp/test.html
user1@something.com's password:
Connected to something.com.
Fetching /home/user1/www/fileadmin/subdir/test.html to /tmp/test.html
/home/user1/www/fileadmin/subdir/test.html       100%   12     8.1KB/s   00:00
rpi ~$

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.