USB memory sticks can be used to steal data from your Linux computer.

Here’s how it works and how you could set it up.

They’re cheap, effective, portable, and easy to use.

freer-shutterstock_697210159

Freer / Shutterstock

Gone are the days of having to mount them by hand on the command line.

If other people use your machine you might want to limit what they can do with USB memory sticks.

Perhaps your kids regularly have friends over to play.

Using lsusb to list connected USB devices

Locking down USB access is a sensible precaution to stop them from inadvertently causing issues.

What Is USBGuard?

It’s not just for USB memory sticks.

Installing USBGuard on Ubuntu with apt

It’s something like afirewallfor USB connectivity.

The USBGuard daemon runs as soon as it is installed.

double-check you configure USBGuard straight after installing it.

Installing USBGuard on Fedora with dnf

If you don’t, all your USB devices will be blocked when youreboot your setup.

It’s a simple process.

We insert a USB memory stick and see what happens.

Installing USBGuard on Manjaro with pacman

We hear an audible alert sound, and a memory stick icon appears in the dock.

Opening the file internet tool shows an entry has been added to the list of locations in the sidebar.

The name displayed is the one given to the unit when it was formatted.

Creating a base USBGuard policy with a root terminal session

Opening a terminal andusing thelsusbcommandlists the connected USB devices.

The top entry is the memory stick in question, which happens to be a TDK-branded gadget.

On the latestManjaro,Fedora, andUbuntubuilds that we tested, these were already installed.

Using cat to list the auto-generated rules in /etc/usbguard/rules.conf

It has a command that creates a rule allowing all currently connected USB devices to continue work unhindered.

That means you might create a baseline configuration for all of your always-required devices.

This set of rules is called a base policy.

Using lsusb to list connected USB devices

USBGuard uses three types of rules.

USBGuard has a neat trick.

It is also a convenient way to capture trusted, intermittent devices.

Using the list-devices command to list blocked, connected, devices

Just double-check all of your trusted devices are connected to your system when you issue the command.

Related:How to Control sudo Access on Linux

An odd quirk requires you to do this asroot.

Usingsudowith the command doesn’t work.

Giving a USB device temporary access

We need to usesudo -i(login) command toopen a shell as root, then issue the command.

confirm you use theexitcommand to leave the root login session once you’ve finished.

The-X(–no-hashes) option prevents USBGuard from generating hash attributes for each rig.

USBGUard finding no blocked devices

The-t(target) option sets a default target for all unrecognized USB devices.

In our case we’ve chosen “reject.”

We could also have chosen “block.”

Giving a device permanent access by adding an allow rule to the policy

To see our new rules, we can usecat.

On our test computer, this detected three USB devices and created “allow” rules for them.

It added “reject” as the target for all other USB devices.

Obtaining a USB device’s ID number using the list-devices command

But because we used a “reject” target for unrecognized devices,lsusbcan list its details.

This shows the currently connected, but blocked, USB devices.

To give our equipment temporary access, we’ll use the equipment ID number.

Permanently blocking a USB device’s access using the block-device command

In our example this is “10.”

Our machine is connected and appears in the dock and the file surfing app.

If we ask USBGuard to list the blocked devices, none are listed.

We can make the permission permanent by using the-p(permanent) option.

This creates a rule for us and adds it to our policy.

We can now use this USB equipment as normal.

We need to know the gadget ID.

We can find this by listing the allowed devices.

In our case the ID is “13.”

Note that this immediately disconnects the gadget.

Only use this command when you’ve finished using any data on the rig.

It’s your setup, so it’s only fair you get to choose.

Related:How to Mount and Unmount Storage Devices from the Linux Terminal