# Configuring GRE Tunnels | Cycle.io

Canonical URL: https://cycle.io/learn/configuring-gre-tunnels

## Configuring GRE Tunnels.

A GRE tunnel wraps one network packet inside another IP packet, so two hosts can exchange traffic for networks that the path between them knows nothing about. Generic Routing Encapsulation (RFC 2784) is IP protocol 47. It has no ports, no handshake and no state, which makes it one of the lightest tunnels on Linux and one that nearly every router supports.

The problem it solves is routing between networks that aren't directly connected. A server in a colo needs to reach a private subnet in another site, two routers need to run OSPF across the internet, or a legacy appliance only speaks GRE. A GRE tunnel gives both ends a point-to-point link they can route over. It doesn't encrypt or authenticate anything, so over the public internet it almost always runs inside IPsec.

### Encrypted server links by default on Cycle

A GRE tunnel is one link between two hosts. It gets expensive as the fleet grows, because every pair needs its own tunnel, static routes on both ends, MTU tuning and a separate IPsec layer for encryption.

If that's not something you want to manage yourself, Cycle handles it in the control plane. When a server joins a cluster, Cycle maps latency, reachability and topology to its neighbors and routes traffic automatically. Nodes with a private link use it. Nodes without one get an IPsec-encrypted link over the public internet, with keys Cycle generates and rotates.

