This is working for the current session:

costales@dev:~/Desktop$ xmodmap -e "keycode 166 = less"
costales@dev:~/Desktop$ xmodmap -e "keycode 167 = greater"

Then I created this config file:

costales@dev:~/Desktop$ cat ~/.Xmodmap 
keycode 166 = less
keycode 167 = greater

But it's not working after a reboot. How could I force the remap to survive reboot?


9 Answers 9


In my long experience with remapping keys in Ubuntu, the permanent solution is to modify a file called evdev in /usr/share/X11/xkb/keycodes.

Run this command in the command prompt.

sudo gedit /usr/share/X11/xkb/keycodes/evdev

Remember gedit is a Ubuntu text editor so you can use nano or vim instead of gedit.

The file is in the format: ALIAS: CODE. You can swap buttons by interchanging codes of different keys. For example, to swap RCONTROL with RETURN, edit two lines in evdev into this:

<RTRN> = 105;

<RCTL> = 36;

To know the number code for a button run xev on the terminal.

Log out and back in to apply changes.

  • 3
    I think this might the easiest way to permanently remap a key. I just did that successfully on a Ubuntu 18.04-based distribution, replacing my dead up arrow key by the right shift key. Thanks!
    – stragu
    Dec 28, 2019 at 13:22
  • 1
    I found that guide particularly helpful when editing evdev: charvolant.org/doug/xkb/html/xkb.html
    – mfg
    Jul 15, 2020 at 15:33
  • 1
    HOORAY! I've been struggling with this for hours, and this is what finally worked. In my case I wanted my web key to be my menu key, so I made <COMP> = 180 and commented out the line where <I180> = 180. Downside is that if I plug in a different keyboard, the real Menu key won't work, but the compact keyboard I'm using doesn't have one at all. Sep 2, 2020 at 18:19
  • 1
    Do we need restart after changes? Sep 20, 2021 at 6:38
  • 2
    UPVOTED! Only working solution on the web for 20.04.Well done! Sep 20, 2021 at 6:47

I've been using 16.04 for a little bit and it seems to use sddm as its desktop manager. It used to be LightDM and GDM before that. Both its predacessors are documented to load ~/.Xmodmap automatically but I can't find anything explicitly saying sddm does.

Therefore it may be advisable to just force it to load with a new script. You just need to run xmodmap ~/.Xmodmap and you can do that a number of ways:

