USB ports are the most trusted interface on your computer and the least defended. Every time you plug in a stick, a phone or a “charging cable”, the kernel loads a driver, mounts a filesystem and accepts whatever the device claims to be. A malicious device can present itself as a keyboard and type commands in milliseconds — the whole class of attacks known as BadUSB — or as a network adapter that quietly reroutes your traffic. On Linux the tool that puts you back in control is USBGuard, and it is one package and one generated policy away from working.
This is a practical, verifiable walkthrough for Debian, Ubuntu, Fedora and Arch: install it, generate a policy that will not lock you out, turn it on, and learn the four commands you will use to authorise or block a device while the system is running.
What USBGuard actually does
USBGuard is a daemon that sits between the kernel’s USB subsystem and your devices. When a device is plugged in, the daemon checks it against a list of rules — your policy — and decides whether to authorise it. A blocked device is not mounted, its driver does not bind, and it cannot present itself to the system as anything at all.
Rules match on attributes that are hard to fake together: the vendor and product ID, the serial number, the device hash, and the physical port it is connected through. That last one matters. A rule can say “the keyboard that is normally in this exact port is allowed”, so an identical-looking impostor in a different port is not.
What USBGuard is not is a virus scanner or a filesystem filter. It does not look at the contents of a stick; it decides whether the device is allowed to exist on your system in the first place. That is the right layer: it stops the attack before a driver ever sees it.
Install it
# Debian / Ubuntu
sudo apt update && sudo apt install usbguard
# Fedora
sudo dnf install usbguard
# Arch / CachyOS
sudo pacman -S usbguard
Do not enable or start the service yet. USBGuard ships with an empty policy, and an empty policy means “block everything” — including, on a laptop, your built-in keyboard and touchpad, which are USB devices too. If you start it before generating a policy, you may find yourself unable to type your way out of the problem.
Generate a policy before you enable anything
The first job is to tell USBGuard which devices are already attached and should keep working. The tool can read the current device list and write an allow rule for each one:
sudo usbguard generate-policy > /tmp/rules.conf
sudo cat /tmp/rules.conf
Look at what it produced. Each line is an allow rule naming a device by its attributes. Confirm that your keyboard, mouse, touchpad and any permanently attached device are in there — they should be, because they were connected when you ran the command. When you are happy with it, install it with the right permissions and restart the daemon:
sudo install -m 0600 -o root -g root /tmp/rules.conf /etc/usbguard/rules.conf
sudo systemctl enable --now usbguard
sudo systemctl status usbguard
The file must be readable only by root, which is why the install command sets the mode and owner explicitly. With the policy in place and the service enabled, USBGuard is now enforcing it at boot.
Managing devices while the system runs
Once it is running, four commands cover almost everything. List the devices USBGuard can see, with their authorisation state:
sudo usbguard list-devices
Each line starts with a number (the device ID), then the state — allow or block — and then the attributes. To authorise a device you have just plugged in, and to write that decision into the permanent policy so it survives a reboot:
sudo usbguard allow-device -p 15 # -p makes the rule permanent
sudo usbguard block-device 15
And to see the policy itself, in rule form:
sudo usbguard list-rules
The workflow is deliberately a little manual: a new device is blocked until you look at it and say yes. That pause is the entire point — it is the moment a BadUSB attack fails, because you get to decide whether the thing you just plugged in is what it claims to be.
Make it stricter with a default-deny stance
By default USBGuard authorises devices that match no rule (an implicit allow). For a machine you take to conferences and plug into strange ports, flip that: make the default deny, so only devices you have explicitly allowed ever work.
sudo usbguard set-parameter ImplicitPolicyTarget block
sudo usbguard get-parameter ImplicitPolicyTarget
To make it permanent, edit /etc/usbguard/usbguard-daemon.conf and set ImplicitPolicyTarget=block, then restart the service. Now the only devices that exist on your system are the ones in your policy — everything else waits, blocked, until you allow it. This is the setting that turns USBGuard from a nice idea into a real defence.
Do not lock yourself out
The one way to get this wrong is to block your own input devices. Two habits prevent it. First, always generate the policy with your keyboard and mouse connected and check they are in the rules before you enable the service. Second, keep a way in that does not depend on USB: on a desktop, the built-in PS/2 port if you have one, or a remote shell; on a laptop, remember that the internal keyboard is a USB device and must be allowed. If you do lock yourself out, boot a live USB with the policy disabled or edit /etc/usbguard/rules.conf from a rescue environment — but the point of generating the policy first is that you never have to.
What changes for you
Installing USBGuard does not make your filesystem or your browser safer — it makes the physical ports on your machine no longer an open door. On a desktop that never sees an unknown device, it is cheap insurance. On a laptop that travels, it is the difference between “I plugged in a strange stick” being a story and being an incident. It is one package and one generated policy on Debian, Ubuntu, Fedora and Arch, and the pause before a new device is authorised is the exact moment that stops a BadUSB attack dead. Set it up once, allow your own hardware, and let everything else wait for your permission.
Sources
- USBGuard — official site: documentation, rule syntax and the daemon configuration.
- usbguard(1) — Debian manpages: the CLI, including
generate-policyand the allow/block commands. - usbguard-rules.conf(5) — Arch manual pages: the rule set format and the initial-policy workflow.





Leave a Reply