I am trying to change the default display-manager on a Ubuntu 18.04.1 in a non-interactive way to do this in a script.
Everything works as expected when using
dpkg-reconfigure gdm3dpkg-reconfigure lightdm
I tried setting shared/default-x-display-manager to a valid display-manager and run dpkg-reconfigure, but that did not do the trick.
First check which display-manager is set.
root@host:~# cat /etc/X11/default-display-manager
/usr/sbin/gdm3
root@host:~# cat /etc/X11/default-display-manager
/usr/sbin/gdm3Then set shared/default-x-display-manager to lightdm and validate it.
root@host:~# echo set shared/default-x-display-manager lightdm | debconf-communicate
0 value set
root@host:~# echo get shared/default-x-display-manager | debconf-communicate
0 lightdmFinally run dpkg-reconfigure --frontend noninteractive lightdm and check which display-manager is active. Which unfortunately is gdm3 again.
root@host:~# dpkg-reconfigure --frontend noninteractive lightdm
root@host:~# echo get shared/default-x-display-manager | debconf-communicate
0 gdm3
root@host:~# cat /etc/X11/default-display-manager
/usr/sbin/gdm3
root@host:~# ll /etc/systemd/system/display-manager.service
lrwxrwxrwx 1 root root 32 Jan 31 20:03 /etc/systemd/system/display-manager.service -> /lib/systemd/system/gdm3.serviceThe same happens if I want to switch from lightdm to gdm3.
Anything I am missing here or is this not intended to work at all?
I have seen in the scripts for gdm3 that if there is a /etc/X11/default-display-manager.debconf-update file, the systemd unit and the /etc/X11/default-display-manager are updated accordingly.
Unfortunately this does not work for lightdm as this one uses some other method(s) to do the work.
Hope someone else can shed some light on this and knows a convenient way to configure the display-manager non-interactively.
2 Answers
After reading up the .post*, '.pre*' and .config scripts of gdm3 and lightdm I have found a way to run dpkg-reconfigure interactively to change between the display managers and keep the magic of the mentioned pre, post and config scripts.
All you have to do is to update the /etc/X11/default-display-manager file with the display manager binary you want to change to. Then run dpkg-reconfigure non-interactively and you are done. Well, gdm3 does not update the debconf database, whereas lightdm does, but that is OK to do that manually.
Here we go...
Change from lightdm to gdm3.
$ echo "/usr/sbin/gdm3" > /etc/X11/default-display-manager
$ DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true dpkg-reconfigure gdm3
$ echo set shared/default-x-display-manager gdm3 | debconf-communicateChange from gdm3 to lightdm.
$ echo "/usr/sbin/lightdm" > /etc/X11/default-display-manager
$ DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true dpkg-reconfigure lightdm
$ echo set shared/default-x-display-manager lightdm | debconf-communicateAs already mentioned the last step echo set shared/default-x-display-manager lightdm | debconf-communicate is not really necessary here as the scripts of lightdm will take care of it. But for the sake of a simplified script it does not hurt to set it.
Putting things together into a shell script could be as follows. One could add more control if needed.
$ cat set_dm.sh
#!/bin/bash
set_dm() { DISPLAY_MANAGER="gdm3" DISPLAY_MANAGER_SERVICE="/etc/systemd/system/display-manager.service" DEFAULT_DISPLAY_MANAGER_FILE="/etc/X11/default-display-manager" if [ -n "${1}" ] then DISPLAY_MANAGER="$1" fi DISPLAY_MANAGER_BIN="/usr/sbin/${DISPLAY_MANAGER}" if [ ! -e "${DISPLAY_MANAGER_BIN}" ] then echo "${DISPLAY_MANAGER} seems not to be a valid display manager or is not installed." exit 1 fi echo "${DISPLAY_MANAGER_BIN}" > "${DEFAULT_DISPLAY_MANAGER_FILE}" DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true dpkg-reconfigure "${DISPLAY_MANAGER}" echo set shared/default-x-display-manager "${DISPLAY_MANAGER}" | debconf-communicate &> /dev/null echo -n "systemd service is set to: " readlink "${DISPLAY_MANAGER_SERVICE}" echo -n "${DEFAULT_DISPLAY_MANAGER_FILE} is set to: " cat "${DEFAULT_DISPLAY_MANAGER_FILE}" echo -n "debconf is set to: " echo get shared/default-x-display-manager | debconf-communicate
}
set_dm $1The two variables DEBIAN_FRONTEND=noninteractive and DEBCONF_NONINTERACTIVE_SEEN=true are needed to really run interactively.
What also helped was the environment variable DEBCONF_DEBUG to see what debconf is doing. It can be set to
userdeveloperdb.*which is all of the above
Here is an ansible recipe for doing this on Ubuntu 20 for people using that tool.
---
- name: Render /etc/X11/default-display-manager template: src: etc/X11/default-display-manager dest: /etc/X11/default-display-manager owner: root group: root mode: "0644" become: yes
- name: Switch to XFCE4 window manager debconf: name: lightdm question: shared/default-x-display-manager vtype: string value: lightdm register: reconfigure_xfce4 become: yes
- name: Reconfigure dpkg with XFCE4 choice command: dpkg-reconfigure lightdm environment: DEBIAN_FRONTEND: noninteractive DEBCONF_NONINTERACTIVE_SEEN: true become: yes when: reconfigure_xfce4.changed
- name: Select XFCE4 as session manager command: update-alternatives --set x-session-manager /usr/bin/xfce4-session become: yes when: reconfigure_xfce4.changed
- name: Set runlevel default to multi-user file: src: /lib/systemd/system/multi-user.target dest: /etc/systemd/system/default.target state: link become: yes register: change_runlevel
- name: Set runlevel to multi-user command: systemctl isolate multi-user.target become: yes when: change_runlevel.changed