Setting up Public Key Authentication for RPi SFTP

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:

  1. Log into cPanel.
  2. Visit the ‘SSH Access’ page.
  3. Visit the ‘Manage SSH Keys’ page.
  4. Choose to ‘Import Key’.
  5. Fill in the form:
    1. Name for this key: ‘id_rsa’.
    2. Paste the public key …: pasted in the contents of the ‘id_rsa.pub’ file in my RPi root’s .ssh directory.
  6. Click ‘Import’
  7. Then in the Public Keys table, authorise the key with:
    1. Click the ‘Manage’ link along side my new public key.
    2. Click ‘Authorize’

Leave a Reply

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