Skip to content

Instantly share code, notes, and snippets.

@danielrosehill
Created November 22, 2025 19:00
Show Gist options
  • Select an option

  • Save danielrosehill/7048617cb8bcd970ba27d01da0cfb268 to your computer and use it in GitHub Desktop.

Select an option

Save danielrosehill/7048617cb8bcd970ba27d01da0cfb268 to your computer and use it in GitHub Desktop.
Fix for USB keyboard not being recognized after reconnection on Linux

Fix: USB Keyboard Not Recognized After Reconnection on Linux

Problem

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.

Root Cause

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.

Solution

This fix implements three layers of protection to ensure USB keyboards are always recognized upon reconnection:

1. Create Udev Rule

Create a udev rule to disable USB autosuspend for keyboards and hubs:

sudo nano /etc/udev/rules.d/99-usb-keyboard-power.rules

Add 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 trigger

2. Create Systemd Service

Create a systemd service to ensure USB hub power settings persist across reboots:

sudo nano /etc/systemd/system/usb-keyboard-power.service

Add 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.target

Enable and start the service:

sudo systemctl daemon-reload
sudo systemctl enable usb-keyboard-power.service
sudo systemctl start usb-keyboard-power.service

3. Immediate Fix (Current Session)

Apply 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
done

Verification

Check 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
done

All hubs should show Power control: on.

Testing

  1. Disconnect your USB keyboard
  2. Wait 5-10 seconds
  3. Reconnect the keyboard
  4. The keyboard should be immediately recognized and functional

Troubleshooting

Check keyboard device path

lsusb | grep -i keyboard

View USB connection events

sudo dmesg | tail -50 | grep -i "usb\|keyboard"

Check specific device power settings

First, find your keyboard's device path:

udevadm info -q all -n /dev/input/by-id/*kbd* | grep DEVPATH

Then check its power control (replace X-X with your device path):

cat /sys/bus/usb/devices/X-X/power/control

Verify udev rule is active

sudo udevadm test /sys/bus/usb/devices/1-4.4.2 2>&1 | grep power

Alternative: Disable USB Autosuspend Globally

If the above solution doesn't work, you can disable USB autosuspend globally via kernel parameters:

  1. Edit GRUB configuration:
sudo nano /etc/default/grub
  1. Add usbcore.autosuspend=-1 to GRUB_CMDLINE_LINUX_DEFAULT:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash usbcore.autosuspend=-1"
  1. Update GRUB and reboot:
sudo update-grub
sudo reboot

Technical Details

  • 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

Tested On

  • Ubuntu 25.04 / 25.10
  • Kernel 6.17.0-6-generic
  • Should work on most modern Linux distributions with systemd and udev

Notes

  • 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.

Comments are disabled for this gist.