WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Remote Python debugging through SSH  (Read 3446 times)

Offline tohox

  • Newbie
  • *
  • Posts: 25
Remote Python debugging through SSH
« on: August 11, 2016, 08:24:36 AM »
Hi,

I'm using PyCharm to debug an application running on a Pi3/picore8b3 under the Python3.5 interpreter. This is currently done through SSH/SFTP with the default tc user. This seems to be working fine except if my script uses a package such as RPi.GPIO which normally requires running the python interpreter as sudo. My script and debugging session then stop with the following error:
RuntimeError: No access to /dev/mem.  Try running as root!

What I'd like to do is give the tc user the privileges which are required to use the GPIO (or /dev/mem). I tried this http://stackoverflow.com/questions/12133860/accessing-the-gpio-of-a-raspberry-pi-without-sudo which is very similar but based on Raspbian and it doesn't work.

Any other suggestions?

Thanks,

EDIT: The same applies if I use the smbus2 or i2c packages. They too require that the script be run with root privileges in order to access /dev/i2c-1
« Last Edit: August 11, 2016, 08:47:43 AM by tohox »

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: Remote Python debugging through SSH
« Reply #1 on: August 11, 2016, 09:47:20 AM »
sudo
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline tohox

  • Newbie
  • *
  • Posts: 25
Re: Remote Python debugging through SSH
« Reply #2 on: August 11, 2016, 10:48:24 AM »
Yes, which is what I do when I simply want to run the python script from the command line...

However PyCharm is an IDE which is run remotely from a separate computer and connects via SSH/SFTP to update the files on the Pi and hook into the interpreter to run and debug the script. In order to do this PyCharm is given a username and password (tc/piCore in this case) so it can connect remotely. I don't think PyCharm can issue the equivalent of a sudo. It simply runs with the privileges of the user specified in its config.

So my question is: How can I give a user (tc or any other new user I create) the sufficient privileges to access GPIO and I2C without using sudo?

Thanks,

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: Remote Python debugging through SSH
« Reply #3 on: August 11, 2016, 10:52:06 AM »
The dirty way is to enable root login in the sshd config file.
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline tohox

  • Newbie
  • *
  • Posts: 25
Re: Remote Python debugging through SSH
« Reply #4 on: August 11, 2016, 11:17:13 AM »
I tried that as well:
- uncommented PermitRootLogin yes in /usr/local/etc/ssh/sshd_config
- sudo passwd root (gave a new password)
- sudo passwd -u root (which returned passwd: password for root is already unlocked)
- filetool.sh -b

and I rebooted but I still couldn't connect via SSH using the root user : Authentication failed

Offline patrikg

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 672
Re: Remote Python debugging through SSH
« Reply #5 on: August 11, 2016, 11:46:22 AM »
You maybe forget to include the changed files in the file .filetool.lst for your change.

Code: (bash) [Select]
nano -w /opt/.filetool.lst

etc/passwd
etc/shadow
usr/local/etc/ssh/ssh_config
usr/local/etc/ssh/sshd_config
usr/local/etc/ssh/ssh_host_dsa_key
usr/local/etc/ssh/ssh_host_dsa_key.pub
usr/local/etc/ssh/ssh_host_ecdsa_key
usr/local/etc/ssh/ssh_host_ecdsa_key.pub
usr/local/etc/ssh/ssh_host_rsa_key
usr/local/etc/ssh/ssh_host_rsa_key.pub

And take backup with
Code: (bash) [Select]
filetool.sh -bv
« Last Edit: August 11, 2016, 11:49:24 AM by patrikg »

Offline tohox

  • Newbie
  • *
  • Posts: 25
Re: Remote Python debugging through SSH
« Reply #6 on: August 11, 2016, 12:40:59 PM »
Thanks for your suggestion.

I added the usr/local/etc/ssh/ssh_config and usr/local/etc/ssh/sshd_config lines which were indeed missing in filetool.lst but the changes in sshd_config don't seem to get stored when I do filetool.sh -b. Also when I ran filetool.sh with the above the first time I got an error for usr/local/etc/ssh/ssh_config as the file doesn't exist so I removed it from filetool.lst and started over with the same unfortunate result.

Why aren't the changes I make to usr/local/etc/ssh/sshd_config getting backed up if it is included in filetool.lst?

Offline tohox

  • Newbie
  • *
  • Posts: 25
