1

I am porting a shell script written for Arch to Debian.

The relevant part:

keymaps=$(localectl list-keymaps)

if test -n "${1}" && localectl list-keymaps | grep -q "${1}"
then
    keymap="${1}"
else
    exec 3>&1
    keymap=$(/sbin/dialog --title "Keyboard layout" --menu "Choose a keyboard layout" 25 50 20 $(for item in ${keymaps[@]}; do echo ${item} "-" ; done) 2>&1 1>&3) || exit 1
    exec 3>&-
fi

localectl set-keymap ${keymap}

if [[ $DISPLAY ]] && [[ -r /etc/X11/xorg.conf.d/00-keyboard.conf ]]; then
    # X11 is already running
    x11keymap=$(awk '/^\s*Option "XkbLayout"/ { print $3 }' /etc/X11/xorg.conf.d/00-keyboard.conf)
    setxkbmap -layout ${x11keymap}
fi

The problem is, localectl list-keymaps and localectl set-keymap ${keymap} do not seem to work on Debian systems. I did some research and figured out that that is because instead of using a pure systemd solution to control the keyboard layout in the console like Fedora and Arch, Debian uses a mix of systemd and sysvinit solutions.

However, I was unable to find a way to set a keymap like with localectl set-keymap ${keymap}—running this command doesn't throw an error, but the layout doesn't change. I was able to make localectl list-keymaps list keymaps by manually adding them following this solution https://unix.stackexchange.com/a/763320/610025, but I'm afraid it will not work with the actual layout changing command. Is there a Debian-working solution?

0

1 Answer 1

3

The Debian way of setting up the keyboard is

dpkg-reconfigure keyboard-configuration

as root.

This is interactive by default but asks quite a few more questions than your script:

  • the keyboard model
  • the keyboard layout
  • which key to use as AltGr
  • which key to use as Compose
  • whether to allow CtrlAltBackspace to terminate the X server

Since this uses debconf, the values can be pre-seeded using debconf-set-selections, and the script run in non-interactive mode (DEBIAN_FRONTEND=noninteractive dpkg-reconfigure keyboard-configuration) to apply the settings. There are quite a few interactions between settings however so it might not be all that easy to handle all the cases without running the interactive version.

Another option is to edit the configuration in /etc/default/keyboard directly, and reconfigure in non-interactive mode to apply the settings.

See also /usr/share/doc/keyboard-configuration/README.Debian, and man 5 keyboard which explains how to apply the settings without rebooting:

  • run udevadm trigger --subsystem-match=input --action=change to update graphical desktop environments;
  • run setupcon to update text consoles.
4
  • Thanks, it works! At first it showed unable to locate package when being launched from root, but I figured out that you should use su - root instead of su root to switch user (or just sudo) for it to work. One more problem remains—you need to reboot the system for the changes to take place. Is there any way to apply settings without a need for full reboot? Commented May 8 at 20:00
  • See the updated answer. Commented May 9 at 12:25
  • I tried both udevadm trigger --subsystem-match=input --action=change and service keyboard-setup restart that was described here wiki.debian.org/… . setupcon returnes Couldn't open /dev/tty2 and 3 more lines with tty3, 5 and 6. Two more lines say The keyboard is in some unknown mode and Changing to the requested mode may make your keyboard unusable, please use -f to force the change.. setupcon -f returnes the same thing without the last 2 lines, but still does not affect the layout. Commented May 10 at 3:33
  • It worked on another machine fine though Commented Jun 18 at 20:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .