Ubuntu 12.04 keyboard layout persistence problems

A recent update for Ubuntu 12.04 (Precise Pangolin) killed off my ability to persist changes in keyboard layout across reboots. No matter how I juggled, removed and recreated any combination of layouts in Ubuntu’s System settings -> Keyboard Layout, I couldn’t get Gnome to recognize the correct input.

The next thing to try was a sudo dpkg-reconfigure keyboard-configuration or even a sudo dpkg-reconfigure console-setup. While this did work for the active session, it did not persist after the next reboot. Not even /etc/default/keyboard was much of a help as it showed exactly what it was supposed to. Gnome was looking like the culprit, allowing me to make and keep a choice of layouts which was subsequently ignored when actually parsing input from the device (a wireless Logitech k750).

So what to do?

As it turns out, this is confirmed bug #995401

A decent workaround is the use of setxkbmap. The command itself does no better at persisting any changes but it can be used non-interactively which means it can be placed in ~/.profile

But hang on, I also need to log in via SSH to this account. An SSH session, at least the way I use it, is not X aware and couldn’t care less about the remote keyboard layout so it kicks up some nasty looking display errors if I just append a setxkbmap no to my ~/.profile.

SSH sessions, however, typically add some easily testable stuff to your environment variables. like so:

What this means is that I am in an SSH session connected from localhost port 54805 to localhost port 22. I am further associated with /dev/pts/7 should I need it. This isn’t really the most interesting thing in the world but these variables are not set upon non-SSH sessions so we can use them to determine when setxkbmap is appropriate and append the following to ~/.profile.

It’s a cheap hack but since I don’t do any fancy X redirecting, it will suffice until a proper fix can be found.