Re: Remote Python debugging through SSH
« Reply #7 on: August 11, 2016, 01:11:34 PM »
I just noticed that every time I restarted sshd, the changes I made to sshd_config got overwritten so I looked inside the /usr/local/etc/init.d/openssh script and my impression is that sshd_config gets overwritten by sshd_config.example each time the service is started which would explain why filetool.sh -b wasn't effective.

Now, sshd_config.example appears to be a link to /tmp/tcloop/openssh/usr/local/etc/ssh_config.example which I can't edit...

Could this be the same issue as reported here?
http://forum.tinycorelinux.net/index.php/topic,19859.msg123215.html#msg123215
« Last Edit: August 11, 2016, 01:16:22 PM by tohox »

Offline tohox

  • Newbie
  • *
  • Posts: 25
Re: Remote Python debugging through SSH
« Reply #8 on: August 12, 2016, 07:16:21 AM »
I finally managed to set PermitRootLogin to yes in openssh_config by creating a copy of the openssh startup script and modifying it so it wouldn't overwrite openssh_config every time the service starts. I then modified bootlocal.sh so it would start my script instead of the usual one, backed everything up, restarted and I could finally log in as root.

Something strange is happening however. Even if I use the root username and password to log in via SSH, once I'm connected I get the tc@box prompt and don't have any of the root privileges. So it would seem I'm effectively logged in as tc even though I use root's credentials...

Offline tohox

  • Newbie
  • *
  • Posts: 25
Re: Remote Python debugging through SSH
« Reply #9 on: August 15, 2016, 06:07:45 AM »
In case anyone else would like to attempt remote Python debugging with a Raspberry Pi under piCore here are the conclusions I came to:

- In order to access peripherals like  I2C or the GPIO port you need root access to /dev/mem (be it locally or remotely through SSH). All attempts I made at giving the default TC user the appropriate rights failed so I tried logging in as root through SSH instead.

- By default (and rightfully so) root is not allowed to login through SSH but there is a setting in the ssh service configuration file named PermitRootLogin yes that should allow you to do it anyway.

- I t would seem like the startup script for OpenSSH that comes with piCore 8b4 has a bug which prevents any modifications made to sshd_config to stick as the file will get overwritten by sshd_config.example each time the service is started/restarted.

- Even if you do manage to start the ssh service with PermitRootLogin yes then assign a password to the root user and login using the new credentials, tinycore will change the actual user back to TC upon logging in... I think you can change this behavior with a combination of adding noautologin to cmdline.txt and adding /etc/shadow to filetool.lst before backing up but it didn't work for me unfortunately.

In the end the solution which proved simplest was to use Raspbian as my debug platform and once debug was done copy my finished python script back to piCore.

I'd really like to know if anyone has managed to give the default TC user access to /dev/mem which would by far have been the simplest option for me.

Thanks!

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: Remote Python debugging through SSH
« Reply #10 on: August 15, 2016, 07:43:13 AM »
openssh updated to 7.3p1 in piCore 8.x repos. Startup and service control scripts also updated, bugs fixed.
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline gavinmc42

  • Sr. Member
  • ****
  • Posts: 301
Re: Remote Python debugging through SSH
« Reply #11 on: August 16, 2016, 07:52:42 PM »
I have a setio.sh script I call on boot to setup the GPIO pins

Code: (bash) [Select]
echo "21" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio21/direction
echo "0" > /sys/class/gpio/gpio21/value
chmod 666 /sys/class/gpio/gpio21/value


For getting i2c data I also call a bit of shell script

Code: (bash) [Select]
varhi=$(sudo i2cget -y 1 0x73 0x00 b)
I have also used micropython to read the i2c

Code: (python) [Select]
temp1hi = os.system("sudo i2cget -y 1  0x73 0x00 b" )


Have not tried pycharm or use full python, but most of my stuff is now done remotely.


Offline tohox

  • Newbie
  • *
  • Posts: 25
Re: Remote Python debugging through SSH
« Reply #12 on: August 17, 2016, 06:15:22 AM »
Thanks to Béla's package update which contains a corrected open_ssh script I can now login as root via SSH.

There remained the problem of TC automatically switching to the tc user every time I tried logging in as root. It turns out an easy fix was to rename /root/.picore to something else which prevents this automatic switch.

Thanks everyone for the help!