After several days research, I found that in Ubuntu /etc/gdm/Xsession is executed instead of /etc/X11/Xsession. Though both run the scripts under /etc/X11/Xsession.d.

I have defined /etc/X11/Xmodmap and created a hook script in /etc/X11/Xsession.d/40load-xmodmap, but the loaded xmodmap is lost after 99x11-common_start.

Debug codes:

for file in Xsession.d/*; do
  . $file

  echo The current xmodmap is: 

The printed xmodmap shows 40load-xmodmap works very well, but the defined key mods are lost after the desktop is brought up.

At last, I defined xmodmap in user directory ~/.Xmodmap, this time when the session is started a popup window prompts me to enable the Xmodmap. So I guess maybe GNOME desktop reset the xmodmap?


First, you need to be aware that there are two interfaces for configuring the keyboard under X:

  • Xmodmap: traditional, simple, limited.
  • XKB: modern, complicated, powerful.

You can define your keyboard layout in /etc/X11/xorg.conf with the kbd device. (The man page doesn't seem to be in the actual distribution for some reason.) If you don't do this, the settings from /etc/default/console-setup apply.

If your display manager is Gdm, it loads XKB and xmodmap settings (look in /etc/gdm/Xsession) from /etc/X11/Xkbmap and /etc/X11/Xmodmap respectively. Other display managers leave your settings alone at this point.

When you log in, your desktop environment might override your settings with its own settings. If you're running the default Ubuntu environment, see this question.

I recommend choosing the built-in layout that corresponds to your keyboard as the system layout by using the Ubuntu Keyboard Preferences GUI and pressing “Apply system-wide”, and separately storing your personal preferences in ~/.xmodmap or ~/.xkbmap.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.