{"id":2984,"date":"2024-03-14T22:39:38","date_gmt":"2024-03-14T17:09:38","guid":{"rendered":"https:\/\/www.veeble.org\/kb\/?p=2984"},"modified":"2025-04-10T11:10:24","modified_gmt":"2025-04-10T05:40:24","slug":"linux-gre-tunnel-setup","status":"publish","type":"post","link":"https:\/\/www.veeble.com\/kb\/linux-gre-tunnel-setup\/","title":{"rendered":"How to Setup Linux GRE Tunnel"},"content":{"rendered":"\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/Setting-up-a-Linux-GRE-Tunnel-1.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"566\" data-id=\"3003\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/Setting-up-a-Linux-GRE-Tunnel-1.png\" alt=\"\" class=\"wp-image-3003\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Setting-up-a-Linux-GRE-Tunnel-1.png 1200w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Setting-up-a-Linux-GRE-Tunnel-1-300x142.png 300w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Setting-up-a-Linux-GRE-Tunnel-1-1024x483.png 1024w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Setting-up-a-Linux-GRE-Tunnel-1-768x362.png 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<p>Imagine having a tunnel to convey letters to a friend across the city anonymously. On the internet, a Linux GRE tunnel works similarly. It connects two internet points like an invisible tube. In this tube, your data is shielded from the outer world, allowing computers to communicate securely and secretly regardless of distance. It&#8217;s like delivering your messages in a secure, invisible envelope that only you and your friend can open.<br>Technically speaking, a GRE tunnel is similar to a VPN because it wraps diverse network protocols inside IP tunnels. This wrapping helps connect networks over the internet safely and anonymously.<\/p>\n\n\n\n<p>In this tutorial, we set up a GRE tunnel between two Linux servers in a step-by-step approach to understand and set it easily.<\/p>\n\n\n\n<pre class=\"wp-block-verse\">A GRE tunnel works much like a VPN, but it doesn't encrypt packets. They're just encapsulated in a GRE header. So, if you're looking to keep your data encrypted, you'll need to set up IPSec as an additional step.<\/pre>\n\n\n<h4 class=\"wp-block-heading\" id=\"setting-up-gre-tunnel\">Setting Up GRE Tunnel<\/h4>\n\n<h5 class=\"wp-block-heading\" id=\"update-your-system\">Update Your System:<\/h5>\n\n\n<p>Ensure both Linux servers are updated to avoid any compatibility issues. Execute the following command to update it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update &amp;&amp; sudo apt upgrade -y<\/code><\/pre>\n\n\n<h5 class=\"wp-block-heading\" id=\"load-the-gre-module\">Load the GRE Module<\/h5>\n\n\n<p>The <code>ip_gre<\/code> module lets your Linux system use GRE tunnels, which help different networks connect over the internet. To use it on your servers, simply load this module into your system&#8217;s kernel with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo modprobe ip_gre &amp;&amp; lsmod | grep gre<\/code><\/pre>\n\n\n\n<p class=\"has-small-font-size\">The output will look like this:<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-14-173321.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"660\" height=\"81\" data-id=\"3061\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-14-173321.png\" alt=\"How to load gre module in linux\" class=\"wp-image-3061\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-14-173321.png 660w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-14-173321-300x37.png 300w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/a><\/figure>\n<\/figure>\n\n\n<h5 class=\"wp-block-heading\" id=\"creating-the-gre-tunnel\">Creating the GRE Tunnel<\/h5>\n\n<h6 class=\"wp-block-heading\" id=\"server-a\"><span style=\"text-decoration: underline\">Server A<\/span><\/h6>\n\n\n<p><strong>Create the GRE tunnel:<\/strong><\/p>\n\n\n\n<p>To establish a GRE tunnel between ServerA and ServerB. Execute the following commands to configure the tunnel between them: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ip tunnel add gre1 mode gre remote ServerB_IP local ServerA_IP ttl 255<\/code><\/pre>\n\n\n\n<p class=\"has-small-font-size\">Replace <code>ServerB_IP<\/code> with the private IP address of Server B and <code>ServerA_IP<\/code> with the private IP of Server A<\/p>\n\n\n\n<p>For example; if the private IP of Server A is 172.31.47.239 and Server B is 172.31.36.233, then the command will look like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/image-1.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1075\" height=\"125\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/image-1.png\" alt=\"GRE Tunnel\" class=\"wp-image-3062\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/image-1.png 1075w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/image-1-300x35.png 300w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/image-1-1024x119.png 1024w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/image-1-768x89.png 768w\" sizes=\"auto, (max-width: 1075px) 100vw, 1075px\" \/><\/a><\/figure>\n\n\n\n<p><strong>Assign an IP address to the GRE tunnel interface:<\/strong><\/p>\n\n\n\n<p>Assign a tunnel IP address for both Server A and Server B. Ensure the IP addresses are part of a separate subnet that&#8217;s different from the main server IPs. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ip addr add TunnelIP_A peer TunnelIP_B dev gre1<\/code><\/pre>\n\n\n\n<p class=\"has-small-font-size\">Here replace <code>TunnelIP_A<\/code> with the desired private IP address for Server A within the tunnel network. <\/p>\n\n\n\n<p>For example, if we are assigning 10.0.0.1 for the tunnel interface of Server A and 10.0.0.2 for Server B, the command will look like:<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-3 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-14-182206.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"766\" height=\"105\" data-id=\"3066\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-14-182206.png\" alt=\"Assign an IP address to the GRE tunnel interface\" class=\"wp-image-3066\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-14-182206.png 766w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-14-182206-300x41.png 300w\" sizes=\"auto, (max-width: 766px) 100vw, 766px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<pre class=\"wp-block-verse\"><span style=\"text-decoration: underline\">Note on GRE Tunnel IP Addresses<\/span>\n\nWhen setting up a GRE tunnel between servers, the IP addresses assigned to the tunnel interfaces (e.g., 10.0.0.1 on one server and 10.0.0.2 on the opposite server) are considered virtual IP addresses. These addresses are used exclusively for communication over the GRE tunnel and do not need to be configured as part of the server's physical network interfaces or added through the cloud provider's management console.<\/pre>\n\n\n<h6 class=\"wp-block-heading\" id=\"bring-the-gre-tunnel-interface-up\">Bring the GRE tunnel interface up:<\/h6>\n\n\n<p>Bring the GRE tunnel interface active and running by executing the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ip link set gre1 up<\/code><\/pre>\n\n\n<h6 class=\"wp-block-heading\" id=\"server-b\"><span style=\"text-decoration: underline\">Server B<\/span><\/h6>\n\n\n<p>On Server B, do the same but in reverse, replacing <code>ServerA_IP<\/code> with the IP address of Server A, and <code>TunnelIP_B<\/code> with the desired IP address for Server B within the tunnel network.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ip tunnel add gre1 mode gre remote ServerA_IP local ServerB_IP ttl 255\n\nsudo ip addr add TunnelIP_B peer TunnelIP_A dev gre1\n\nsudo ip link set gre1 up<\/code><\/pre>\n\n\n\n<p class=\"has-small-font-size\">Example command prompt:<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-4 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-14-182912.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1071\" height=\"110\" data-id=\"3068\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-14-182912.png\" alt=\"Assign an IP address to the GRE tunnel interface\" class=\"wp-image-3068\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-14-182912.png 1071w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-14-182912-300x31.png 300w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-14-182912-1024x105.png 1024w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-14-182912-768x79.png 768w\" sizes=\"auto, (max-width: 1071px) 100vw, 1071px\" \/><\/a><\/figure>\n<\/figure>\n\n\n<h6 class=\"wp-block-heading\" id=\"testing-the-tunnel\">Testing the Tunnel<\/h6>\n\n\n<p>Ping the tunnel IP address of Server B from Server A to ensure the tunnel is operational and vice-versa.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ping TunnelIP_B<\/code><\/pre>\n\n\n\n<p>If you receive a response like the below screenshot, your GRE tunnel is correctly set up and operational.<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-5 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-14-193224.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"629\" height=\"206\" data-id=\"3071\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-14-193224.png\" alt=\"ping command in linux\" class=\"wp-image-3071\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-14-193224.png 629w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-14-193224-300x98.png 300w\" sizes=\"auto, (max-width: 629px) 100vw, 629px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<pre class=\"wp-block-verse\"><span style=\"text-decoration: underline\">If ping doesn't work!<\/span>\n\n1) Make sure to check IP forwarding on both servers, if it was disabled you can enable it by executing the command: <code><a href=\"https:\/\/www.veeble.org\/kb\/tune-linux-kernel-without-reboot\/\" target=\"_blank\" rel=\"noopener\">sysctl -w net.ipv4.ip_forward=1<\/a><\/code>\n2) Ensure ICMP traffic and port 47 traffic for GRE is allowed on server firewall.<\/pre>\n\n\n\n<p class=\"has-text-align-center\">\ud83d\udda5\ufe0f\u2ad8\u2ad8\u2ad8\ud83d\udda5\ufe0f<\/p>\n\n\n\n<p>GRE tunnels are great for straightforward IP tunneling where encryption isn&#8217;t a priority, offering a simpler, faster solution. However, when privacy is essential add IPSec to this setup to make the moving data encrypted through the tunnel. Choose based on your need for speed or security.<\/p>\n\n\n\n<p>In conclusion, with a few simple commands from this tutorial, you&#8217;ve built invisible bridges between two servers with the Generic Routing Encapsulation Tunnel. Off you go, happy tunneling! <\/p>\n\n\n\n<div style=\"height:45px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-uagb-call-to-action uagb-block-9f37ab95 wp-block-button\"><div class=\"uagb-cta__wrap\"><h3 class=\"uagb-cta__title\">Premium KVM VPS, Without the Premium Price<\/h3><p class=\"uagb-cta__desc\">Get the performance you need at a price you&#8217;ll love. Our KVM VPS plans are designed for value.<\/p><\/div><div class=\"uagb-cta__buttons\"><a href=\"https:\/\/www.veeble.com\/in\/kvm-vps\/\" class=\"uagb-cta__button-link-wrapper wp-block-button__link\" target=\"_blank\" rel=\"noopener noreferrer\">View Pricing<svg xmlns=\"https:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\"><path d=\"M504.3 273.6l-112.1 104c-6.992 6.484-17.18 8.218-25.94 4.406c-8.758-3.812-14.42-12.45-14.42-21.1L351.9 288H32C14.33 288 .0002 273.7 .0002 255.1S14.33 224 32 224h319.9l0-72c0-9.547 5.66-18.19 14.42-22c8.754-3.809 18.95-2.075 25.94 4.41l112.1 104C514.6 247.9 514.6 264.1 504.3 273.6z\"><\/path><\/svg><\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to set up a Linux GRE Tunnel with our comprehensive guide. Perfect for secure and efficient networking.<\/p>\n","protected":false},"author":7,"featured_media":8550,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[5,9],"tags":[],"class_list":["post-2984","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-firewallsecurity"],"uagb_featured_image_src":{"full":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/How-to-Setup-Linux-GRE-Tunnel.jpg",1536,1024,false],"thumbnail":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/How-to-Setup-Linux-GRE-Tunnel-150x150.jpg",150,150,true],"medium":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/How-to-Setup-Linux-GRE-Tunnel-300x200.jpg",300,200,true],"medium_large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/How-to-Setup-Linux-GRE-Tunnel-768x512.jpg",768,512,true],"large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/How-to-Setup-Linux-GRE-Tunnel-1024x683.jpg",1024,683,true],"1536x1536":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/How-to-Setup-Linux-GRE-Tunnel.jpg",1536,1024,false],"2048x2048":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/How-to-Setup-Linux-GRE-Tunnel.jpg",1536,1024,false]},"uagb_author_info":{"display_name":"Adrian Antony","author_link":"https:\/\/www.veeble.com\/kb\/author\/adrian\/"},"uagb_comment_info":0,"uagb_excerpt":"Learn how to set up a Linux GRE Tunnel with our comprehensive guide. Perfect for secure and efficient networking.","_links":{"self":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/2984","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/comments?post=2984"}],"version-history":[{"count":30,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/2984\/revisions"}],"predecessor-version":[{"id":7567,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/2984\/revisions\/7567"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media\/8550"}],"wp:attachment":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media?parent=2984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/categories?post=2984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/tags?post=2984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}