How do I allow a non-default user to use serial device ttyUSB0?

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!


Solution 1:

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.

Solution 2:

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!

Solution 3:

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.

Solution 4:

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.

Solution 5:

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