87

I need to change the layout of the keyboard while working in the console on Linux. I need to set up English layout as the default for the console. How can I do it?

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
J.Olufsen
  • 3,492
  • 14
  • 43
  • 59

9 Answers9

82

To change the system-wide keyboard layout, run

sudo dpkg-reconfigure keyboard-configuration

or edit the XKBLAYOUT line in /etc/default/keyboard.

ephemient
  • 24,884
  • 4
  • 30
  • 20
  • 10
    On Red Hat 7: `localectl status`, `localectl list-keymaps`,`localectl set-keymap cz`, see [Changing the Keyboard Layout](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/s1-changing_the_keyboard_layout) ... The keymaps are in `/usr/lib/kbd/keymaps/` btw.. – David Tonhofer Nov 29 '17 at 13:21
  • Hi)) Ubuntu 18.04.2 with the Gnome desktop does not respond to the state `/etc/default/keyboard`. To change the keyboard settings in this case, you need to use `dconf` – AtachiShadow Feb 18 '19 at 19:58
36

To change your keyboard layout fast, just install setxkbmap with:

sudo apt-get install x11-xkb-utils

After this you can always change the keyboard layout with:

setxkbmap us

To do this automatically every time, extend your .bashrc with:

echo "setxkbmap us" >> ~/.bashrc

Now, open a new console and the US keyboard layout is activated. Don't worry if the small keyboard layout icon in the Ubuntu panel does not change.

Arjan
  • 30,974
  • 14
  • 75
  • 112
Malte Ahl
  • 477
  • 4
  • 3
  • 1
    This does not really change the default keyboard layout but is a sort of hack to reset the keyboard layout to another one everytime you open a new console (while it is only necessary to reset after reboot). For a permanent solution, it is possible to set it directly, for example in `/etc/default/keyboard` like suggested by other answers. – baptx Aug 16 '18 at 15:27
  • 3
    This doesn’t answer the question. setxkbmap(1) works under X (graphical) while OP asked about the console. [OP even](https://superuser.com/questions/404457/how-to-change-input-keyboard-layout-while-in-console#comment460038_404457) brought up loadkeys(1) in a comment on his question, three years before you posted this answer (loadkeys(1) is a console-only program). – Guildenstern Oct 26 '19 at 19:17
13

Another way, but I understand it won't last for the next boot:

sudo loadkeys /usr/share/keymaps/i386/qwerty/us.kmap.gz

Check /usr/share/keymaps/i386/ for you keyboard layout and keymaps to what suits you better.

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
Aquarius Power
  • 785
  • 12
  • 19
  • Put it in /etc/profile.d/bashrc – B K Mar 25 '12 at 01:01
  • 1
    @BK, may be at `/etc/rc.local` to not require sudo? – Aquarius Power Dec 26 '17 at 18:48
  • 2
    `loadkeys us` (**without** the full path) is the only thing that worked for me on a system without *setxkbmap* (fresh minimal RHEL-8 install). The full path differs between distros, on RHEL it's actually under `/usr/lib/kbd/keymaps` and there are 2 sets -- *legacy* and *xkb*. – Alois Mahdal Sep 10 '21 at 11:36
  • Thank you. I have an embedded system (Toradex) with a minimal Linux image and I needed this to switch the keyboard layout to azerty. – Aminos Jul 30 '22 at 13:54
6

loadkeys us, it loads US keys but for a single session.

mashuptwice
  • 2,929
  • 2
  • 12
  • 25
  • Loadkeys has alrwsyd been suggested, and in better answers which included information on HOW to use it. Please avoid posting duplicate information in answers, especially when your posts does not actually consist of a complete answer. – music2myear Sep 22 '19 at 15:48
  • Without `sudo`, I got `KDSKBMODE: Operation not permitted: could not switch to Unicode mode` ([Ubuntu 19.10 (Eoan Ermine)](https://en.wikipedia.org/wiki/Ubuntu_version_history#Ubuntu_19.10_(Eoan_Ermine))). – Peter Mortensen May 06 '20 at 15:47
4

On current systemd based systems use localectl (as mentioned by @david-tonhofer):

sudo localectl set-keymap us
sudo localectl set-x11-keymap us,de pc104
localectl status
TNT
  • 281
  • 3
  • 7
2

The very funny could be to switch back... a possible solution is, to make a Bash script (named with symbols of the target language. For instance, англ).

The second trick is to type "setxkbmap us; setxkbmap ru", and then it's possible to use the Bash history to return the main layout to the console.

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
Z0dCHiY8
  • 31
  • 1
  • 2
    Can you please clarify (1) what you’re saying, and  (2) how it answers the question? – Scott - Слава Україні Mar 06 '18 at 20:29
  • 1
    well, you switched console in russian language. how would you like to switch it back to english 777 actually, i didn't answer main question, i just added some tricks to avoid possible problems. For example, you work w/o gui & switched console to russian, so you cannot type any command. :) – Z0dCHiY8 Mar 06 '18 at 22:06
  • but what if— it defaults to ru? – Fuseteam May 28 '22 at 19:09
1

I know this is old, but I'm gonna leave this in case someone else needs it. First answer is correct, it works, the only thing is that in newer versions the "keyboard" file doesn't exist, instead, we have a "console-setup" file where you can also find and edit the XKBLAYOUT option.

  • Avoid posting answers to old questions that already have well received answers unless you have something substantial and new to add. – Toto Oct 23 '22 at 20:47
0

A better hack is to monitor udev and reset it automatically if the keyboard gets disconnected. Even users on laptops might need it, since it might get instantaneously disconnected. I just made a GitHub repository to do this.

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
Spenser Truex
  • 151
  • 1
  • 7
  • ***How*** do you monitor udev (respond by [editing you answer](https://superuser.com/posts/1428399/edit), not here in comments)? – Peter Mortensen May 06 '20 at 15:44
0

switch layout:

setxkbmap -layout us && setxkbmap -layout us,ru
setxkbmap -layout ru && setxkbmap -layout ru,us

force switch:

setxkbmap -layout ru && setxkbmap -layout us && setxkbmap -layout us,ru