/go-vpn-linux

Creating a VPN on Linux using a TUN interface

Primary LanguageGoMIT LicenseMIT

How to create a VPN on Linux using a TUN device

  1. Ensure that the TUN module is loaded.
lsmod | grep tun

If the above command produces no output then the TUN module is not loaded.

You can load the TUN module by running the command below.

sudo modprobe tun

If the above command produces and error this means the module is not installed. Run the following command to install the TUN module (on Arch Linux).

sudo pacman -S linux-headers
  1. Create the TUN interface

You can use the ip command to create a TUN interface.

sudo ip tuntap add mode tun name <tun-interface-name>
  1. Check if the TUN device has been created
ip link show dev <tun-interface-name>
  1. Bring the device UP

You need to bring-up the interface. When an interface is up it means that the operating system has configured the interface and it is ready to send and receive network traffic. To bring up the interface, run the command below.

sudo ip link set <tun-interface-name> up
  1. Assign an IP and Subnet Mask to the interface

Once the interface is up, you need to assign an IP and a Subnet Mask to it. When assigning an IP and Subnet Mask, make sure that the network does not conflict with any other network. You are actually acting as a router in the sence that a router assigns IP addresses.

sudo ip addr add 192.168.1.1/24 dev <tun-interface-name>
  1. Delete the TUN interface

You can delete the TUN interface using the command below

sudo ip tuntap del dev <tun-interface-name> mode tun

Install net-tools for Arch Linux

By installing net-tools you get access to tools such as:

  • ifconfig
  • netstat
  • route
  • and more ...

You can search for the core packge using yay

yay -Ss net-tools

then install the package using the command

yay -S core/net-tools <I guess this is the package name>