When I run any GUI application on Linux (Ubuntu) using sudo command from the Terminal, the application fails with QT error as below.

QT application on sudo

Almost every application fails when attempted; here is an example for firefox

firefox on sudo

As sudo/root doesn't have DISPLAY by default, On setting the display this works Ref: Running GUI application as another (non-root) user

Is it possible to identify that the application doesn't have display and set this programmmatically?


1 Answer 1


You would simply tell sudo to forward the existing environment variable into the process it's spawning as root. See the --preserve-env=list option in man sudo.

Also, running firefox as root gets, no questions asked, a very harsh

No. Don't do that.

There's positively no reason to run a browser as root, and whatever you're trying to solve that way must be solved a different way.

Same, also, for cmake and cmake-gui. Software should not be built as root, and if you have to become root to build it, something has gone wrong. And even if you had to build software as root (you don't, but some software is badly written), you do not have to run cmake or cmake-gui as root.

So, bad use cases for sudo.

  • Thank you Marcus. This is helpful. Adding additional explanation: The usecase is we are building a desktop GUI application which would be run by users from GUI Linux systems. If the application is run without sudo, it comes up properly. If the application is triggered using sudo it gives QT errors saying no display. Instead of asking users to set the display manually when they run the application as sudo, is there a programmatic way in C to identify this, set the display and then bring up the application.
    – Vijesh K
    Jan 19 at 5:34
  • the solution is to prohibit running a user application as root. It's really that simple! The solution is to let the user run the application as non-root, and use policykit to acquire the root privileges at runtime, rather than starting it using sudo. Jan 19 at 11:44
  • Thank you @Marcus. This helps. (1) We do want to run the program as root when we run it as a daemon (2) Also, if I log into a GUI enabled root session, the same program can be launched (Agree, we would want to recommend to run as a user always). (3) If the application is launched using su and provided with a DISPLAY it works. Can this be identified programmatic-ally that we are launched using su but without display and thus, we need to set it by ourselves (like, $export DISPLAY=:0.0) and then run.
    – Vijesh K
    Feb 14 at 5:45
  • you never run firefox as root, and firefox as daemon: are you using geckodriver?! A daemon with a GUI makes no sense, and a daemon with GUI that has to run as root even less so. Again, the problem you're solving HAS to have a better solution than this. Feb 14 at 8:14
  • We are writing a desktop application which has a GUI. We are able to run it as sudo/root by providing DISPLAY. Now, I am thinking Firefox is a wrong example to share. Our application can also daemonize itself by forking out when it is launched as daemon; it won't have the GUI when it becomes daemon. The challenge for us is to run in GUI mode when sudo needs to be identified programmatically and we attempt to set the display and then run.
    – Vijesh K
    Feb 15 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.