44

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?

7 Answers 7

61

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
3
  • 1
    I'm going to accept this one since it got me on the right path. For Fedora, the command usermod is used after a user has been created. The group in question was also dialout for me: sudo usermod -a -G dialout <username>. Cheers.
    – iegod
    Jun 3, 2011 at 20:24
  • 8
    Note that you may need to log out and log in to appear in the new group May 25, 2015 at 19:42
  • 6
    By popular demand: sudo usermod -a -G dialout $USER :-) Jul 19, 2018 at 18:06
2

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.

1
  • 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? Apr 12 at 21:34
0

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

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.

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.

3
  • 2
    This answer has been given before.To quote RalfFriedl, “chmod -R 777 /dev is a very bad idea.”  (And the second -R is just wrong.)  To quote unfa, “Giving free access to all users to everything inside /dev/ is extremely unsafe to do.  Please don't do this.” Apr 12 at 21:01
  • Also, the question says "without root" Apr 18 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.
    – Community Bot
    Apr 18 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.

-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!!!

4
  • 9
    chmod -R 777 /dev is a very bad idea.
    – RalfFriedl
    Oct 11, 2018 at 12:58
  • Yaah agree, if there is any other suggestion for this , that will be great Oct 11, 2018 at 13:22
  • I wonder if you needed to log out and back in again, as in the comment on the accepted answer? This often trips people up.
    – sourcejedi
    Oct 11, 2018 at 13:39
  • 2
    Giving free access to all users to everything inside /dev/ is extremely unsafe to do. Please don't do this.
    – unfa
    Mar 4, 2019 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.