Apps with root privileges not scaled

Hello.
I am runing Ubuntu 20.04 with Mate Desktop. But same problem I had with older version of ubuntu.
Everything is fine except one thing.
When I run some programs as root, they are not scalled. (MATE TWEAK - HiDPI or Preferences->Appearance->Fonts)
I am using Krusader, and if I need to run it as root (via menu Tools->"Start Root Mode Krusader" or via terminal). It has very small fonts (not scalled).
If I run konsole, it is OK. When I run sudo konsole, it is not scalled.
And many more.

Can someone help me please?

1 Like

Both "MATE TWEAK - HiDPI" and "Preferences->Appearance->Fonts" are bound to the preferences of the user and not systemwide

So if you want to have HiDPI as root, you probably will have to set it while you are root. :slight_smile:

I know (from experience) that the same works for the colors of pluma.
I assume that this also would work for HiDPI and fontsize (at least that seems logical to me).

Close all instances of Konsole that you have currently running (if any), then try launching the Konsole using this command:

sudo env QT_SCALE_FACTOR=$QT_SCALE_FACTOR konsole

If that gives you nice and big fonts, you can apply the fix permanently by running:

sudo visudo

...and then adding the following line of text to the file, right after the block of lines near the top of the file that start with Defaults [...]:

Defaults        env_keep += "QT_SCALE_FACTOR"

When you're done, the top of the file should look something like this:

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Defaults        env_keep += "QT_SCALE_FACTOR"

Then save the file, exit the text editor, and try running Konsole / Krusader as sudo konsole / sudo krusader again.


I'm sure the commands above look like a bunch of spooky mumbo-jumbo, so if you want to know more, read on.

First of all, make no mistake: The behavior you've witnessed is a bug, and it should be fixed. You've got my support there.

OK, down to business: Most applications on Ubuntu MATE use a program code library called GTK+ to do the job of drawing stuff on the screen. (This is how so many applications can look so consistent -- i.e. everything uses the same theme and looks similar.) GTK+ needs to know some settings -- how big to make the fonts, the "scaling" or pixel density of the monitor screen, what theme to use, how long to wait for a double-click, et cetera. Normally (and this is what happens here), when GTK+ needs to query these settings, it contacts a background program called the "settings manager" or "settings daemon". The settings manager then tells GTK+ all the settings it needs to know.

However, Konsole and Krusader don't use GTK+ -- they use another popular library called Qt to do the same kinds of things that GTK+ does. This by itself is not a problem, but Qt doesn't query settings the same way GTK+ does -- especially nowadays. Qt is designed to run on myriad operating systems (including ones that are not Linux, such as Windows); as such, Qt by itself doesn't query any kind of settings manager. Qt has to rely on other means to check your settings, such as "environment variables" (text passed to a program by the program that launched it, a.k.a its "parent process").

Well, actually, it's not entirely true that Qt doesn't contact your settings manager. If you have an extra add-on to Qt installed that makes Qt fit in better with GTK+ (if I recall, installing the packages qt5-style-plugin-gtk2 and qt5-style-platform-gtk2 would do the trick), then Qt will adopt the font sizes and other settings that GTK+ uses, and thus your Qt applications will have readable text that way, too. The only problem with that method is that I think that those packages I just mentioned are either deprecated or entirely removed in the latest releases of Ubuntu (MATE) -- and because of that, either way, you might have difficulty getting them to work. I just don't know.

Even without the extra Qt add-ons, environment variables still work. The MATE desktop sets a special environment variable called QT_SCALE_FACTOR when you log in. This variable usually contains either a numeral 1, 2, or (rarely) a 3. Qt applications test the value of this variable when they start up: If it's 1, Qt does no special scaling (because you have a non-HiDPI monitor). If it's 2, Qt doubles the size of everything it draws onscreen (this is the usual setting for HiDPI monitors); if it's 3, it triples the size of everything onscreen (for uncommon, ultra-hi-resolution monitors). And so on.

So why didn't Qt applications run as sudo have scaled fonts? Well, unfortunately some environment variables can be dangerous to carry over to a program run as sudo. For example, if you ran a program with sudo and sudo carried over, say, your PATH variable (which contains a list of directories to search for executable programs, in order), theoretically, someone malicious could come along, change your PATH variable as a regular user (your user, that is), and when you went to run something else as sudo, the malicious user's directories of choice would be searched for programs to execute. What if you ran sudo ls to list the contents of some directory, only to accidentally run the attacker's version of the program which actually deletes files, for instance? This may sound a bit far-fetched, but you never know what will happen, and for this reason sudo clears out almost all environment variables before it executes any program as root.

So how does Qt fit into all this? Well, a Qt program running as your normal user can read the QT_SCALE_FACTOR variable, no problem -- so it scales the text it displays accordingly. But when you run a program as sudo, sudo wipes out your environment variables, including QT_SCALE_FACTOR -- so Qt applications run under sudo default to a scale factor of 1, which isn't enough for your screen, clearly.

So the commands I showed you above add a special exception for the QT_SCALE_FACTOR variable, so that sudo will still wipe out most of your environment variables (for safety) but not QT_SCALE_FACTOR. Now Qt applications will know that you have a HiDPI screen, even applications run as sudo. Now, applications should display text full size.


I'd better stop talking before I wear my fingertips down typing on these keys!

BTW, I hope I didn't insult you by explaining too much.

4 Likes

Sorry for late reply.

THANK YOU very much.
It works, and thank you for the explanation. It was sufficient and understandable.

Best regards.

Hello tkn, that doesn't work. It was first thing, what came to my mind.

You are completely right, I forgot to mention that this only works for GTK and Gnome applications. Krusader is KDE/Qt. I'm glad that @gordon covered that part. :slight_smile: