To make a Python script on my Raspberry Pi easily upload files to a Secure FTP server, I used the steps from Computer Hope’s Linux and UNIX sftp command page to set up Public Key Authentication.
Because my script uses the GPIO pins, it needs to be run as root (‘sudo python photobooth.py’), and so the public and private keys needed to be stored in the root user’s ~/.ssh directory (root@raspberrypi:~/.ssh), not the normal ‘pi’ user’s ~/.ssh directory.
So after completing Computer Hope’s steps to create the public and private keys in my RPi’s normal ‘pi’ user’s .ssh directory, I switched to ‘root’ user, and copied the .ssh directory from ‘pi’ user’s home directory to the ‘root’ user’s home directory:
sudo -i cp -r /home/pi/.ssh ~/.ssh exit
Uploading public key to server
My server has a cPanel interface, so I copied the public key to the server by:
- Log into cPanel.
- Visit the ‘SSH Access’ page.
- Visit the ‘Manage SSH Keys’ page.
- Choose to ‘Import Key’.
- Fill in the form:
- Name for this key: ‘id_rsa’.
- Paste the public key …: pasted in the contents of the ‘id_rsa.pub’ file in my RPi root’s .ssh directory.
- Click ‘Import’
- Then in the Public Keys table, authorise the key with:
- Click the ‘Manage’ link along side my new public key.
- Click ‘Authorize’