I had a really bad lockup of my X server and had to do a Sys Rq + r to release my keyboard from X and get into a console. I was able to kill the process that was locking up my system, and continue my work in my still running X server.

Now whenever I e.g. push Alt + F4 to kill a window, my system switches to the 4th console instead of killing the active window. So it seems that my keyboard still is in released mode.

How do I undo my previous Sys Rq + r command, such that I can continue my work in my running X server?

up vote 28 down vote accepted

I found the solution myself just after asking this question.

To switch back the console in which X is running (usually tty7), from ASCII mode to RAW mode execute the following command:

sudo kbd_mode -s -C /dev/tty7

And now everything works as expected again. :)

More information available in the question: What does raw/unraw keyboard mode mean?

  • Thanks! Still trying to figure out if there's a way to do it for all ttys at once, the same way unRaw does... – phyzome Nov 1 '15 at 13:58
  • 3
    @phyzome You don't want to do this for all ttys, as the "normal" (text-mode) tty expects ASCII (XLATE) or UTF-8 (UNICODE) mode. Otherwise you can't type there (that's just what SysRq-R fixes). For fun, try kbd_mode -s -C /proc/self/fd/0 on a tty, see it break, then fix it by SysRq-R. – nobody Dec 1 '16 at 22:25
  • If you are using the TTY that X is running in, you can also use sudo kbd_mode -s -C /dev/tty$(sudo fgconsole). – TSJNachos117 May 31 at 22:34

If you find yourself searching the web for this particular command more than once (like I've done) you can use the following shell script to recapture the keyboard to the TTY in use:

#!/bin/bash

IFS=$'\n\t'
function ttys() {
    # find open files named `/dev/tty?` from commands named `Xorg`
    lsof -Fn -c Xorg -a /dev/tty? | \
      egrep '^n' |                  \
      sed -r 's/^n(.+)$/\1/g' |     \
      sort |                        \
      uniq
}

tty=$(ttys)
count_ttys=$(wc -l <<< $tty )

if [[ $count_ttys -gt 1 ]]; then
  echo 'more than one tty in use, exiting'
  exit 1
fi

echo binding keyboard to $tty
sudo kbd_mode -s -C $tty

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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