snupis
Posts: 1
Joined: Sun Mar 22, 2020 4:55 am

Serial Port Permissions

Sun Mar 22, 2020 2:33 pm

We’re having permission issues reading and writing from our Raspberry Pi Zero W’s serial port. We’re trying to use the mh-z19 Python module, but the problem can be pinpointed to starting the serial-getty service. When the service is started the permissions of the serial device change(group name and permissions). Please see below for a minimal example:

Code: Select all

pi@raspberrypi:~ $ ls -l /dev/serial0 /dev/ttyAMA0
lrwxrwxrwx 1 root root          7 Mar 22 03:56 /dev/serial0 -> ttyAMA0
crw-rw---- 1 root dialout 204, 64 Mar 22 03:56 /dev/ttyAMA0
pi@raspberrypi:~ $ sudo systemctl start serial-getty@serial0.service
pi@raspberrypi:~ $ ls -l /dev/serial0 /dev/ttyAMA0
lrwxrwxrwx 1 root root       7 Mar 22 03:56 /dev/serial0 -> ttyAMA0
crw--w---- 1 root tty  204, 64 Mar 22 03:58 /dev/ttyAMA0
pi@raspberrypi:~ $ sudo systemctl stop serial-getty@serial0.service
pi@raspberrypi:~ $ ls -l /dev/serial0 /dev/ttyAMA0
lrwxrwxrwx 1 root root       7 Mar 22 03:56 /dev/serial0 -> ttyAMA0
crw--w---- 1 root tty  204, 64 Mar 22 03:58 /dev/ttyAMA0
pi@raspberrypi:~ $ sudo systemctl is-active serial-getty@serial0.service
inactive
The /boot/config.txt file contains:

Code: Select all

dtoverlay=disable-bt
enable_uart=1
When running

Code: Select all

sudo raspi-config
it indicates “The serial login shell is disabled. The serial interface is enabled.”

We want to be able to use the serial port as the user pi (who is part of the dialout group). This works fine until the getty service gets started for the first time which then changes the permissions and prevents further readings as the user pi, regardless of whether the service is stopped. How do we prevent `/dev/ttyAMA0`’s permissions from being changed when starting a service? We also tried with serial0 pointing to /dev/ttyS0 before disabling bluetooth, but it gave the exact same problem.

It’s our first time working with serial ports, so it may be something obvious we’re missing, but searching the internet hasn’t yielded any solutions.

Many thanks in advance!

hadur
Posts: 2
Joined: Sat Apr 11, 2020 11:41 pm

Re: Serial Port Permissions

Sun Apr 12, 2020 11:41 am

Hi!
NO solution here, I have similar problem.
The settings are exactly the same like in snupis machine.
This is what happens all the time.
I set up the port with chmod (my user is in the tty, dialup groups) and after a few seconds its changed back to its original permission by 'whoknows'.

Code: Select all

hadur@raspi:/srv/uart2 $ ls -l /dev/serial0 /dev/ttyAMA0
lrwxrwxrwx 1 root root       7 ápr   11 23:37 /dev/serial0 -> ttyAMA0
crwxrwxrwx 1 root tty  204, 64 ápr   12 13:17 /dev/ttyAMA0
hadur@raspi:/srv/uart2 $ python test2.py 
0R2,Ta=023.0C,Ua=034.6P

hadur@raspi:/srv/uart2 $ python test2.py 
Traceback (most recent call last):
  File "test2.py", line 13, in <module>
    timeout=1
  File "/home/hadur/.local/lib/python2.7/site-packages/serial/serialutil.py", line 240, in __init__
    self.open()
  File "/home/hadur/.local/lib/python2.7/site-packages/serial/serialposix.py", line 268, in open
    raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 13] could not open port /dev/ttyAMA0: [Errno 13] Permission denied: '/dev/ttyAMA0'
