Ubuntu: Write to serial port: permission denied

All we need is an easy explanation of the problem, so here it is.

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

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 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).

Method 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.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply