Ubuntu: Write to serial port: permission denied

I've connected a USB to Serial bridge dongle, and running dmesg | grep tty outputted the following:

[    0.000000] console [tty0] enabled
[603199.380677] usb 2-2: cp210x converter now attached to ttyUSB0

So now I'm trying to write to it as per the answer in this previous question by running the following:

cat hello.txt > /dev/ttyUSB0

and

sudo cat hello.txt > /dev/ttyUSB0

But both result in the following error:

bash: /dev/ttyUSB0: Permission denied

What am I doing wrong?

Thanks


Solution 1:

If you still want to use sudo to access the port, the issue is that cat is running with root privileges, but the redirection isn't. Try cat hello.txt | sudo tee /dev/ttyUSB0. This uses the tee tool, run as root. It outputs both to stdout (the terminal), and to the specified destination (in this case, the serial port).

Solution 2:

By default as I recall serial ports on Ubuntu belong to the "dialout" group. You can add yourself to this group by running something like the following:

sudo gpasswd --add jodes dialout

You may need to log out and log back in before this takes effect, but after doing so you should be able to read and write from and to your serial ports including USB to serial converters.