67

I have an Ubuntu 11.10 system with 2 users:

  • The first was created during the installation
  • The second instead was created after. It belongs to the sudoers group.

Now the problem is that when the second tries to use a device ttyUSB0 the following error is returned:

"Could not open serial port /dev/ttyUSB0"

I was able to fix it by using:

sudo chown :second_user /dev/ttyUSB0

However when I disconnect the device and reconnect it the problem comes back.

Is there a way to allow different users to access the devices? I suppose I have to add the user to a specific group. Currently the owner is root and the group is dialout. However I'm not sure about the group and I don't know how to add the user.

Thanks!

7 Answers 7

94

As you've noticed, the /dev/ttyUSB0 device has the group of dialout. All you need to do is add the second user to the dialout group:

sudo adduser second_user dialout

second_user will need to log out & log back in again for this to take effect.

5
  • Thanks Jeremy! Does this work also for ssh access? Because when I access the pc via ssh I see two users with the same name. I suppose that one is the local user and the other the ssh one. How can I give the ownership to the ssh user?
    – Maverik
    Mar 14, 2012 at 15:44
  • 1
    This will allow second_user to access the serial port from any login, ssh or local. You don't need to assign ownership to a particular login of that user. Mar 15, 2012 at 1:01
  • 2
    Yes, you are right. I had to logout and then login again to have the access vis ssh.
    – Maverik
    Mar 15, 2012 at 7:43
  • For sessions of a user that was automatically logged in, you need to reboot the computer, not just to re-login to X Window System. Observed with Ubuntu 19.04.
    – tanius
    May 1, 2019 at 14:52
  • 6
    sudo usermod -a -G dialout $USER Jul 18, 2022 at 12:28
28

The easy way:

sudoedit /etc/udev/rules.d/50-myusb.rules

Save this text:

KERNEL=="ttyUSB[0-9]*",MODE="0666"
KERNEL=="ttyACM[0-9]*",MODE="0666"

Unplug the device and replug it, and it should be read/write from any user!

4
  • Works in 14.04 for home-made device in ttyACM socket. Thanks. +1 May 10, 2016 at 18:35
  • Don't you need to do this before the replug to reload the rules? sudo udevadm control --reload ? Aug 28, 2019 at 7:16
  • Depending on which filesystem stores your /etc/udev, and which version of udev you have, you might indeed need to reload explicitly. But on most machines I've used, udev seems to figure it out automatically (using inotify). Aug 30, 2019 at 7:41
  • 1
    That's really not how it's supposed to be done. USB serial devices should belong to the dialout group or similar, and the user should be added to that. What you are proposing would make sense for other types of USB devices however Oct 25, 2019 at 18:02
8

You could use UDEV. It's a system that triggers every time plug or unplug a device (amongst other stuff). With it, you script various things to happen, including setting permissions.

Run sudoedit /etc/udev/rules.d/50-ttyusb.rules and stick this in there:

KERNEL=="ttyUSB[0-9]*",NAME="tts/USB%n",SYMLINK+="%k",GROUP="uucp",MODE="0666"

Save, exit and replug and you should be up and running. Setting the permission to 666 allows anybody to write to the device.

I'm basing this off this page which is from a few years ago but something like this should work if Jeremy's solution doesn't.

1
  • 1
    This worked for me, that is, I used the udev rules, but I changed the syntax to match my own case. linux.m2osw.com/sane-cannot-find-any-scanners -- note that if you use MODE="0666" then you probably don't need the GROUP. Only one or the other would be useful. Mar 11, 2014 at 3:18
5

Fantastic -- the UDEV solution given here was the ticket for me.

I installed Icom's CS-F3020_F5010_F5020 program via Wine, created the link for the Com port as follows:

ln -s /dev/ttyUSB0 ~/.wine/dosdevices/com1 but nothing. 

Then I realised I needed to change permissions on /dev/ttyUSB0 to allow me to access it. That works great until you unplug/replug the USB in, then you need to re-change the permissions.

I tried adding my user to dialout group but this didn't solve the problem for some reason.

Using UDEV solves the final piece of the puzzle. Now I can program my Icom radio using Linux, plug and unplug the USB/Serial device without any more fuss. Woohoo. Thanks.

4

The udev rules work but as written they have the very nasty side effect of making all ttyUSB* devices accessible to everyone. This is not good because that may be a security risk depending on what else is on the system.

Instead use a more selective udev rule. For example I have a USB device that operates a set of switches. From dmesg when it is plugged in I can see the ID of the manufacturer and the product code (plus, even, in this case, a serial number for the device). I can add:

ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001"

to the kernel lines above and the rule will only apply to that device. Even better rather than assigning to the MODE variable alone set the group too:

GROUP="whatever", MODE="0660"

then only people in group 'whatever' will get write access.

John Bowler

0

You can also use chmod (I did it on Raspberry linux).

In my case, sudo adduser pi dialout didn't work ("The user 'pi' is already a member of 'dialout'", but non root app still didn't have access to serial).

See its linkage (/dev/serial0 -> /dev/ttyS0):

pi@raspberrypi:~ $ ls -al /dev/serial*
lrwxrwxrwx 1 root root 5 Dec 30 18:03 /dev/serial0 -> ttyS0
lrwxrwxrwx 1 root root 7 Dec 30 18:03 /dev/serial1 -> ttyAMA0
pi@raspberrypi:~ $ ls -al /dev/ttyS*
crw--w---- 1 root tty 4, 64 Jan  7 16:07 /dev/ttyS0

And update read/write permission for all users:

pi@raspberrypi:~ $ sudo chmod ugo+rw /dev/ttyS0

Now user code (e.g. 'pi' user dotnet core application) can access /dev/serial0.

0
sudo usermod -a -G dialout $USER

Then have that user log out and log back in. The command groups will now show dialout as one of the options.

Now you can use serial terminals such as picocom (my favorite), without sudo:

picocom --baud 115200 /dev/ttyUSB0

Exit picocom with Ctrl + A, then Ctrl + X.

References:

Modified from my personal notes here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/blob/master/useful_apps/serial_terminals_README.md#installation

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .