Operating System CentOS

How to Set Up a GRE Tunnel Between Two AlmaLinux 9 / Rocky Linux 9 Servers

Build a GRE tunnel between two RHEL 9 family servers with NetworkManager, allow protocol 47 in firewalld, route extra networks through it and troubleshoot it.

A GRE (Generic Routing Encapsulation) tunnel wraps IP packets inside other IP packets, so two servers on the public internet can talk to each other over a private point-to-point link and route other networks across it. This guide builds a persistent GRE tunnel between two servers running AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9, using NetworkManager's nmcli. At the end both servers can reach each other on private tunnel addresses, the tunnel comes back after a reboot, and the firewall only accepts GRE from the other endpoint.

CentOS 7 reached end of life in June 2024. Older tutorials for it create the tunnel with an ifcfg-tun0 file in /etc/sysconfig/network-scripts and bring it up with ifup. The network-scripts package no longer exists in the RHEL 9 family; NetworkManager manages tunnels there and stores them as keyfiles in /etc/NetworkManager/system-connections/.

Requirements

  • Two servers running AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9, each with a static public IPv4 address.
  • A user with sudo rights on both.
  • A network path that passes IP protocol 47 (GRE) between the two servers. GRE has no ports, so it does not pass through ordinary port-forwarding NAT, and some providers filter it. If the tunnel stays silent after you follow every step, ask your provider whether protocol 47 is allowed.

GRE does not encrypt or authenticate anything. Treat the tunnel like a plain cable across the internet: use it for routing, and run TLS, SSH, IPsec or WireGuard on top when the traffic must stay private.

The examples use these addresses. Replace them with your own.

ServerPublic addressTunnel address
A203.0.113.1010.0.1.1/30
B198.51.100.510.0.1.2/30

The tunnel interface is named gre1 on both sides. Do not use gre0, gretap0 or erspan0: the kernel creates devices with those names by itself as soon as the ip_gre module loads.

Step 1: Allow GRE through the firewall

firewalld is enabled by default on these systems and drops incoming GRE packets. On server A, accept protocol GRE from server B only:

sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="198.51.100.5" protocol value="gre" accept'
sudo firewall-cmd --reload

On server B, accept it from server A:

sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.10" protocol value="gre" accept'
sudo firewall-cmd --reload

If your provider has its own firewall in front of the server (security groups or a network firewall in the hosting control panel), allow protocol 47 from the other endpoint there as well.

Step 2: Create the tunnel on server A

Create the tunnel connection. local is this server's public address, remote is the other server's:

sudo nmcli connection add type ip-tunnel ip-tunnel.mode gre con-name gre1 ifname gre1 remote 198.51.100.5 local 203.0.113.10

Give the tunnel a static address and switch off IPv6 on it, since this tunnel only carries IPv4:

sudo nmcli connection modify gre1 ipv4.method manual ipv4.addresses 10.0.1.1/30 ipv6.method disabled

Activate it:

sudo nmcli connection up gre1

NetworkManager loads the ip_gre kernel module for you; there is no need to run modprobe by hand.

Step 3: Create the tunnel on server B

Run the same three commands on server B with the addresses mirrored:

sudo nmcli connection add type ip-tunnel ip-tunnel.mode gre con-name gre1 ifname gre1 remote 203.0.113.10 local 198.51.100.5
sudo nmcli connection modify gre1 ipv4.method manual ipv4.addresses 10.0.1.2/30 ipv6.method disabled
sudo nmcli connection up gre1

Step 4: Test the tunnel

Check the interface on either server:

ip -d link show gre1
ip addr show gre1

The first command shows the tunnel type with its remote and local endpoints, the second shows the 10.0.1.x address. Then ping across the tunnel. From server A:

ping -c 4 10.0.1.2

From server B:

ping -c 4 10.0.1.1

Replies in both directions mean the tunnel works. Because the connection profile is saved, it comes up automatically at boot.

Step 5: Decide what may use the tunnel

The gre1 interface joins firewalld's default zone (public), so services you have not opened there are still blocked for the peer, even through the tunnel. Open individual services in that zone as usual, or, if you fully trust the other server, put the tunnel interface into the trusted zone, which accepts everything arriving on it:

sudo nmcli connection modify gre1 connection.zone trusted
sudo nmcli connection up gre1

Optional: route another network through the tunnel

A tunnel is most useful when it carries traffic for networks behind the endpoints. Suppose server B also has a private network 172.16.0.0/24 that server A should reach. On server A, add a static route that points to B's tunnel address:

sudo nmcli connection modify gre1 +ipv4.routes "172.16.0.0/24 10.0.1.2"
sudo nmcli connection up gre1

On server B, which now forwards packets between interfaces, enable IPv4 forwarding permanently:

echo "net.ipv4.ip_forward=1" | sudo tee /etc/sysctl.d/95-ipv4-forwarding.conf
sudo sysctl -p /etc/sysctl.d/95-ipv4-forwarding.conf

Hosts in 172.16.0.0/24 need a route back to 10.0.1.0/30 via server B, and firewalld on server B must permit forwarding between the tunnel and the private interface (interfaces in the same zone can forward to each other when the zone has intra-zone forwarding enabled; otherwise add a policy between the zones).

Troubleshooting

  • Ping times out. Watch for GRE packets on the public interface of the receiving server while you ping from the other side (install the tool with sudo dnf install tcpdump; replace eth0 with your interface name from ip -br link):
    sudo tcpdump -ni eth0 proto gre
    If nothing arrives, GRE is being filtered between the servers (provider firewall or a NAT device). If packets arrive but nothing is answered, recheck the firewalld rule and that local and remote are mirrored correctly.
  • Large transfers stall while ping works. GRE over IPv4 adds 24 bytes of headers, so the tunnel MTU is 1476 on a standard 1500-byte path. If the path between the servers has a smaller MTU, lower the tunnel's with sudo nmcli connection modify gre1 ip-tunnel.mtu 1400 on both sides and bring the connection up again.
  • The server's public address is not on the server itself. On platforms that map a public address to a private one (1:1 NAT), set local to the address actually configured on the interface, and remote on the other side to the public address.

Removing the tunnel

Delete the connection on both servers and remove the firewall rule you added (shown for server A):

sudo nmcli connection delete gre1
sudo firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="198.51.100.5" protocol value="gre" accept'
sudo firewall-cmd --reload

Frequently asked questions

Is a GRE tunnel secure?

No. GRE only encapsulates; anyone on the path can read and forge the packets. The optional GRE key (ip-tunnel.input-key and ip-tunnel.output-key in NetworkManager) merely tags packets so several tunnels between the same endpoints can be told apart. For confidentiality, run IPsec underneath the tunnel or use WireGuard instead.

What is the difference between gre and gretap mode?

gre carries IP packets (layer 3) and is what you want for routing between two servers. gretap carries whole Ethernet frames (layer 2), so the tunnel can be added to a bridge to stretch one LAN segment across two sites. NetworkManager supports both as values of ip-tunnel.mode.

Can one server have several GRE tunnels?

Yes. Create one connection per peer with a different name (gre2, gre3), its own remote address and its own small tunnel subnet. Each peer needs its own firewall rule for protocol GRE.

Does this work when one side is behind NAT?

Usually not. GRE has no port numbers, so a home router or carrier NAT has nothing to map, and many drop it. Both endpoints should have public addresses; when one cannot, a UDP-based tunnel such as WireGuard is the practical choice.

Share story:

Continue reading other guides on How VPS.