Linux: Configure locale and keyboard layout when remotely accessing from a Mac

At work I have to remote into several different Linux systems from a Mac and there is always the pain of having to handle different keyboard layouts if using Synergy or VMs.

The conversion from a Mac keyboard layout doesn’t translate correctly when the Linux system has the keyboard configured as a PC.

First find out what the current configuration is.

$ localectl

The output in a British system is:

  System Locale: LANG=en_GB.UTF-8
                 LANGUAGE=en_GB:en
     VC Keymap : n/a
     X11 Layout: gb
      X11 Model: pc105
    X11 Options: terminate:ctrl_alt_bksp

Note that the last line might not show in Ubuntu systems.

Unfortunately if you are accessing from a Mac this layout will not work and basic things like |pipe| will not be easy to find.

You can edit the locale and keyboard layout of the system you are accessing so the mapping matches your Mac keyboard layout.

The ideal configuration should be like this:

  System Locale: LANG=en_GB.UTF-8
                 LANGUAGE=en_GB:en
     VC Keymap : n/a
     X11 Layout: gb
      X11 Model: macintosh
    X11 Variant: mac
    X11 Options: lv3:alt_switch

Ubuntu 18.04 / Ubuntu 20.04 / Debian 9 / Debian 10

Edit the following file:

/etc/default/keyboard

With these entries:

XKBMODEL="macintosh"
XKBLAYOUT="gb"
XKBVARIANT="mac"
XKBOPTIONS="lv3:alt_switch"

The XKBOPTIONS I have here are for Synergy to keep the Control and Alt keys on the Mac working the same on the Linux systems. You might not need or want it. Just remove it from the commands if that is the case.

You can also do a text GUI configuration of the keyboard with:

# dpkg-reconfigure keyboard-configuration

If your environment isn’t in English the menus won’t be either. You can force the language output of the application launched to be in the default one. That would be English in most cases. The same command as above but forcing the output to be in English:

# LC_ALL=C dpkg-reconfigure keyboard-configuration

An internet search of LC_ALL will show you more languages and options if needed.

CentOS 6

I have only been able to change the keyboard to a British layout, the Mac layout doesn’t seem to work.

Edit the following file:

/etc/sysconfig/i18n

with the following:

LANG="en_GB.UTF-8"
SYSFONT="latarcyrheb-sun16"
SUPPORTED="en_GB.UTF-8:en_GB:en"

Or type the following command:

# loadkeys uk

You might need to restart if editing i18n, but the change should be automatic with loadkeys.

CentOS 7

Edit the following file:

/etc/locale.conf

with the following:

LANG="en_GB.UTF-8"

Or type the following command:

# localectl set-locale LANG=en_GB.UTF-8

Set the keymap:

# localectl set-x11-keymap gb macintosh mac lv3:alt_switch

In CentOS 7 it isn’t necessary to reboot, the above command automatically loads the key mappings.

CentOS 8

The same commands used for CentOS 7 fail. I suspect that there is a file or folder with the keyboard mappings that has been moved. It might be a bug or a deprecated feature.

Just in case I opened a bug report with CentOS.

Regardless, the following will set the locale and the keyboard mappings correctly:

# localectl set-locale LANG=en_GB.UTF-8
# localectl set-keymap gb-mac

There is no need to reboot.