How to create a GRE tunnel on Linux

Question: I want to connect to remote networks by using a GRE tunnel. How can I create a GRE tunnel between two end points on Linux?

GRE tunnels are IP-over-IP tunnels which can encapsulate IPv4/IPv6 and unicast/multicast traffic. To create a GRE tunnel on Linux, you need ip_gre kernel module, which is GRE over IPv4 tunneling driver.

So first make sure that ip_gre is loaded.

$ sudo modprobe ip_gre
$ lsmod | grep gre
ip_gre                 22432  0
gre                    12989  1 ip_gre

Here, we assume that you want to create a GRE tunnel between two interfaces with the following IP addresses.

  • Host A:
  • Host B:

On host A, run the following command.

$ sudo ip tunnel add gre0 mode gre remote local ttl 255
$ sudo ip link set gre0 up
$ sudo ip addr add dev gre0

In the above, we create a GRE-type tunnel device called gre0, and set its remote address to Tunneling packets will be originating from (local IP address), and their TTL field will be set to 255. The tunnel device is assigned IP address with netmask

Now verify that route for the GRE tunnel is set up correctly:

$ ip route show
default via dev eth0  proto static dev gre0  proto kernel  scope link  src

On host B, run similar commands as follows.

$ sudo ip tunnel add gre0 mode gre remote local ttl 255
$ sudo ip link set gre0 up
$ sudo ip addr add dev gre0

At this point, a GRE tunnel should be established between host A and host B.

To verify that, from one tunneling end point, ping the other end point.

$ ping (from host A)
PING ( 56(84) bytes of data.
64 bytes from icmp_req=1 ttl=64 time=0.619 ms
64 bytes from icmp_req=2 ttl=64 time=0.496 ms
64 bytes from icmp_req=3 ttl=64 time=0.587 ms

If you want to tear down the GRE tunnel, run the following command from either end.

$ sudo ip link set gre0 down
$ sudo ip tunnel del gre0

Subscribe to Ask Xmodulo

Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

4 thoughts on “How to create a GRE tunnel on Linux

  1. Very good easy tutorial. I directly linked two of my laptops, assigned static ip's to them and then used above to enable tunneling. The static assigned ip's may not stay for long, and reset sometimes, so before pinging check ifconfig to make sure your ip's whatever you put in local and remote are present in respective hosts

Leave a comment

Your email address will not be published. Required fields are marked *