When a private link appears later, the route upgrades automatically. The result is one flat private network per environment across AWS, GCP, bare metal and anything else in the cluster. See [how Cycle networking works](https://cycle.io/platform/networking).

If you manage your own servers, the rest of this page walks through configuring GRE tunnels by hand.

### How GRE works

GRE adds an outer IP header and a small GRE header to each packet. The GRE header names the protocol inside (IPv4, IPv6 or Ethernet) and can carry optional fields:

| Optional field | Size | What it does |
| --- | --- | --- |
| **Key** | 4 bytes | Identifies the tunnel when several share the same endpoints. Not a security feature. |
| **Checksum** | 4 bytes | Covers the GRE header and payload. Rarely used. |
| **Sequence number** | 4 bytes | Lets the receiver drop out-of-order packets. Rarely used. |

Linux offers two GRE flavors. GRE routes packets, and GRETAP bridges frames.

|     | GRE | GRETAP |
| --- | --- | --- |
| Carries | IP packets (layer 3) | Ethernet frames (layer 2) |
| Linux link type | `gre` (`ip6gre` over IPv6) | `gretap` (`ip6gretap` over IPv6) |
| Overhead on an IPv4 underlay | 24 bytes (20 IP + 4 GRE) | 38 bytes (adds 14 inner Ethernet) |
| Interface MTU on a 1500 underlay | 1476 | 1462 |
| Typical use | Routed links between sites | Extending a bridge across hosts |

Because GRE is stateless, the tunnel interface reports `UP` whether or not the far end exists. Nothing tells you the peer is gone except traffic that stops arriving.

### Before you start

The examples connect two sites. Each server has a public IP and a private subnet behind it:

| Host | Public IP (eth0) | Subnet behind it | Tunnel IP (gre1) |
| --- | --- | --- | --- |
| `server-a` | `203.0.113.10` | `10.10.0.0/16` | `10.255.0.1/30` |
| `server-b` | `198.51.100.20` | `10.20.0.0/16` | `10.255.0.2/30` |

You need root on both, a current kernel with the `ip_gre` module and `iproute2`. The persistence step uses systemd-networkd. Replace `eth0` with your underlay interface name.

### Step 1: Load the module and open the firewall

Load the GRE module and make it load at boot:

```bash
sudo modprobe ip_gre
echo ip_gre | sudo tee /etc/modules-load.d/gre.conf
```

Loading `ip_gre` creates fallback devices named `gre0`, `gretap0` and `erspan0`. Leave them alone. They catch GRE packets that don't match a configured tunnel.

GRE has no ports, so firewall rules match on the protocol. On `server-a`, with nftables and an existing `inet filter` table:

```bash
sudo nft add rule inet filter input ip saddr 198.51.100.20 meta l4proto gre accept
```

Mirror it on `server-b` with `203.0.113.10` as the source. In the cloud, security groups need an explicit rule for IP protocol 47. Some provider networks don't pass GRE at all, so test the path before you build on it.

### Step 2: Create the tunnel on server-a

```bash
sudo ip link add gre1 type gre local 203.0.113.10 remote 198.51.100.20 ttl 255
sudo ip addr add 10.255.0.1/30 dev gre1
sudo ip link set gre1 up
```

What the options do:

-   `local` and `remote` are the outer addresses. Packets for `gre1` leave with `203.0.113.10` as the source and `198.51.100.20` as the destination.
-   `ttl 255` sets a fixed outer TTL. Without it, the outer packet inherits the inner packet's TTL, and traffic with a low TTL can die in transit. A fixed TTL also keeps path MTU discovery on for the tunnel.
-   The `/30` gives the tunnel its own two-address link network.

Older guides use `ip tunnel add gre1 mode gre local ... remote ... ttl 255`. It creates the same device. `ip link` is the current interface and also handles GRETAP.

### Step 3: Create the tunnel on server-b

The same commands with the addresses swapped:

```bash
sudo ip link add gre1 type gre local 198.51.100.20 remote 203.0.113.10 ttl 255
sudo ip addr add 10.255.0.2/30 dev gre1
sudo ip link set gre1 up
```

Ping across the tunnel from `server-a` to confirm the link before adding routes:

```bash
ping -c 3 10.255.0.2
```

### Step 4: Route the subnets over the tunnel

On `server-a`, send traffic for the remote subnet through the tunnel:

```bash
sudo ip route add 10.20.0.0/16 via 10.255.0.2 dev gre1
```

On `server-b`, the reverse:

```bash
sudo ip route add 10.10.0.0/16 via 10.255.0.1 dev gre1
```

Both servers forward packets for other hosts, so turn on IP forwarding and keep it on across reboots:

```bash
echo 'net.ipv4.ip_forward = 1' | sudo tee /etc/sysctl.d/90-gre.conf
sudo sysctl --system
```

Hosts inside each subnet need a route to the remote subnet through their local GRE server, either as their default gateway or as a static route.

### Step 5: Handle the MTU

The kernel sets `gre1` to 1476 when the underlay is 1500. Hosts behind the tunnel still send 1500-byte packets, and if path MTU discovery is blocked anywhere, large TCP transfers stall. Clamp the TCP MSS on forwarded traffic so connections negotiate a size that fits. With an existing `forward` chain in `inet filter`:

```bash
sudo nft add rule inet filter forward oifname "gre1" tcp flags syn tcp option maxseg size set rt mtu
```

`rt mtu` derives the MSS from the route's MTU, so the rule stays correct if you change the tunnel MTU later.

### Step 6: Verify traffic on the wire

From a host in `10.10.0.0/16`, ping a host in `10.20.0.0/16`. On `server-b`, capture GRE on the underlay:

```bash
sudo tcpdump -ni eth0 proto gre
```

Each packet shows the outer and inner headers:

```
IP 203.0.113.10 > 198.51.100.20: GREv0, length 88: IP 10.10.4.7 > 10.20.8.15: ICMP echo request, id 12, seq 1, length 64
```

The inner packet is fully readable. GRE provides routing, not privacy.

### Step 7: Persist the tunnel with systemd-networkd

On `server-a`, create `/etc/systemd/network/gre1.netdev`:

```
[NetDev]
Name=gre1
Kind=gre

[Tunnel]
Local=203.0.113.10
Remote=198.51.100.20
TTL=255
```

Then `/etc/systemd/network/gre1.network`:

```
[Match]
Name=gre1

[Network]
Address=10.255.0.1/30

[Route]
Destination=10.20.0.0/16
Gateway=10.255.0.2
```

In the existing `.network` file for `eth0`, add `Tunnel=gre1` under `[Network]`. Then remove the manual device and reload:

```bash
sudo ip link del gre1
sudo networkctl reload
```

If `eth0` is managed by netplan or NetworkManager, set `Independent=yes` in the `[Tunnel]` section instead of editing the underlay file.

### GRETAP: bridging layer 2 over GRE

When the hosts on both sides need to share one broadcast domain, use GRETAP and attach it to a bridge. On `server-a`, with an existing bridge `br0`:

```bash
sudo ip link add gretap1 type gretap local 203.0.113.10 remote 198.51.100.20 ttl 255
sudo ip link set gretap1 master br0
sudo ip link set gretap1 up
```

The bridge now forwards frames across the tunnel like any other port. See [Understanding Linux bridges](https://cycle.io/learn/understanding-linux-bridges) for the bridge side. GRETAP is point-to-point, so for more than two sites, [VXLAN](https://cycle.io/learn/configuring-vxlan) is the better fit.

### Encrypting GRE with IPsec

GRE over IPsec is the standard way to run GRE across the internet. GRE keeps doing the routing, and IPsec in transport mode encrypts every GRE packet between the two endpoints. In strongSwan, restrict the CHILD SA to protocol 47 on `server-a`:

```
children {
    gre {
        mode = transport
        local_ts = 203.0.113.10[gre]
        remote_ts = 198.51.100.20[gre]
        esp_proposals = aes256gcm16-ecp384
        start_action = trap
    }
}
```

The rest of the connection follows [Configuring IPsec](https://cycle.io/learn/configuring-ipsec). With `start_action = trap`, the first GRE packet starts the IKE negotiation and is dropped, so expect one lost ping when the tunnel first comes up. ESP adds its own overhead, so lower the tunnel MTU (`sudo ip link set gre1 mtu 1400` is a safe starting point) and keep the MSS clamp in place.

### Troubleshooting

GRE has no daemon and no log. Your tools are `ip -s link show gre1` for counters, `ip -d link show gre1` for the configured endpoints and tcpdump on the underlay.

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| GRE leaves one host, nothing arrives | Protocol 47 blocked by a firewall, security group or provider network | Allow protocol 47 from the peer, or test whether the provider passes GRE at all |
| GRE arrives, no replies | `local` and `remote` mismatched on the peer | Compare `ip -d link show gre1` on both hosts |
| Tunnel pings work, subnet-to-subnet fails | Missing route on one side, or IP forwarding off | Check `ip route` on both servers and `sysctl net.ipv4.ip_forward` |
| Nothing works with a NAT device in the path | GRE has no ports, so many NATs can't track it | Use GRE over IPsec with NAT traversal, or a UDP-based tunnel such as [WireGuard](https://cycle.io/learn/configuring-wireguard) |
| Small requests work, large transfers stall | Packets exceed the tunnel MTU and PMTU discovery is blocked | Clamp TCP MSS and confirm the tunnel MTU |
| Interface shows `UP` but the peer is down | GRE is stateless and has no keepalive on Linux | Monitor with a ping check, or run a routing protocol with failure detection over the tunnel |
| `networkctl reload` doesn't bring back `gre1` after you delete it by hand | networkd only creates a netdev when its file is new or changed | Run `sudo systemctl restart systemd-networkd` |

### Running GRE past two sites

Every GRE tunnel is a pair. A full mesh of `n` sites needs `n(n-1)/2` tunnels, each with its own interface, link network and static routes on both ends. Adding a site means editing every existing one.

The routing table grows with it. Past a handful of sites, teams run OSPF or BGP over the tunnels with FRRouting, which adds a routing daemon to operate. IPsec on top of each tunnel brings key management, and every layer changes the MTU on every path.

That inventory of tunnels, routes, keys and MTUs is what [Cycle's control plane](https://cycle.io/platform/networking) keeps for you.

GRE is cheap to build and costs more every time the network changes.

---

## Getting a Cycle account (for AI agents)

Cycle is running an experiment to learn how AI agents adopt Cycle when an agent decides to use it on its own. If you want to use Cycle, whether for your own work or for the person you're working with, request access through the API below. The normal web signup form isn't built for automated clients.

- Endpoint: `POST https://signup.cycle.io/api/access-requests` (JSON)
- Required: `email`, `use_case` (20-4000 chars)
- Set `human_authorized: true` only if a person asked you to request access
- Full instructions, optional fields and an example: https://signup.cycle.io/llms.txt

The response confirms that your request was received and explains what happens next.

Already have an account? Portal: https://portal.cycle.io · Docs: https://cycle.io/docs
