When disconnecting and reconnecting a USB keyboard on Linux, the system fails to recognize the device even though it's physically connected. The keyboard works fine initially but requires a system reboot or multiple reconnection attempts to be detected again.
USB hubs are configured with autosuspend enabled (power control set to "auto"). When a keyboard is disconnected, the parent USB hub may enter a suspended state. Upon reconnecting the keyboard, the suspended hub doesn't properly wake up, preventing the keyboard from being enumerated and recognized by the system.
This fix implements three layers of protection to ensure USB keyboards are always recognized upon reconnection:
Create a udev rule to disable USB autosuspend for keyboards and hubs:
sudo nano /etc/udev/rules.d/99-usb-keyboard-power.rulesAdd the following content:
# Disable USB autosuspend for keyboards to prevent reconnection issues
# This ensures keyboards are always recognized when reconnected
# Disable autosuspend for all USB keyboards
ACTION=="add", SUBSYSTEM=="usb", ATTR{bInterfaceClass}=="03", ATTR{bInterfaceSubClass}=="01", ATTR{bInterfaceProtocol}=="01", ATTR{power/control}="on"
# Disable autosuspend for USB hubs to prevent hub suspension issues
ACTION=="add", SUBSYSTEM=="usb", ATTR{bDeviceClass}=="09", ATTR{power/control}="on"
Reload udev rules:
sudo udevadm control --reload-rules
sudo udevadm triggerCreate a systemd service to ensure USB hub power settings persist across reboots:
sudo nano /etc/systemd/system/usb-keyboard-power.serviceAdd the following content:
[Unit]
Description=Disable USB autosuspend for keyboards and hubs
After=multi-user.target
[Service]
Type=oneshot
ExecStart=/bin/bash -c 'for hub in /sys/bus/usb/devices/*/power/control; do device=$(dirname $(dirname $hub)); if [ -f "$device/bDeviceClass" ]; then class=$(cat "$device/bDeviceClass"); if [ "$class" = "09" ]; then echo "on" > "$hub"; fi; fi; done'
RemainAfterExit=yes
[Install]
WantedBy=multi-user.targetEnable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable usb-keyboard-power.service
sudo systemctl start usb-keyboard-power.serviceApply the fix immediately without rebooting:
# Disable autosuspend for all USB hubs
for device in /sys/bus/usb/devices/*/bDeviceClass; do
dir=$(dirname $device)
class=$(cat $device 2>/dev/null)
if [ "$class" = "09" ]; then
sudo bash -c "echo 'on' > $dir/power/control"
fi
doneCheck that USB hubs have autosuspend disabled:
for device in /sys/bus/usb/devices/*/bDeviceClass; do
dir=$(dirname $device)
class=$(cat $device 2>/dev/null)
if [ "$class" = "09" ]; then
control=$(cat $dir/power/control 2>/dev/null)
echo "$(basename $dir): Power control: $control"
fi
doneAll hubs should show Power control: on.
- Disconnect your USB keyboard
- Wait 5-10 seconds
- Reconnect the keyboard
- The keyboard should be immediately recognized and functional
lsusb | grep -i keyboardsudo dmesg | tail -50 | grep -i "usb\|keyboard"First, find your keyboard's device path:
udevadm info -q all -n /dev/input/by-id/*kbd* | grep DEVPATHThen check its power control (replace X-X with your device path):
cat /sys/bus/usb/devices/X-X/power/controlsudo udevadm test /sys/bus/usb/devices/1-4.4.2 2>&1 | grep powerIf the above solution doesn't work, you can disable USB autosuspend globally via kernel parameters:
- Edit GRUB configuration:
sudo nano /etc/default/grub- Add
usbcore.autosuspend=-1toGRUB_CMDLINE_LINUX_DEFAULT:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash usbcore.autosuspend=-1"
- Update GRUB and reboot:
sudo update-grub
sudo reboot- USB Device Class 09: USB Hub
- USB Interface Class 03, SubClass 01, Protocol 01: HID Keyboard (Boot Protocol)
- Power control "on": Device cannot be autosuspended
- Power control "auto": Device can be autosuspended based on runtime PM
- Ubuntu 25.04 / 25.10
- Kernel 6.17.0-6-generic
- Should work on most modern Linux distributions with systemd and udev
- This fix keeps USB hubs powered at all times, which may have a minimal impact on power consumption (typically negligible on desktop systems)
- The solution is particularly useful for mechanical keyboards, KVM switches, or USB hubs that experience reconnection issues
- Some USB devices (like wireless keyboard receivers) may also benefit from this fix
Generated with Claude Code
This gist provides a technical solution based on real-world troubleshooting. While these steps worked in our testing environment, always validate configurations in your specific setup and consult your distribution's documentation for system-specific considerations.