hadur@raspi:/srv/uart2 $ ls -l /dev/serial0 /dev/ttyAMA0
lrwxrwxrwx 1 root root       7 ápr   11 23:37 /dev/serial0 -> ttyAMA0
crw--w---- 1 root tty  204, 64 ápr   12 13:19 /dev/ttyAMA0
hadur@raspi:/srv/uart2 $ sudo  chmod 777  /dev/ttyAMA0
hadur@raspi:/srv/uart2 $ python test2.py 
0R2,Ta=023.0C,Ua=034.6P

hadur@raspi:/srv/uart2 $ python test2.py 
0R2,Ta=023.0C,Ua=034.6P

hadur@raspi:/srv/uart2 $ python test2.py 
0R2,Ta=023.0C,Ua=034.6P

hadur@raspi:/srv/uart2 $ python test2.py 
Traceback (most recent call last):
  File "test2.py", line 13, in <module>
    timeout=1
  File "/home/hadur/.local/lib/python2.7/site-packages/serial/serialutil.py", line 240, in __init__
    self.open()
  File "/home/hadur/.local/lib/python2.7/site-packages/serial/serialposix.py", line 268, in open
    raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 13] could not open port /dev/ttyAMA0: [Errno 13] Permission denied: '/dev/ttyAMA0'
hadur@raspi:/srv/uart2 $ 
hadur@raspi:/srv/uart2 $ ls -l /dev/serial0 /dev/ttyAMA0
lrwxrwxrwx 1 root root       7 ápr   11 23:37 /dev/serial0 -> ttyAMA0
crw--w---- 1 root tty  204, 64 ápr   12 13:20 /dev/ttyAMA0
hadur@raspi:/srv/uart2 $
os

Code: Select all

hadur@raspi:/srv/uart2 $ uname -a
Linux raspi 4.19.97-v7+ #1294 SMP Thu Jan 30 13:15:58 GMT 2020 armv7l GNU/Linux $ 


So as the problem, the question is the same as the OP's question.

hippy
Posts: 7729
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Serial Port Permissions

Sun Apr 12, 2020 12:56 pm

hadur wrote:
Sun Apr 12, 2020 11:41 am
I set up the port with chmod (my user is in the tty, dialup groups) and after a few seconds its changed back to its original permission by 'whoknows'.
You should not have to 'chmod' the port. I have experienced issues when adding groups for users and would recommended logging out and back in, or rebooting, after doing that.

User avatar
bensimmo
Posts: 4622
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Serial Port Permissions

Sun Apr 12, 2020 2:02 pm

Why are you starting serial-getty?

You didn't mention that you had disable hciuart, since you've disabled BT.
sudo systemctl disable hciuart

hadur
Posts: 2
Joined: Sat Apr 11, 2020 11:41 pm

Re: Serial Port Permissions

Mon Apr 13, 2020 10:20 am

bensimmo wrote:
Sun Apr 12, 2020 2:02 pm
Why are you starting serial-getty?

You didn't mention that you had disable hciuart, since you've disabled BT.
sudo systemctl disable hciuart
I am not using it.
hippy wrote:
Sun Apr 12, 2020 12:56 pm
hadur wrote:
Sun Apr 12, 2020 11:41 am
I set up the port with chmod (my user is in the tty, dialup groups) and after a few seconds its changed back to its original permission by 'whoknows'.
You should not have to 'chmod' the port. I have experienced issues when adding groups for users and would recommended logging out and back in, or rebooting, after doing that.
First time tried to set up the groups for my user. Rebooted and I was unable to use them still.
My last chance was to change permissions, but something allways resetting permissions back to original.
I have to communicate with another device through this serial port and cannot use sudo with it.

What is left to do ?

User avatar
bensimmo
Posts: 4622
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Serial Port Permissions

Mon Apr 13, 2020 11:50 am

is was talking about the original poster...
disable both the Getty (settings should have done this when disabling console) and then also hciuart if disabling BT
if you are running as Pi then there is no need to mess with permissions?

Return to “Troubleshooting”