Home Command line utilities How To Lock The Keyboard And Mouse, But Not The Screen In Linux
Lock The Keyboard And Mouse Using xtrlock

How To Lock The Keyboard And Mouse, But Not The Screen In Linux

By sk
1654 Views

In this tutorial, I will show you how to lock the keyboard and mouse, but not the screen in Linux using "xtrlock" utility. Xtrlock is a simple, yet useful utility to lock the X display till the user enters their password at the keyboard.

Why lock the keyboard and mouse, but not the screen?

My 4-years-old niece is a curious-kid. She loves "Avatar" movie very much. Whenever the Avatar movie is playing, she became so focused and her eyes are glued to the screen. But the problem is she often touches a key in the keyboard or move the mouse or click the mouse button while watching the movie. Sometimes, she accidentally close or pause the movie by pressing a key in the keyboard. So I started looking for a way to lock down both the keyboard and mouse, but not the screen.

After a couple google searches, I came across this perfect solution in Ubuntu forum. If you don't want your cat walking on your keyboard or your kid messing up with the keyboard and mouse while you watching something important on the screen, I suggest you to try xtrlock utility to lock X display until password supplied, leaving windows visible.

Some of you might think - why not simply unplug the keyboard or mouse? True! If it is a desktop, you can temporarily remove the keyboard/mouse. But in Laptops, there is no way to do it.

Install xtrlock

The xtrlock package is available in the default repositories of most Linux operating systems. So, you can install it using your distribution's package manager.

On Arch Linux and derivatives, run the following command to install it.

$ sudo pacman -S xtrlock

On Fedora:

$ sudo dnf install xtrlock

On RHEL, CentOS:

$ sudo yum install xtrlock

On SUSE/openSUSE:

$ sudo zypper install xtrlock

On Debian, Ubuntu, Linux Mint:

$ sudo apt-get install xtrlock

Create keyboard shortcut to lock the X display

Once xtrlock installed, create a keyboard shortcut. You need this to lock the keyboard and mouse using the key combination of your choice.

Create a new file called lockkbmouse in /usr/local/bin.

$ sudo vi /usr/local/bin/lockkbmouse

Add the following lines into it.

#!/usr/bin/env bash
sleep 1 && xtrlock

Save the file and close the file.

Make it as executable using the following command:

$ sudo chmod a+x /usr/local/bin/lockkbmouse

Next, we need to create keyboard a shortcut to lock the kb/mouse.

In Ubuntu GNOME DE:

In Ubuntu 20.04 LTS desktop, ho to Settings -> Keyboard shortcuts. Click the + (plus) symbol at the end. On older Ubuntu versions like Ubuntu 18.04 LTS, desktop, go to System Settings -> Devices -> Keyboard.

Keyboard shortcuts window in Ubuntu

Keyboard shortcuts window in Ubuntu Gnome

Enter a name for your shortcut and add the following line in the command box, and click Set Shortcut button.

/usr/local/bin/lockkbmouse

You can also use this line in the command box as well.

bash -c "sleep 1 && xtrlock"
Add custom Keyboard shortcut to xtrlock

Add custom Keyboard shortcut to xtrlock

Next, you will see the following screen. Type the key combination of your choice to assign the shortcut key to the newly created shortcut. For example, I use Alt+k.

Assign the keyboard shortcut key to xtrlock

Assign the keyboard shortcut key to xtrlock

To clear the key combination, press BACKSPACE key.

Now you will see that the selected shortcut key appears in the Shortcut box.

Set keyboard shortcut for xtrlock

Set keyboard shortcut for xtrlock

This is the key combination to lock the keyboard and mouse. Close this window.

The keyboard shortcut key for xtrclock has been created.

keyboard shortcut key for xtrclock

keyboard shortcut key for xtrclock

Once you finished, close the Keyboard Settings window.

In Arch Linux MATE desktop:

Go to System -> Preferences -> Hardware -> keyboard Shortcuts.

Click Add to create a new shortcut.

Keyboard Shortcuts window in MATE DE

Keyboard Shortcuts window in MATE DE

Enter the name for your shortcut and add the following line in the command box, and click Apply button.

bash -c "sleep 1 && xtrlock"
Create custom keyboard shortcut to xtrlock in MATE DE

