Disable USB Ports on FreeBSD for Security
In this article
You can disable USB ports on FreeBSD by preventing the kernel from loading the USB host controller drivers at boot, or by unloading them at runtime. The most reliable method is a kernel module blacklist in /boot/loader.conf combined with a devfs rule to block new device nodes. This stops USB storage, keyboards, and network adapters from being recognized, which helps prevent data theft, BadUSB attacks, and unauthorized device access. Before you start, make sure you have an alternative way to access the machine — serial console, SSH over the network, or IPMI/KVM — because disabling USB also disables USB keyboards and mice. You will need root access and a backup of your configuration files.
What You'll Need
- Root or sudo access on the FreeBSD system.
- An out-of-band access method (SSH, serial console, or IPMI) — do not rely on a USB keyboard after the change.
- A backup of
/boot/loader.confand/etc/devfs.rules. - Basic familiarity with editing files in
vioree.
Step-by-Step
Confirm the USB stack is loaded. Run
kldstat | grep -i usbto see which USB modules are active (commonlyusb,uhci,ohci,ehci,xhci,umass,ukbd,ums). Note them down; you will block these.Back up your boot configuration. Copy the file:
cp /boot/loader.conf /boot/loader.conf.bak. Also back up/etc/devfs.rulesif it exists:cp /etc/devfs.rules /etc/devfs.rules.bak.Blacklist the USB kernel modules. Edit
/boot/loader.confand add lines that prevent the modules from loading at boot. For each module you noted, add a line likemodule_name_load="NO". For example:usb_load="NO" uhci_load="NO" ohci_load="NO" ehci_load="NO" xhci_load="NO" umass_load="NO" ukbd_load="NO" ums_load="NO"
This stops the kernel from attaching USB host controllers and device drivers at startup. The exact module names can differ slightly by FreeBSD version; checkkldstatoutput.Unload the modules now (optional, for immediate effect). If you are on SSH or serial console, you can unload them without rebooting:
kldunload umass,kldunload ukbd,kldunload xhci(or whichever are loaded). Unloading the host controller driver will disconnect all USB devices immediately, including any USB keyboard — make sure you have remote access first.Block device nodes with devfs (defense in depth). Edit
/etc/devfs.rulesand add a rule set that hides USB device nodes. For example:[usbblock=10] add path 'usb*' mode 000 add path 'da*' mode 000 add path 'cd*' mode 000
Then apply it in/etc/devfs.confor viadevfs_system_ruleset="usbblock"in/etc/rc.conf. This prevents even a loaded driver from exposing device nodes to users. Note thatda*also covers SATA disks, so adjust the paths to match only USB-attached devices if you need internal disks accessible.Reboot to apply the boot-time changes. Run
reboot. After the system comes up, verify withkldstat | grep -i usb— you should see no USB modules. Also checkusbconfig list; it should report no devices or fail to find a controller.Verify that USB devices are not recognized. Plug in a USB stick and run
dmesg | tail. You should see no attachment messages.camcontrol devlistshould not list the stick. If a device still appears, revisit step 3 and confirm the module names match your system.
Common Mistakes
- Locking yourself out. Disabling USB also disables USB keyboards and mice. Always confirm SSH or serial access works before rebooting.
- Blocking all
da*devices. The devfs rule above can hide SATA disks too. Use narrower paths or skip the devfs step if you are unsure. - Using the wrong module names. Module names vary between FreeBSD versions and hardware. Always check
kldstatfirst. - Forgetting the loader.conf syntax. A typo in
loader.confcan prevent boot. Keep the backup and be ready to boot from a rescue environment. - Assuming a reboot is enough. If a USB module was loaded dynamically before you edited the file, it stays loaded until you unload it or reboot.
Tips
- Test changes on a non-production machine or VM first; FreeBSD runs well in virtual machines with USB passthrough disabled.
- Use
sysrcto set rc.conf variables safely:sysrc devfs_system_ruleset=usbblock. - If you only need to block USB storage (not keyboards), blacklist just
umassand leave the host controller drivers loaded. This is less disruptive. - Keep a copy of your working
loader.confon another machine or in version control. - Document the change so future administrators know why USB is disabled.
Troubleshooting
- System does not boot after the change. Boot into single-user mode or from a rescue USB/CD, mount the root filesystem, and restore
/boot/loader.conf.bak. - USB devices still appear. Check
kldstatfor loaded modules and unload them manually. Confirm yourloader.conflines are spelled correctly and that you edited the right file. - SSH stops working after reboot. If your network adapter is USB-based, disabling USB will cut network access. Use a non-USB NIC or serial console.
- devfs rule hides internal disks. Remove or narrow the
da*rule, then reload devfs withservice devfs restart. - Cannot unload a module. Some modules are in use by mounted filesystems or active devices. Unmount USB storage first, then retry
kldunload.
Frequently Asked Questions
Does disabling USB ports also disable USB keyboards?
Yes, if you blacklist the host controller drivers (xhci, ehci, etc.) or ukbd. To keep keyboards working, block only umass (USB storage).
Can I re-enable USB later?
Yes. Remove or comment out the _load="NO" lines in /boot/loader.conf, remove the devfs rule, and reboot. Or load modules manually with kldload.
Is this reversible without a reboot?
Partially. You can kldload modules back at runtime, but boot-time blacklists require a reboot to take full effect.
Will this stop a BadUSB attack?
It prevents the kernel from recognizing the device, so malicious firmware cannot present itself as a keyboard or storage device. It is an effective mitigation.
Does this work on FreeBSD jails?
Jails share the host kernel, so USB control must be done on the host. Jails cannot load or unload kernel modules.
What if I need USB for a specific device?
Use a narrower approach: allow the host controller but blacklist umass and ukbd, or use devfs rules to permit only specific device nodes.