I noticed the other day that one of the Ubuntu Linux systems in our IT center changes theme at sunset and that the background wallpaper changes, not just the window colors. Nice. How can I do that on my own computer?
While light and dark themes have become the standard for most modern operating systems, automating when that switch happens is far less standardized. The best systems offer you a single click to enable having your computer change to dark mode at sunset and back to light mode at sunrise, but while Ubuntu and its GNOME graphical interface offer light and dark themes, there’s not yet any automation for switching.
This means that the IT center has cobbled something together and the easiest solution might be to ask one of them to help you emulate it on your own computer. There’s an extension you can install, but it’s somewhat convoluted because it requires installing some helper utilities beforehand. You can also manage it all through a shell script or two, which I’ll show, but automating that requires you to create cron jobs which is also a bit tricky.
To start, though, let’s look at how GNOME automatically does offer different backgrounds for light and dark themes.
GNOME & LINUX: BACKGROUND BASED ON THEME
On my Ubuntu Linux system running GNOME I have the following background wallpaper, as detailed in the Settings app:
Look closely at the text under the image here in Settings > Background and you’ll see it says “This background selection only applies to the light style.”
A click on Settings > Appearance shows the light/dark theme switch (without, alas, any scheduling or automation):
If I switch to “Dark” style (theme, mode, everyone has a different name for it!) then the background image automatically changes…
Switch back to Settings > Background and I can change this Dark style background image, knowing it will only be used when I have selected Dark mode:
To change it to something else suitable, I’ll find an image that is primarily dark – to stick with the theme – and click. It instantly changes what’s on the main screen too:
If you want the light and dark themes to have the same background wallpaper, you can now switch style, chose the image you desire, and know it’s all set.
AUTOMATING THE LIGHT/DARK THEME SWITCH?
The wrinkle is when you decide you’d like to have this light/dark switch automated. One possibility that’s elegant once you get it properly installed is Night Theme Switcher, which unfortunately doesn’t support Ubuntu GNOME, so if that’s the distro you’re running, it’s not going to work.
Instead, it’s time to utilize the command line. Yes, open up that Terminal and let’s get typing!
To find out the current state of your Desktop theme, uh, style, use this command:
$ gsettings get org.gnome.desktop.interface color-scheme prefer-dark $
That shows that the current setting is “prefer-dark”. To switch it to Light, use “set” instead of “get” and specify that you’d, well, prefer light:
$ gsettings set org.gnome.desktop.interface color-scheme prefer-light $
Note in these examples you only need to type in the bold characters, not the “$” symbol, which is just a prompt.
With this setup, you can easily then create aliases for ‘light’ and ‘dark’ that you can type in to switch to the specified theme:
$ alias light="gsettings set org.gnome.desktop.interface color-scheme prefer-light" $ alias dark="gsettings set org.gnome.desktop.interface color-scheme prefer-dark"
Drop those into your ~/.login file or equivalent to have them available the next time you log in too. Now you can switch by just typing “dark” or “light”. Turn this into a shell script and you can have “switch” which switches from the current mode to the other so you don’t have to remember (by utilizing both comments shown above):
#!/bin/sh # switch.sh - switch light/dark theme in GNOME on Ubuntu Linux # 1. get current state: scheme="$(gsettings get org.gnome.desktop.interface color-scheme)" # 2. switch state: if [ "$scheme" = "'prefer-light'" ] ; then newscheme="prefer-dark" else newscheme="prefer-light" fi echo Switching theme to $newscheme gsettings set org.gnome.desktop.interface color-scheme $newscheme exit 0
With this new script you can automate the process through a cron job, which you can learn more about with ‘man cron’ (or ask your administrator or IT team for assistance).
That’ll get you going with your own solution and you picked up a bit of command line expertise too. Well done!
Pro Tip: I’ve been writing about Linux since the dawn of the operating system, and Unix before that. Please check out my extensive Linux help area and Linux shell script programming area for lots of additional tutorial content while you’re visiting. Thanks!