Create custom keyboard shortcut to xtrlock in MATE DE

To assign the shortcut key, just select or double click on it and type the key combination of your choice. For example, I use Alt+k.

Custom keyboard shortcut to xtrlock in MATE DE

Custom keyboard shortcut to xtrlock in MATE DE

To clear the key combination, press BACKSPACE key. Once you finished, close the Keyboard Settings window.

Lock the Keyboard and Mouse, but not the Screen using xtrlock

From now on, whenever you press the keyboard shortcut key (ALT+k in our case), the mouse pointer will turn into a a padlock. Now, the keyboard and mouse have been locked, so you can freely watch the movies or whatever you want to. Even your kid or pet touches some keys on the keyboard or clicks a mouse button, they won't work.

Here is xtrlock in action.

Lock the Keyboard and Mouse, but not the Screen using xtrlock In Linux

Lock the Keyboard and Mouse, but not the Screen using xtrlock In Linux

Do you see the a small lock button? It means that the keyboard and mouse have been locked. Even if you move the lock button, nothing will happen. The task will keep running until you unlock your screen and manually close it.

Unlock keyboard and mouse

To unlock the keyboard and mouse, simply type your password and hit "Enter". You will not see the password as you type it. Just type the password anyway and hit ENTER key. The mouse and keyboard will start to work after you entered the correct password. If you entered an incorrect password, you will hear a bell sound. Press ESC key to clear the incorrect password and re-enter the correct password again. To remove one character of a partially typed password, press either BACKSPACE or DELETE keys.

What if I permanently get locked out of the screen?

The xtrlock tool may not work on some DEs, for example GDM. It may permanently lock you out of the screen. Please test it in a virtual machine and then try it in your personal or official desktop to verify if it really works. I tested this on Arch Linux MATE desktop and Ubuntu 20.04 and 18.04 GNOME desktop. It worked just fine.

Just in case, you are locked out of the screen permanently, switch to the TTY (CTRL+ALT+F2) then run:

$ sudo killall xtrlock

Alternatively, you can use the chvt command to switch between TTY and X session.

For example, to switch to TTY1, run:

$ sudo chvt 1

To switch back to the X session again, type:

$ sudo chvt 7

Different distros uses different key combinations to switch between TTYs. Please refer your distribution's official website for more details.

In case of GDM crash, even TTY is not accessible and the system hangs at reached graphical interface. In such cases, you might need to chroot into the system and uninstall xtrlock. You have been warned!

For more details about xtrlock, refer man pages.

$ man xtrlock

Resource:

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

11 comments

César September 14, 2017 - 9:25 pm

Thank you very much for this post SK. It is very useful.
Only one suggestion. I think it is not necesary to write the command again in the custom shortcut command box.
You have already created an executable file with the command inside, so if you select that file in the custom shortcut box (resulting in “/usr/local/bin/lockkbmouse”), it will work as well.

Reply
Jarom Jackson January 24, 2019 - 5:24 am

Thanks! On kde I just added a global shortcut (ctrl+win+L) and had it call xtrlock directly (no need for a script file or the sleep command). Works great.

Reply
Jo April 15, 2019 - 6:10 pm

I’m afraid screenshots show the name of the program as “xtrclock”, which is unfortunate.

Reply
sk April 16, 2019 - 2:31 pm

Good catch. Thanks Jo. I fixed the screenshots now.

Reply
Pete June 30, 2019 - 4:09 am

Thanks. Just what I needed

Reply
Kev June 30, 2019 - 4:42 pm

No mention of how to set the initial password, or how to change it?

Reply
sk June 30, 2019 - 8:29 pm

Just type your system user password.

Reply
kevin snowden June 7, 2020 - 3:34 am

can i make this auto run after reboot?

Reply
sk June 7, 2020 - 1:23 pm

You mean you want to lock keyboard & mouse on every reboot? Simply add this to your .bashrc file:

/usr/local/bin/lockkbmouse

But there is no point to automatically lock display. I won’t recommend doing it. You may permanently get locked out of the screen.

Reply
eduardo September 11, 2020 - 7:14 pm

why not just remove the mouse and keyboard cables in THAT case?

Reply
sk September 11, 2020 - 7:50 pm

Yes, that’s very easy solution. But, what would you do if it is a Laptop?

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More