I finally found the right way to remap keys in Ubuntu.
This only works for later versions of udev so check your version.
> udevadm --version
Should be 240+
> sudo systemd-hwdb update # if it's not
First find the keyboard to remap
> sudo evtest
pick the keyboard by event id. Note the id (say it's 4)
press keys you want to remap and note the 'value' on the MSC_SCAN line on the key you want to change
press the key you want to map to and note the KEY_XXXX code
We will need the modalias, so
> cat /sys/class/input/event4/device/modalias
Now you need to make a file
> sudo gedit /etc/udev/hwdb.d/10-my-modifiers.hwdb
Here is mine:
evdev:input:b0011v0001p0001*
KEYBOARD_KEY_db=leftalt
KEYBOARD_KEY_38=leftmeta
The format needs to be exact. There is one space before KEYBOARD_XXX.
The key codes are lowercase.
So now you need to load the config
> sudo udevadm trigger
> sudo udevadm trigger
> udevadm info /dev/input/event4 | grep KEYBOARD
You should see your mappings are active.
Now even if you reboot your mappings should still be there.