11.6. Creating a structured custom policy for USB devices
You can organize your custom USBGuard policy in several .conf files within the /etc/usbguard/rules.d/ directory. The usbguard-daemon then combines the main rules.conf file with the .conf files within the directory in alphabetical order.
Prerequisites
-
The
usbguardservice is installed and running.
Procedure
Create a policy which authorizes the currently connected USB devices, and store the generated rules to a new
.conffile, for example,<policy.conf>.# usbguard generate-policy --no-hashes > ./<policy.conf>The
--no-hashesoption does not generate hash attributes for devices. Avoid hash attributes in your configuration settings because they might not be persistent.Open the
<policy.conf>file with a text editor of your choice, and select the lines with the rules that you want to record, for example:... allow id 04f2:0833 serial "" name "USB Keyboard" via-port "7-2" with-interface { 03:01:01 03:00:00 } with-connect-type "unknown" ...Copy the selected lines into a separate
.conffile.참고The two digits at the beginning of the file name specify the order in which the daemon reads the configuration files.
For example, to copy the rules for your keyboards into a new
.conffile:# grep "USB Keyboard" ./<policy.conf> > ./<10keyboards.conf>Install the new policy to the
/etc/usbguard/rules.d/directory.# install -m 0600 -o root -g root <10keyboards.conf> /etc/usbguard/rules.d/<10keyboards.conf>Move the rest of the lines to the main
rules.conffile.# grep -v "USB Keyboard" ./policy.conf > ./rules.confInstall the remaining rules.
# install -m 0600 -o root -g root rules.conf /etc/usbguard/rules.confRestart the
usbguarddaemon to apply your changes.# systemctl restart usbguard
Verification
Display all active USBGuard rules.
# usbguard list-rules ... 15: allow id 04f2:0833 serial "" name "USB Keyboard" hash "kxM/iddRe/WSCocgiuQlVs6Dn0VEza7KiHoDeTz0fyg=" parent-hash "2i6ZBJfTl5BakXF7Gba84/Cp1gslnNc1DM6vWQpie3s=" via-port "7-2" with-interface { 03:01:01 03:00:00 } with-connect-type "unknown" ...Display the contents of the
rules.conffile and all the.conffiles in the/etc/usbguard/rules.d/directory.# cat /etc/usbguard/rules.conf /etc/usbguard/rules.d/*.conf- Verify that the active rules contain all the rules from the files and are in the correct order.