How to use a “hardware key” to control Linux networking

hardwarenetworking

Sometimes I have trouble controlling myself while in front of the computer and I tend to procrastinate a lot doing Internet surfing. Fortunately I do not need Internet for most of my work.

So I would like to have a way to completely disable networking on Linux. To enable it again I would need a USB stick which would act as a key.

Do you have any advice how one could go about implementing such a scheme? Ideally the enabling/disabling of networking should be as quick and automatic as possible. Also I would like a solution which, once implemented, is easy to set up on different machines.

EDIT: I have a similar set-up for my laptop, but it is not as automatic as I like. I simply delete all the wireless drivers and keep a copy of them on a USB stick. But this solution is not easily portable. So I am looking for ideas how to improve it.

Best Answer

Perhaps combine udev rules and iptables trick?

When target usb device is added, execute iptables -D OUTPUT -j DROP; when removed, execute iptables -I OUTPUT DROP

For detail on the script, take a look at the guide here

(Use lsusb to retrieve vendor information)

EDIT

Well, this is designed for non-root users, for otherwise, it's impossible to block the root user from restoring it, unless the usb key is the internet access media.

Related Question