Skip to content

Systemd configuration files for setting up an openvpn tunnel in a network namespace (without veth pairs)

License

Notifications You must be signed in to change notification settings

conallprendergast/openvpn-netns-systemd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

openvpn-netns-systemd

Executable files for executing arbitrary commands in a seperate OpenVPN namespace.

Description

This project contains files for executing arbitrary commands in OpenVPN namespaces. VPNs established in a namespace do not affect the global network stack, so that the main network connection of the running machine can remain unaffected.

It does not use veth pairs, but instead moves the tun device into the new namespace. It is possible to run multiple VPN namespaces seperatly (in seperate terminals), without polluting the local network configuration.

It is even possible to nest VPN connections (Your PC -> VPN 1 -> VPN 2 -> public internet).

Usage

Shell Usage

./vpn-exec <VPN conf> <command>

eg

./vpn-exec /etc/openvpn/France.conf sudo -u johndoe firefox

This will create a vpn connection to France and execute firefox in the new vpn-France namespace. It ensures that any traffic generated by this instance of firefox is tunnelled over VPN, but it does not affect the networking configuration of the running system.

(Note: In the example above, it is necessary to close other running instances of firefox, to ensure our firefox process does not bind to another in the global network namespace)

The vpn-exec script can be executed multiple times in different terminals, effectively putting each terminal in a different VPN.

To nest VPN connections, simply execute the vpn-exec script again, while in a vpn-exec /bin/bash session.

Systemd Usage

The VPN scripts are implemented as systemd units.

The example daily-backup.timer and daily-backup.service show how to leverage this to implement a timed backup script through VPN without affecting the global network namespace. Given an openvpn configuration file /etc/openvpn/office.conf

systemctl start openvpn-ns@office.service

Using the new network namespaces from above:

sudo ip netns exec vpn-office <command>

eg.

sudo ip netns exec vpn-office /bin/bash
su - <user>
firefox

Stopping the VPN namespace

systemctl stop openvpn-ns@<VPN>.service

or to stop all:

systemctl stop 'openvpn-ns@*'

About

Systemd configuration files for setting up an openvpn tunnel in a network namespace (without veth pairs)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages