2

I would like to disable any keyboard and mouse user puts in USB port (regardless their ID, vendor, name, ...). I found this article http://wpkg.org/Disable_/_enable_keyboard_and_mouse_in_Linux but it is not exactly what I am looking for. Any idea how to disable usb devices (keyboard, mouse, wireless keyboard/mouse) and leave usb port enabled just for usb mass storage?

7

Create a file called, e.g., usbhid.conf in /etc/modprobe.d/ and add the following line:

blacklist usbhid

Then re-generate your initramfs with:

update-initramfs -u -k $(uname -r)

or, if you want to rebuild the initramfs for all installed kernel versions:

update-initramfs -u -k all

After you reboot, usbhid.ko will be prevented from loading. This will persist for any new kernel versions you install until you either delete the /etc/modprobe.d/usbhid.conf file or comment out the blacklist line it contains (of course, you have to re-generate the initramfs again).


BTW if you need to attach a USB kbd/mouse to work on the console for any reason, you can ssh in and run (as root):

insmod /lib/modules/$(uname -r)/kernel/drivers/hid/usbhid/usbhid.ko

and plug the keyboard/mouse into a usb socket. Unlike modprobe, the insmod command ignores any entries (incl. blacklist and module options) in /etc/modprobe.d/.

Don't forget to rmmod usbhid when you don't need to use the kbd/mouse any more.

| improve this answer | |
  • this seems to be a nice solution in case, if I'd need kbd/mouse later. thank you, I will try it! – peter Apr 4 '16 at 16:30
1

You could simply remove the corresponding drivers:

sudo rm /lib/modules/<your_kernel>/kernel/drivers/hid/usbhid/*

(actually, you should rather move those drivers somewhere in case you'll want them back, but I hope you get the idea)

| improve this answer | |
  • 2
    don't delete the modules, just add blacklist usbhid to a .conf file in /etc/modprobe.d and re-generate your initramfs - e.g. on debian update-initramfs -u. when you reboot, usbhid.ko will be prevented from loading. – cas Apr 4 '16 at 14:22
  • No, deleting the drivers is a BAD IDEA. What @cas said. – Faheem Mitha Apr 4 '16 at 15:13
  • Thank you, I will try @cas solution. It seems to be more universal if I need to persist drivers. – peter Apr 4 '16 at 16:31
  • @peter Of course, modifying the config file is the way to go. I'm glad I could find the right driver though. – Dmitry Grigoryev Apr 4 '16 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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