Lock Keyboard and Mouse on Linux with xtrlock – Prevent Accidental Input
This guide shows how to install the xtrlock utility on Linux, create a script that locks the keyboard and mouse while leaving the screen visible, bind the script to a custom shortcut on Ubuntu or Arch MATE, and safely unlock or recover from a permanent lock.
1. Install xtrlock
The xtrlock program is available in the default repositories of most Linux distributions. On Debian‑based systems such as Debian, Ubuntu, and Linux Mint you can install it directly with the package manager: $ sudo apt-get install xtrlock Other distributions use their own package commands (e.g., dnf, pacman, etc.).
2. Create a script to lock the keyboard and mouse
After installing xtrlock, create an executable script named lockkbmouse in /usr/local/bin: sudo vim /usr/local/bin/lockkbmouse Insert the following content and save the file (Esc, then :wq in vim):
#!/bin/bash
sleep 1 && xtrlockMake the script executable:
sudo chmod a+x /usr/local/bin/lockkbmouse3. Bind the script to a keyboard shortcut
a) Ubuntu (GNOME)
Open Settings → Devices → Keyboard , click the “+” button to add a new shortcut, give it a name, and set the command to: bash -c "sleep 1 && xtrlock" Assign a key combination such as Alt+K . Confirm the shortcut and close the settings window. The following screenshots illustrate the process:
Press the chosen key combination; the mouse pointer will turn into a lock icon, indicating that both mouse and keyboard are locked while the screen remains visible.
b) Arch Linux with MATE
Navigate to System → Preferences → Hardware → Keyboard Shortcuts , click “Add”, give the shortcut a name, and use the same command: bash -c "sleep 1 && xtrlock" Assign a key combination such as Alt+K. The screenshots below show the configuration steps:
4. Unlocking the keyboard and mouse
To unlock, simply type your password and press Enter . The password entry is hidden, so just type it and press Enter. If the password is incorrect, the system will beep; press Esc to clear the entry, then try again. You can also delete characters with Backspace or Delete .
5. Recovering from a permanent lock
On some distributions (e.g., those using GDM) xtrlock may freeze the screen. Test the tool first in a virtual machine. If you become permanently locked, switch to a TTY with Ctrl+Alt+F2 and terminate the process: $ sudo killall xtrlock For additional options, consult the manual page:
$ man xtrlockLiangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
