Recover a Linux console keyboard with kbd_mode
You will identify a Linux virtual console's current keyboard mode and, when necessary, return it to UTF-8 mode with kbd_mode. This is a recovery tool for a console left in the wrong state, not a general keyboard customisation command. Allow about ten minutes. You need the kbd package and access to the target console; changing a console's mode normally requires root.
The route
Jump straight to the step you need, or tick off Done means at the end.
The examples match kbd 2.6.4-2ubuntu2 installed on this machine. The installed command accepts long options as well as the short forms shown in the original manual page. Your package may use a different version, so check its local help before putting a command into a recovery script.
1. Confirm the command and package version
Start with checks that do not change keyboard state:
$ command -v kbd_mode
/usr/bin/kbd_mode
$ kbd_mode --version
kbd_mode from kbd 2.6.4
The version output and package revision are separate: this host reports kbd_mode from kbd 2.6.4 and the package manager reports 2.6.4-2ubuntu2. Record the command version when troubleshooting a machine with older tooling.
Checkpoint
If command -v prints nothing, install the kbd package through your normal distribution process. Do not copy a replacement binary into /usr/bin.
2. Report the current mode
Run kbd_mode without a mode option while you are logged in on the console you want to inspect:
$ kbd_mode
UTF-8
The underlying manual describes the report as RAW, MEDIUMRAW or XLATE; the installed version prints the corresponding current state in its own wording, such as UTF-8. The command reads the console associated with standard input when -C is absent. A shell reached through SSH usually does not have the physical console as its standard input, so specify the console device explicitly there.
Do not mistake a terminal emulator for a virtual console. A desktop terminal and an SSH pseudo-terminal are not automatically the console whose keyboard is misbehaving.
3. Name the console explicitly when needed
Use -C with the device for the target Linux console. For example, from a privileged recovery shell:
# kbd_mode --console=/dev/tty1
UTF-8
The short equivalent is kbd_mode -C /dev/tty1. The option selects which console is inspected or changed; it does not switch your current shell to that console. Replace /dev/tty1 with the actual target device. Check the device before acting:
# test -c /dev/tty1 && echo 'character device exists'
character device exists
# ls -l /dev/tty1
crw--w---- 1 root tty ... /dev/tty1
The exact owner, group and permission bits vary. If the device does not exist, stop and identify the active console rather than guessing. A bad device path produces an error; it does not diagnose another console.
4. Restore the normal UTF-8 mode
If a program has left the target console unusable and you have confirmed the device, set UTF-8 mode:
# kbd_mode --unicode --console=/dev/tty1
--unicode is the long form of -u. In this mode, the kernel receives Unicode characters encoded as one, two or three UTF-8 bytes, and the key mapping loaded by loadkeys is used. The command normally prints no success message, so check its exit status immediately:
# status=$?
# printf 'kbd_mode exit status: %s\n' "$status"
kbd_mode exit status: 0
# kbd_mode --console=/dev/tty1
UTF-8
This changes console state but does not rewrite keymaps or alter boot configuration. If the first command fails, preserve the error text and investigate permissions or the device path before trying another mode.
5. Understand the other modes before touching them
kbd_mode has four mode selectors:
-uor--unicode: UTF-8 mode.-aor--ascii: ASCII mode, calledXLATEby the traditional manual.-kor--keycode: keycode orMEDIUMRAWmode.-sor--scancode: scancode orRAWmode.
Switching between ASCII and Unicode is the ordinary compatibility change. The manual warns that changing to or from the raw and keycode modes will probably make the keyboard unusable. Do not use -k or -s as experiments on a remote rescue session.
Warning
--force or -f overrides the program's protection and permits a mode change likely to break keyboard input. It does not make the change safer and it does not provide an undo transaction. Only use it when you have a tested recovery path to the selected console and a specific reason to override the warning.
6. Recover if the console remains wrong
First return to Unicode without force, using the exact device:
# kbd_mode --unicode --console=/dev/tty1
# kbd_mode --console=/dev/tty1
UTF-8
If the console is still displaying unusable input, reconnect through an independent path such as SSH or another virtual console, confirm that you selected the right /dev/ttyN, and inspect the keymap with loadkeys. kbd_mode cannot repair a wrong keymap. Avoid stacking random -f changes: each one can make the affected console harder to control.
For a script, keep the target explicit and stop on failure:
#!/bin/sh
set -eu
console=${1:-/dev/tty1}
kbd_mode --unicode --console="$console"
kbd_mode --console="$console"
Pass a known device, such as /dev/tty1, rather than accepting untrusted input in a privileged wrapper. This command changes terminal behaviour and should not be exposed as an unrestricted service.
Done means
- You confirmed the installed
kbd_modeversion. - You checked the target console instead of assuming an SSH or desktop terminal was the same device.
- The target reports
UTF-8after recovery. - You left raw and keycode modes alone unless you had an independent recovery path.
- You did not use
--forcecasually, and you know that keymap repair belongs toloadkeys.