47

I'm writing an application to read/write to/from a serial port in Fedora14, and it works great when I run it as root. But when I run it as a normal user I'm unable to obtain the privileges required to access the device (/dev/ttySx). That's kind of crappy because now I can't actually debug the damn thing using Eclipse.

I've tried running Eclipse with sudo but it corrupts my workspace and I can't even open the project. So I'd like to know if it's possible to lower the access requirements to write to /dev/ttySx so that any normal user can access it. Is this possible?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
iegod
  • 495
  • 1
  • 4
  • 4

7 Answers7

67

The right to access a serial port is determined by the permissions of the device file (e.g. /dev/ttyS0). So all you need to do is either arrange for the device to be owned by you, or (better) put yourself in the group that owns the device, or (if Fedora supports it, which I think it does) arrange for the device to belong to the user who's logged in on the console.

For example, on my system (not Fedora), /dev/ttyS0 is owned by the user root and the group dialout, so to be able to access the serial device, I would add myself to the dialout group:

usermod -a -G dialout $USER
Lucas
  • 161
  • 8
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
3

I had an additional step to make to solve my issue.  I came to this question while trying to connect to a micro:bit on port /dev/ttyACM0 using Debian 10.

ls -ld /dev/ttyACM0

displays:

crw-rw-rw- 1 root dialout 166, 0 Jan  5 16:13 /dev/ttyACM0

This indicates why we can access the port using sudo – it is owned by root. It also shows why adding the non-root user to the dialout group allows access without being root.  I added my user account to the dialout group in /etc/group using:

sudo usermod -a -G dialout <user>

I still could not connect with the micro:bit using a serial port monitor without being root.

The micro:bit is mounted under /media:

ls -ld /media/<user>
drwxr-x---+  2 root  root  4096 Jan  5 12:44 <user>

This shows that the directory that the micro:bit is mounted on is owned by root.  I needed to change the owner and group of this directory to <user>:

sudo chowner <user> /media/<user>
sudo chgrp   <user> /media/<user>

Now I can connect to the micro:bit using a serial port monitor without using sudo.

Oppy
  • 131
  • 4
  • Your illustration doesn’t illustrate much. `rw-rw-rw-` is 666, which means that everybody should be able to read from and write to the device. And what is `chowner`? And how is a serial port “mounted” under `/media`? – G-Man Says 'Reinstate Monica' Apr 12 '22 at 21:34
0

Many distributions use group memberships to enable access to serial ports. I don't know details for Fedora offhand, though.

geekosaur
  • 31,429
  • 5
  • 79
  • 58
0

I think you can add yourself in sudoers file which will allow you to specify a set of commands as command alias which you would be able to execute without password. You can find an excellent tutorial about sudoers file in Ubuntu docs.

You can then run eclipse normally and it'd be able to execute those specific commands without root permissions.

Deepak Mittal
  • 1,544
  • 13
  • 18
0

I had the same issue using VSCode PlatformIO trying to connect to an ESP32.  Using

sudo chmod -R 777 /dev -R

fixed the issue.

  • 2
    [This answer has been given before.](https://unix.stackexchange.com/q/14354/80216#474824)  [To quote RalfFriedl](https://unix.stackexchange.com/questions/14354/read-write-to-a-serial-port-without-root/698669#comment867326_474824), “`chmod -R 777 /dev` is a very bad idea.”  (And the second `-R` is just wrong.)  [To quote unfa](https://unix.stackexchange.com/questions/14354/read-write-to-a-serial-port-without-root/698669#comment930586_474824), “Giving free access to all users to everything inside `/dev/` is extremely unsafe to do.  Please don't do this.” – G-Man Says 'Reinstate Monica' Apr 12 '22 at 21:01
  • Also, the question says "*without* root" – rubynorails Apr 18 '22 at 19:13
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 18 '22 at 19:14
0

Many device access problems can be resolved through group membership changes.

You can find the device name by watching sudo journalctl --follow as you connect your device. OR ls -1 /dev >dev.before, connect the device, wait 10 seconds, ls -1 /dev >dev.after;diff dev.{before,after}.

Specifically, if ls -l shows that the group permissions (the second "rwx" triplet) is "rw" (e.g."-rw-rw----"), then, adding oneself to the group that owns the device will grant rw access.

Here's how:

# change to your device name 
device="/dev/ttyS0"
sudo adduser $USER $(stat -c "%G" $device)

This allows you membership in the group that can rw the device, but there is one more step.

To make all your processes members of the new group, logout and login. Group memberships are set up at login time.

To create a single process in the new group (for testing, prior to logout/login):

newgrp $(stat -c "%G" $device)  

or, just type the group name. See man newgrp.

waltinator
  • 4,439
  • 1
  • 16
  • 21
-4

i had this problem also back in the day, apart of adding user to a dialout group you need also to give the permission for that user to access /dev. simply su -> enter root password -> chmod -R 777 /dev -R, means recursive mode, everything within that folder will have the same permission

the problem is this, you need to do that every time you reboot your computer or plug in and out your device huh!!!