How do I power cycle a usb port on Fedora? I want to turn off power for a specific USB…
- User
How do I power cycle a usb port on Fedora? I want to turn off power for a specific USB port and then enable it again.
Assistant
Okay, power cycling a specific USB port programmatically in Linux (like Fedora) can be tricky and highly dependent on your hardware (specifically the USB hub controller). Not all hardware supports per-port power switching [1, 10].
Here are the common methods, ranging from the most direct (if supported) to more indirect approaches:
1. Using
uhubctl(Recommended if your hardware is compatible)[1]uhubctlis a command-line tool designed specifically to control power on individual ports of USB hubs that support this feature [1, 10]. Many modern hubs, including some integrated into motherboards or laptops, might be compatible.- Check Hardware Support: First, you need a "smart" USB hub that allows per-port power switching[2]. Many hubs do not support this [10].
- Install
uhubctlon Fedora:
[11]sudo dnf install uhubctl - List Available Hubs and Ports: Run
uhubctlwithout arguments to see detected controllable hubs and their ports, along with their current power status and location IDs [1, 9].
The output will show hubs and their ports, likesudo uhubctllocation 1-4.1or similar. - Turn Power Off: Use the location (
-l), port number (-p), and actionoff(-a off) [1, 9][3].# Example: Turn off port 2 on hub at location 1-4 sudo uhubctl -l 1-4 -p 2 -a off - Turn Power On: Use the same command but with action
on(-a on) [1].# Example: Turn on port 2 on hub at location 1-4 sudo uhubctl -l 1-4 -p 2 -a on
Important: If
uhubctldoesn't list your hub or ports, your hardware likely doesn't support per-port power control via this method [10]. Some hardware (like Raspberry Pi internal hubs) might turn off all ports even when targeting one [10][2].2. Using the
sysfsFilesystem (More Manual, Less Direct Power Control)Linux exposes USB device information and some controls via the
/sys/bus/usb/devices/directory[4][5][7]. You can sometimes influence power states or reset devices here, but direct power cutting isn't always possible [1, 5].- Identify the Device Path:
- First, find the Bus and Device number using
lsusb[6]. - Then, locate the corresponding directory in
/sys/bus/usb/devices/[1]. The directory names often correspond to the bus-port path (e.g.,1-2,2-3.1). You might need to explore subdirectories or check files likeidVendorandidProductto confirm you have the right device.
- First, find the Bus and Device number using
- Method A: Unbind/Bind (Simulates unplug/replug logically): This resets the device driver's connection but doesn't guarantee a power cycle [5, 8].
- Find the device's bus ID (e.g.,
1-2). - Unbind:
echo '1-2' | sudo tee /sys/bus/usb/drivers/usb/unbind - Bind:
echo '1-2' | sudo tee /sys/bus/usb/drivers/usb/bind
- Find the device's bus ID (e.g.,
- Method B: Port Disable (Newer Kernels, Requires Hardware Support): Some newer kernels (around v6+) might expose a
disablecontrol file directly on the port [5].- Navigate to the specific port's directory within the hub's device path (e.g.,
/sys/bus/usb/devices/usbX/X-Y/X-Y:1.0/X-Y-portZ/). You'll need to figure out the exact path. - Disable:
echo 1 | sudo tee disable - Enable:
echo 0 | sudo tee disable - Caveat: Actual power off still depends on hardware support. For USB3 ports, you might need to disable the 'peer' USB2 port as well [5][1].
- Navigate to the specific port's directory within the hub's device path (e.g.,
- Method C: Control Device Power State (Autosuspend): This usually puts the device into a low-power state or suspend, not a full power off, and depends on driver cooperation [1, 5, 7].
- Find the device directory (e.g.,
/sys/bus/usb/devices/1-2/power/). - Force suspend (if supported):
echo "auto" | sudo tee control(orecho "suspend" > levelon older kernels) and potentiallyecho 0 | sudo tee autosuspend_delay_ms[5][1]. - Wake up/force on:
echo "on" | sudo tee control
- Find the device directory (e.g.,
3. Using
usbreset(Sends Reset Signal, Not Power Cycle)[6]There's a common C utility (
usbreset.c) or pre-built tools that send theUSBDEVFS_RESETioctl command [8]. This asks the device to reset itself, similar to a warm reboot for the device, but doesn't cut power [5, 8].- Compile (if needed): You can find the C code online [8] and compile it:
cc usbreset.c -o usbreset chmod +x usbreset - Identify Device: Use
lsusbto find the bus and device number (e.g., Bus 002 Device 005). - Execute:
Replacesudo ./usbreset /dev/bus/usb/002/005002/005with your actual bus/device numbers [8]. Some packaged versions might allow using the device name [8]. A Python toolusb_resetteralso exists [6][9].
4. Reloading the USB Controller Driver (Drastic)
You can unload and reload the kernel module for the entire USB controller (e.g.,
xhci_hcdfor USB3,ehci_hcdoruhci_hcdfor USB2). This resets the controller and all devices attached to it [13].- Identify Driver: Use
lspci -nnkand look for your USB controllers to see the "Kernel driver in use" [13]. - Unload:
sudo modprobe -r xhci_hcd(orehci_hcd,uhci_hcd). Warning: This will disconnect all devices on that controller (keyboard, mouse included if they use it!). - Reload:
sudo modprobe xhci_hcd(or the one you removed).
In Summary:
- Try
uhubctlfirst. It's the cleanest method if your hardware supports per-port power switching. - If
uhubctldoesn't work, explore the/sysfilesystem methods like unbind/bind or port disable (newer kernels) for a logical reset. - Use
usbresetif you just need to reinitialize the device without cutting power. - Reloading the kernel module (
modprobe -r/modprobe) is a last resort as it affects multiple ports/devices.
Remember to use
sudofor most of these commands as they require root privileges.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.