There are probably a few dozen other ways to manage this, essentially doing the same thing.

  • 5
    Thanks for the tip! Add to start up didn't work and ~/.xinitrc doesn't exit. But append "xmodmap ~/.Xmodmap" to ~/.bashrc worked! :)
    – Costales
    Mar 24, 2016 at 11:43
  • 1
    Adding it via the graphical start up tools (which creates a new entry in ~/.config/autostart) works about half the time. I've rebooted about 10 times now and 5 times the command has been executed and the other 5 times it hasn't. :-( Jun 17, 2016 at 11:17
  • 1
    @Alexander Rechsteiner: This seems to be a problem in Ubuntu 16.04. I've been fiddling with startup commands and key remapping, and the behavior seems very inconsistent. I'm not an expert in this type of thing, but it seems utterly ridiculous for it to be happening... Feb 26, 2017 at 17:58
  • Not working 20.04 . Sep 20, 2021 at 6:45

I added a file /etc/X11/Xsession.d/80_xmodmap with these contents:

# Set custom keycodes
# This file is sourced by Xsession(5), not executed.
# The "|| true" is to ensure that the Xsession script does not terminate on error


if [ -x /usr/bin/xmodmap ]; then
        if [ -f "$USRMODMAP" ]; then
                /usr/bin/xmodmap "$USRMODMAP" || true

This works every time.

  • It appears to work but this is the messages I get in .xsessions-errors: .Xmodmap:1: bad keysym in remove modifier list 'Caps_Lock', no corresponding keycodes this is my .Xmodmap: $ cat .Xmodmap remove Lock = Caps_Lock keycode 9 = Escape keycode 66 = Escape
    – MaikoID
    Oct 25, 2016 at 19:16
  • Your error message seems clear: You have an error in your ~/.Xmodmap file Sep 23, 2021 at 17:00

open gnome-session-properties. add command and name. desktop file will be created in ~/.config/autostart.

remenber: use absolute path, eg /home/xxx/.Xmodmap. never use '~' as your home dir.


Go to Startup Applications and make a new entry with the content shown in the following screenshot:

adding new startup application(command)

It works every time.

  • 1
    Thank you! this worked like magic and it's super simple!
    – Jaume
    Feb 24, 2021 at 18:21
  • This is the simplest one, even in older and newer Ubuntu.
    – sugab
    Feb 21, 2023 at 4:44

After searching on Google, I was able to resolve my laptop faulty keys remapping. All faulty keys on my Thinkpad T400 are working fine after remapping. Also, it's permanent without an extra load on the CPU core.

  • First find your keyboard layout or keycode using below command.

    xev -event keyboard 
  • For example, on my Thinkpad T400 keycode found as follows:

    Keycode of "z" key is 52
    Keycode of "x" key is 53
    Keycode of "c" key is 54
    Keycode of "v" key is 55
    Keycode of "F6" key is 72
    Keycode of "F10" key is 76
    Keycode of "F11" key is 95
    Keycode of "F12" key is 96 
  • Remapping character keys "z" "x" "c" "v" by assigning function keys "F6" "F10" "F11" "F12".

  • Modify lines as below (Backup file before making any changes to rollback easily if things go wrong).

    sudo vi /usr/share/X11/xkb/keycodes/evdev
  • Find the lines and change value as below:

    <AB01> = 72;
    <AB02> = 76;
    <AB03> = 95;
    <AB04> = 96;
  • Comment these lines or remove the lines:

    //  <FK06> = 72;
    //  <FK10> = 76;
    //  <FK11> = 95;
    //  <FK12> = 96;
  • Save and exit.

To remap special keys:

  • Backup file before making changes to rollback if things go wrong.

    cd /usr/share/X11/xkb/symbols/
    sudo vi pc
  • Add or modify below lines in function "xkb_symbols "editing" {"

    key [SCLK] {    [  Prior        ]   };
    key [PAUS] {    [  Next         ]   };
  • Save and exit.

  • Clear cache.

    sudo rm -rf /var/lib/xkb/*
  • Reboot.

  • I remapped a broken (cursor) arrow key to another key using this technique. It's simply (xev) and press new key to get its scancode, then edit /usr/share/X11/xkb/keycodes/evdev to replace the arrow key's original keycode with the new keycode and to remove the new arrow key's line with its original scancode. I commented wherever I made a change and kept a backup of the file. After a reboot it works perfectly, including shift/ctrl/alt combinations with the new arrow key. And the change should be left in place even when upgrading packages and OS version upgrades.
    – Matthew
    Oct 29, 2018 at 20:47
  • The only method that's actually persisted from hibernate, restarts and turning off and on my keyboard. Thanks! Jun 30, 2021 at 23:00

Navigate to


With sudo privileges, in an editor, open the layout you desire to edit. Replace the values.

After reboot, the changes will be applied.

Here is an example I use to remap my keypad because of keyboard hardware issues.

keypad remap example

  • 2
    Those edits will be overwritten the next time the packages that owns those files get upgraded.
    – muru
    Nov 26, 2017 at 13:13
  • 1
    That may be true, but I doubt these specifically are updated very often. I have been satisfied with this solution for some months since I solved the issue and not once did I have to do anything additional. And to prevent this, you can save the file and replace it in the case of such event or, (which seems more secure) just recreate the process.
    – quaeched
    Nov 26, 2017 at 13:16
  • I found that this was the best way to persist the changes too. xmodmap seemed to forget the mapping as soon as I even disconnected my keyboard for whatever reason. This solution isn't perfect but works. We'll see how it goes. @Sanalgorithm's answer above instructs on how to do the remapping alluded to here. Jun 30, 2021 at 23:19

Graphical Editor

gnome-tweaks > Keyboard & Mouse > Additional Layout Options

Key Maps


Because .Xmodmap files are much easier to edit than xkb, I use .Xmodmap as a 'source' file, then convert it to xkb (the current standard) and auto-load at boot using the .bashrc method.

It's a few simple steps posted here.

Works perfectly, and using xkb future-proofs against when support for .Xmodmap is dropped.

  • Why do you think support for xmodmap will be dropped? Feb 16, 2020 at 11:04
  • Thanks for the heads-up @OlafDietsche. Just completed upgrades to 18.04.3 on 3 machines and was surprised to find xmodmap support not only returned, but better than before. So I'll update the link and the above post w/ current info. Best to you.
    – u2n
    Feb 18, 2020 at 17:57

You must log in to answer this question.

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