Here is an easy way to make use of IPv4 GRE tunnels in Ubuntu, or any other Debian based distro. You will need to edit /etc/network/interfaces
.
Here is a template with the information you will need to add:
auto tun1 iface tun1 inet static address <tunnel IP> netmask <tunnel subnet mask> pre-up iptunnel add tun1 mode gre local <local IP> remote <remote IP> ttl 255 up ifconfig tun1 multicast pointopoint <remote tunnel IP> post-down iptunnel del tun1
auto tun1
is used by the /etc/init.d/networking script. Just like the 6in4 tunnel, the auto parameter will instruct the script to automatically start or stop the interface. The script will get called during startup and will bring up this interface automatically. This line is entirely optional and depends on your personal preference.
iface tun1 inet static
starts the configuration block for a new IPv4 interface. This is the interface of the tunnel we are about to create that will encapsulate traffic destined for the other side of the tunnel.
address <tunnel IP>
is the IP address you wish to assign to this machine’s side of the GRE tunnel.
netmask <tunnel subnet mask>
is the subnet mask of the tunnel. I highly suggest using a 255.255.255.252 subnet mask as this tunnel will be point-to-point and there is no reason to waste address space, even if it is private addressing. If you are unfamiliar with how subnet masks are used, please refer to Subnetwork on Wikipedia.
pre-up iptunnel add tun1 mode gre local <local IP> remote <remote IP> ttl 255
– Thepre-up
parameter tells the init script to run the iptunnel
command prior to bringing up the interface. This is where we are actually creating the tunnel and telling it to use GRE mode.<local IP>
is this machine’s IP address of an interface on which you want to run this tunnel. For example, if eth0 had the address of 71.31.47.23, you could set the local IP address above to that address in order to have this tunnel use eth0. <remote IP>
is the global IP address where the other side of the tunnel exists.
up ifconfig tun1 multicast
– The up
parameter tells the init script to run ifconfig tun1 multicast
once the interface is up. In this case, we are enabling multicast on this interface. This is particularly useful if you wish to run a routing protocol over this tunnel, such as OSPF.
pointopoint <remote tunnel IP>
– The remote IP here is the IP address of the other side of the GRE tunnel. For example, if your side of the tunnel is 172.31.10.1 and their side is 172.31.10.2, then 172.31.10.2 would be the IP address to specify here.
post-down iptunnel del tun1
– Just like pre-up
, post-down
tells the init script to run this command after the interface has been shutdown. In this case, we are deleting the tunnel we created earlier with the pre-up
command.
You can name this tunnel interface something that is more meaningful than tun1
.