Run Qt project as root
-
wrote on 5 Jul 2021, 14:45 last edited by
Hello, I have a project where I need to connect to the
/dev/ttyUSB0
port. For that, I do the following:void WTemperature::sendDataRequest() { QSerialPort *port = new QSerialPort; port->setPortName("/dev/ttyUSB0"); port->setBaudRate(230400); port->setParity(QSerialPort::NoParity); port->setStopBits(QSerialPort::OneStop); port->setDataBits(QSerialPort::Data8); if(!port->open(QIODevice::ReadWrite)) qDebug() << "open port error" << port->error(); else { //send request const QByteArray writeData(4, '0'); port->write(writeData); port->close(); } delete port }
The code itself compiles and runs but apparently I need root privileges to access the USB port. I get the following error every time:
open port error QSerialPort::PermissionError
I then checked the
Run as user root
box in the Projects menu of the Mode Selector, but then the error I got has been this:No protocol specified qt.qpa.xcb: could not connect to display :0 qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl, xcb.
I have looked at many forums for an answer but none seem to work because this error only appears when I try to run as root. Otherwise the code would run and others forum questions would have helped.
I have also tried this Qt example
https://doc.qt.io/qt-5/qtserialport-cwriterasync-example.html
The problem is that I need to run withsudo
or else I get this error:QIODevice::write (QSerialPort): device not open Failed to write the data to port ttyUSB0, error: Permission denied Operation timed out for port ttyUSB0, error: Permission denied
It is clear to me that the problem is the permission. My question is, is it possible to run a project as root? I hope it is more complex that just do a chmod in /dev/ttyUSB0 (I have not tried because I didn't want to mess up with the USB folder in root).
By the way, I am using Qt 5.15.2, if you need more information about my code, feel free to ask. This is my first post ever so I may have forgotten to add obvious pieces of information. -
Hello, I have a project where I need to connect to the
/dev/ttyUSB0
port. For that, I do the following:void WTemperature::sendDataRequest() { QSerialPort *port = new QSerialPort; port->setPortName("/dev/ttyUSB0"); port->setBaudRate(230400); port->setParity(QSerialPort::NoParity); port->setStopBits(QSerialPort::OneStop); port->setDataBits(QSerialPort::Data8); if(!port->open(QIODevice::ReadWrite)) qDebug() << "open port error" << port->error(); else { //send request const QByteArray writeData(4, '0'); port->write(writeData); port->close(); } delete port }
The code itself compiles and runs but apparently I need root privileges to access the USB port. I get the following error every time:
open port error QSerialPort::PermissionError
I then checked the
Run as user root
box in the Projects menu of the Mode Selector, but then the error I got has been this:No protocol specified qt.qpa.xcb: could not connect to display :0 qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl, xcb.
I have looked at many forums for an answer but none seem to work because this error only appears when I try to run as root. Otherwise the code would run and others forum questions would have helped.
I have also tried this Qt example
https://doc.qt.io/qt-5/qtserialport-cwriterasync-example.html
The problem is that I need to run withsudo
or else I get this error:QIODevice::write (QSerialPort): device not open Failed to write the data to port ttyUSB0, error: Permission denied Operation timed out for port ttyUSB0, error: Permission denied
It is clear to me that the problem is the permission. My question is, is it possible to run a project as root? I hope it is more complex that just do a chmod in /dev/ttyUSB0 (I have not tried because I didn't want to mess up with the USB folder in root).
By the way, I am using Qt 5.15.2, if you need more information about my code, feel free to ask. This is my first post ever so I may have forgotten to add obvious pieces of information.@diego-qt You should rather add your user to the group which is set on the device file (ttyUSB0), then the user should be able to access it.
The problem you see if you run as root is because by default root (with sudo or su) has no access to X11 server for security reasons (this is not Qt issue). -
wrote on 6 Jul 2021, 07:37 last edited by
I don't think that's the problem because I am able to run the example I mentioned (https://doc.qt.io/qt-5/qtserialport-cwriterasync-example.html) with root privileges (aka
sudo ./cwriteasync <...>
).
There must be something I'm doing differently. -
I don't think that's the problem because I am able to run the example I mentioned (https://doc.qt.io/qt-5/qtserialport-cwriterasync-example.html) with root privileges (aka
sudo ./cwriteasync <...>
).
There must be something I'm doing differently.Lifetime Qt Championwrote on 6 Jul 2021, 07:48 last edited by jsulm 7 Jun 2021, 07:48@diego-qt That example is a command line tool without GUI as far as I can see. So, it does not need X server.
Your app is a GUI application which requires the X server. So, it IS the reason... -
@diego-qt That example is a command line tool without GUI as far as I can see. So, it does not need X server.
Your app is a GUI application which requires the X server. So, it IS the reason...wrote on 6 Jul 2021, 08:53 last edited by@jsulm Ok, I see. It's true that the Qt example is a console application. But I find it difficult to understand the relation between X11 and my application.
If I got it right, my application needs access to X11 because, since it is a GUI, it automatically runs a X Window server? A GUI application will always be related to X? Sorry I didn't know the existence of X before yesterday :/ -
@jsulm Ok, I see. It's true that the Qt example is a console application. But I find it difficult to understand the relation between X11 and my application.
If I got it right, my application needs access to X11 because, since it is a GUI, it automatically runs a X Window server? A GUI application will always be related to X? Sorry I didn't know the existence of X before yesterday :/@diego-qt said in Run Qt project as root:
it automatically runs a X Window server?
No, the X server is already running when you're logging in on your machine. If you then start your app it connects to the X server.
"A GUI application will always be related to X?" - it depends. X is still the standard in Linux/UNIX but Wayland is taking over. By default Qt applications use X server on Linux.
Did you check which group is set on /dev/ttyUSB0? You could simply add your user to that group, then there is no need for root. -
@diego-qt said in Run Qt project as root:
it automatically runs a X Window server?
No, the X server is already running when you're logging in on your machine. If you then start your app it connects to the X server.
"A GUI application will always be related to X?" - it depends. X is still the standard in Linux/UNIX but Wayland is taking over. By default Qt applications use X server on Linux.
Did you check which group is set on /dev/ttyUSB0? You could simply add your user to that group, then there is no need for root.wrote on 6 Jul 2021, 09:31 last edited by@jsulm the group is dialout, but I'm being told that it might not be the best way to solve the problem because what I need is that the application runs without the need to adjust specific parameters in every PC that will run the program. Isn't there a way to automatically access /dev/ttyUSB0 ?
-
@jsulm the group is dialout, but I'm being told that it might not be the best way to solve the problem because what I need is that the application runs without the need to adjust specific parameters in every PC that will run the program. Isn't there a way to automatically access /dev/ttyUSB0 ?
@diego-qt said in Run Qt project as root:
what I need is that the application runs without the need to adjust specific parameters in every PC that will run the program.
It is even worse to ask users to run the app as root!
Especially if it requires X server which does not allow access for applications running with sudo/su - you would need to change the configuration on every PC to make this work!
So, my advice is still the same: add the users to the group dialout, this is the correct way to solve this... -
@jsulm the group is dialout, but I'm being told that it might not be the best way to solve the problem because what I need is that the application runs without the need to adjust specific parameters in every PC that will run the program. Isn't there a way to automatically access /dev/ttyUSB0 ?
wrote on 6 Jul 2021, 16:00 last edited by JonB 7 Jun 2021, 16:01@diego-qt said in Run Qt project as root:
the group is dialout, but I'm being told that it might not be the best way to solve the problem because what I need is that the application runs without the need to adjust specific parameters in every PC that will run the program. Isn't there a way to automatically access /dev/ttyUSB0 ?
No, there isn't an "automatic way". The device has permissions on it. It (apparently) requires membership of group dialout to access. Permissions are permissions, and they are there for a reason! So the running user should be a member of that group. (Or, you change the permissions on the device to allow access by any user, regardless of group.) I don't know who's saying you should not have to alter anything to achieve that...!
In case anyone comments. Linux programs can be run "setuid" or "setgid" to grant those permissions. That could apply to your executable. However, those bits must be set on the executable after distribution/installation, so it still requires changes on the target machine.
As @jsulm says
So, my advice is still the same: add the users to the group dialout, this is the correct way to solve this...
-
wrote on 7 Jul 2021, 08:10 last edited by
Ok then, thank you for this clarification. I will follow your advice.
1/10