I am on Ubuntu 18.04 and trying swap Ctrl and CapsLock using xmodmap. But failed to find a way of doing that automatically: .[X|x]modmap[rc] and .config/autostart didn't work. What other ways are there? Could it be possible throgh systemd?

SHORT: Desktop entry in .config/autostart or /etc/xdg/autostart. Exec is not a full-fledged shell command, so sh -c might be required

[Desktop Entry]
Exec=sh -c "xmodmap ~/.xmodmaprc"
  • 5
    I think you want the command to run when gdm starts (so is technically not part of the boot process).
    – RonJohn
    Commented Dec 23, 2020 at 2:17
  • @RonJohn I would be grateful if you could write and answer about how to do it)
    – vatosarmat
    Commented Dec 23, 2020 at 2:59
  • Sadly, I don't know how. Hopefully though this will point you in the right direction.
    – RonJohn
    Commented Dec 23, 2020 at 3:14
  • Comments are intended to be helpful: to help you improve the question / answer. So don't expect them to be an answer. Commented Dec 23, 2020 at 8:24

2 Answers 2


Since Ubuntu switched back from Unity to Gnome in version 17.10, you should be able to use the Gnome autostart mechanism (if it is sufficient that the shell command is launched on login).

To do so:

  • you will need sudo privileges
  • create a shell script that runs the necessary command (say switch_ctrl_capslock.sh) and place it in /usr/local/bin
  • create a .desktop file /etc/xdg/autostart/switch_ctrl_capslock.desktop with (more or less) the following content:
    [Desktop Entry]

If everything is setup correctly, the script should be run once when the user logs into Gnome.

For further reading, have a look at

  • How would this be used to swap the keys before login? (Not that I know why you'd use the Ctrl key at the gdm login screen, but there might be something I don't know about.)
    – RonJohn
    Commented Dec 23, 2020 at 9:03
  • @RonJohn That is a little more involved. One problem is that the desktop environment/window manager often uses input methods that supersede the "raw" input interpretation (as can be found on the virtual consoles e.g.), but since they are run by the user logging in, doing that before login may prove to be ineffective. The solution would probably depend on the exact circumstances (graphics server, desktop manager, window manager).
    – AdminBee
    Commented Dec 23, 2020 at 9:06
  • 1
    Yep, desktop entry is the way to go. I've created one in .config/autostart. I've tried it before, but it failed because Exec is not a full-fledged shell command and don't expand tildes, variables and substitutions
    – vatosarmat
    Commented Dec 23, 2020 at 10:35

The systemd-way:

Upon login

Description=Change keyboard layout AFTER LOGIN TO GUI session

#execute once only
ExecStart=/usr/bin/xmodmap home/<user>/.xmodmaprc


To be put under ~/.config/systemd/user/xmodmap.service (note do NOT replace user with your username!).

To enable the service to be autoloaded:

systemctl --user enable xmodmap.service

To start it the first time without need for a relogin;

 systemctl --user start xmodmap.service

Running it as soon as gdm is up

Same file as above, but rather place it at /etc/systemd/system/xmodmap.service and add the following line to the [Unit] section to be save:


As root (or via sudo):

systemctl enable xmodmap.service

Of course it will then affect all users.

You must log in to answer this question.

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