Graphical environments on Linux can basically be divided in two main groups: full-featured desktop environments such as GNOME, KDE Plasma or XFCE, and barebone, minimalistic window managers, such as i3, openbox, or sway. The former come with a set of applications and utilities designed to work well together, while the latter are meant to perform just one task (or little more): managing windows. When using those environments additional functionalities must be implemented via separate tools.
In this tutorial we talk about some utilties we can use to set the desktop background when using minimal environments, both on X11 and Wayland display servers.
In this tutorial you will learn:
- How to set desktop background on X11 using feh and nitrogen
- How to set desktop background on Wayland using swaybg
|Category||Requirements, Conventions or Software Version Used|
|Conventions||# – requires given linux-commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux-commands to be executed as a regular non-privileged user
Setting desktop background using Feh
Feh is a command line image viewer, but it can also be used to set desktop background in minimal environments based on the X11 display server. Feh is available in the repositories of all the most used Linux distributions. To install it on Fedora we can use the following command:
$ sudo dnf install feh
To install feh on Debian, Ubuntu and derivatives, we can use
$ sudo apt install feh
Feh is also available in the Archlinux “Extra” repository. We can install it with
$ sudo pacman -S feh
Using feh to set the desktop background is pretty easy: all we have to do is to invoke it together with one of the
--bg-*options, depending on how we want the image to be arranged. Most of the time we want to set the background in “fill” mode, so that if the image is smaller than the display, it will be zoomed to fit it, according to its original aspect ratio. To set the background in “fill” mode, we would run:
$ feh --bg-fill /path/to/image.png
Feh supports other background modes: for instance, we can use
--bg-center to center the image we want to use as background, having it surrounded by borders if it is smaller than the screen,
--bg-scale which can be used to make the image fit the screen allowing modifications of its aspect ratio, or
--bg-tile to repeat the image until all the screen surface is covered.
Feh supports also Xinerama and multi-monitor setups, and accepts multiple files as arguments. When we set the desktop background with feh providing a single image as argument, it is applied to all available monitors. If we specify the path of multiple images, however, they are set as backgrounds, in order, on the respective monitors: the first image is used for monitor 0, the second for monitor 1, and so on.
Backgrounds set with feh are not persistent, and will be lost on logout. When feh is used to set a background, however, it automatically creates the
~/.fehbgexecutable script, which can be used to replicate the setup: all we have to do is to make so that it is executed on login. How to do this depends on the window manager we are using. When using i3, for example, it’s enough to add the following line in the
exec --no-startup-id ~/.fehbg
Setting the background using Nitrogen
Unlike feh, Nitrogen is a graphical application written using the GTK2 toolkit. Just like feh it can be used to set backgrounds in minimal environments, and can be installed using our favorite distribution package manager. On Fedora:
$ sudo dnf install nitrogen
On Debian, Ubuntu and derivatives:
$ sudo apt install nitrogen
On Archlinux, instead:
$ sudo pacman -S nitrogen
On first use, Nitrogen interface looks pretty “empty”:
The first thing we want to do is to click on the “Preferences” button, than select one or more directories containing pictures we may want to use as desktop backgrounds. Nitrogen will generate a thumbnail for each image it will find:
To set an image as background we just select its thumbnail and click on the “Apply” button. By using the dropdown menus at the bottom of the interface, we can select how the image should be arranged (Automatic, Scaled, Centered, Tiled, Zoomed or Zoomed fill) and on which monitor it should be applied.
Just like feh, nitrogen doesn’t automatically restore backgrounds on login. It, however, stores the applied setup in the
~/.config/nitrogen/bg-saved.cfgfile. In order to re-apply settings on login, its enough to launch the
nitrogen --restorecommand. Sticking to our i3 example, in the configuration file we would write:
exec --no-startup-id nitrogen --restore
Setting backgrounds using Swaybg
Feh and nitrogen are two really handy tools we can use to set desktop backgrounds when using the X11 window system, but they can’t be used on Wayland. To set the desktop background on window managers designed to work with the new display server, we can use
swaybg, instead. The syntax to use with the program is pretty simple. In the most basic case, we invoke the utility specifying the path of the image we want to use as background as argument to the
$ swaybg -i /path/to/image.png
The mode in which the image will be arranged as background can be passed as argument to the
-moption (short for
--mode). It must be one among: “stretch”, “fit”, “fill”, “center”, “tile”:
$ swaybg -i /path/to/image.png -m fill
The image and settings we specify are, by default, applied to all available monitors. We can, however, apply specific configurations to each monitors. All we have to do is to pass the monitor identifier as argument to the
-o option (
--output): all subsequent settings will be applied only to that monitor. Here is an example. Suppose I am working with two monitors: the first is the notebook native, “eDP-1”, the second is a monitor connected via HDMI, which is recognized as “HDMI-A-2”. To apply a different background to them, I would run:
$ swaybg -o eDP-1 -i /path/to/image0.png -m fill -o HDMI-A-2 -i /path/to/image1.png -m fill
When launching one of the commands used in the examples above in the terminal, you will notice that the process “blocks”, so it needs to be launched in the background: this can be accomplished by appending an ‘&’ to the command. For a new background to be applied an eventual existing instance of swaybg running in the background needs to be terminated. The suggested procedure is to actually set the new background first, than terminate the previous instance, in order to avoid artifacts.
In order to terminate a previous instance of swaybg, we need to know its PID (Process ID). When we launch a long-running command and we send it to background, its PID is stored into the special
$!variable. To retrieve the PID, we can also use the “pidof” utility. All we need to do is to pass the name of the program as argument:
$ pidof swaybg
In my case, the output of the command is:
To terminate the program we can run:
$ kill 82591
By the way, take a look at this tutorial if you want to know more about process management on Linux.
When using minimal graphical environments on Linux, specific utilities must be used to set the desktop background. In this tutorial we learned how to use feh and nitrogen to perform such task when using window managers based on X11, and how to use swaybg when working in minimal environments based on the Wayland display server.