Gre туннель astra linux

How To Setup GRE Tunnel On Linux Servers

Generic Routing Encapsulation (GRE) is a tunneling protocol developed by Cisco Systems which can encapsulate a wide variety of network layer protocols inside virtual point-to-point links or point-to-multipoint links over an Internet Protocol network.

A GRE tunnel is useful in certain situations, like protecting a server without DDoS protection using another one with the protection, or to enable applications that only allow IPv4 to also accept IPv6.

It is relatively easy to set up and secure GRE tunnel («pipe») between server A and server B, which will allow packets to be forwarded with minimal resource usage.

How does GRE tunnel work?

When you create a GRE tunnel on the server, your server will act as a virtual router. Keep in mind that both ends will need a public IP address as packets are sent over multiple networks.

GRE tunnel setup prerequisites

First of all, you must have two servers with root access. If you don’t have yet, you can obtain and use either Virtual Private Server (VPS) or a Dedicated Server to create GRE Tunnel. In this tutorial they will be called Server A and Server B, and will have the following characteristics:

  • Server A — the server that all the clients will connect to
    • IP: 198.51.100.1
    • GRE tunnel internal IP: 10.0.0.1
    • IP: 203.0.113.1
    • GRE tunnel internal IP: 10.0.0.2

    Step 1 — Module loading

    For setting up a GRE tunnel on Linux you must have ip_gre module loaded in your kernel. To make sure it’s loaded just run the following SSH commands:

    sudo modprobe ip_gre lsmod | grep gre

    If you see something else it’s possible that your kernel does not support GRE.

    To forward all the traffic in and out of the GRE tunnel we’re going to use iptables and iproute2 that should be already installed in all the major linux distributions. In case they’re not installed use the following command

    sudo apt install iptables iproute2

    for Red Hat based distros:

    sudo yum install iptables iproute2

    Step 2 — GRE Tunnel setup

    First we have to set up our tunnel.

    On Server A execute this code to enable ip forwarding:

    sudo echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf sudo sysctl -p

    Now create a new networking interface which will be the one using the GRE tunnel:

    sudo ip tunnel add gre1 mode gre local 198.51.100.1 remote 203.0.113.1 ttl 255 sudo ip addr add 10.0.0.1/30 dev gre1 sudo ip link set gre1 up

    Then, do the same on Server B changing the IPs:

    sudo ip tunnel add gre1 mode gre local 203.0.113.1 remote 198.51.100.1 ttl 255 sudo ip addr add 10.0.0.2/30 dev gre1 sudo ip link set gre1 up

    Step 2.1 — Ping test

    On Server A run this SSH command:

    And on Server B run this command:

    If the ping works then the GRE tunnel is correctly set up.

    Step 3 — Implementing the new routes

    A route is required to make sure data that comes in via the GRE tunnel is handled correctly.

    sudo echo '100 GRE' >> /etc/iproute2/rt_tables sudo ip rule add from 10.0.0.0/30 table GRE sudo ip route add default via 10.0.0.1 table GRE

    Step 4 — NAT configuration

    NAT is used to pass data over our GRE and out the other end.

    iptables -t nat -A POSTROUTING -s 10.0.0.0/30 ! -o gre+ -j SNAT --to-source 198.51.100.1

    To test the outbound connection execute on Server B this commands:

    for Red Hat based distros:

    And then run the following command:

    curl https://www.euro-space.net/show-ip

    At the end you should see Server A’s IP address.

    Step 5 — Port forwarding

    On Server A run this commands to allow all the data going to and coming from Server B:

    sudo iptables -A FORWARD -d 10.0.0.2 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT sudo iptables -A FORWARD -s 10.0.0.2 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

    Then, we want to forward all our data from Server A to Server B.

    sudo iptables -t nat -A PREROUTING -d 198.51.100.1 -p PROTO -m PROTO --dport PORT -j DNAT --to-destination 10.0.0.2

    replacing PROTO and PORT with your actual ones.

    For example, to forward all the data to a Webserver (Port TCP 80) we have to run:

    sudo iptables -t nat -A PREROUTING -d 198.51.100.1 -p TCP -m TCP --dport 80 -j DNAT --to-destination 10.0.0.2

    We must do it for each port we are using.

    Step 6 — Persistence

    On a server restart all the things we did will be wiped out. To make sure the GRE tunnel and everything else is going to work after a restart we have to edit the file /etc/rc.local and add all the commands we did (except for the echo ones!) before the exit 0.

    Conclusion

    Now, if we connect to Server A using the ports we configured (Port TCP 80 for example) we’re going instead to connect to Server B without knowing it.

    Note: if you use CSF to manage iptables you may have to put all the iptables commands in your /etc/csf/csfpost.sh and insert both servers’ IP (also the GRE one) in your /etc/csf/csf.allow.

    Источник

    Настройка Linux для туннеля GRE

    Если вы видите что-то еще, возможно, ваше ядро не поддерживает GRE.
    Для пересылки всего трафика в туннель GRE и из него мы будем использовать iptables и iproute2, которые уже должны быть установлены во всех основных дистрибутивах Linux. Если они не установлены, используйте следующую команду
    для дистрибутивов на основе Debian:

    sudo apt install iptables iproute2
    sudo yum install iptables iproute2

    Шаг 2 — Настройка туннеля

    Сначала мы должны настроить наш туннель.
    На сервере A выполните этот код, чтобы включить пересылку ip:

    sudo echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf sudo sysctl -p
    sudo ip tunnel add gre1 mode gre local 198.51.100.1 remote 203.0.113.1 ttl 255 sudo ip addr add 10.0.0.1/30 dev gre1 sudo ip link set gre1 up
    sudo ip tunnel add gre1 mode gre local 203.0.113.1 remote 198.51.100.1 ttl 255 sudo ip addr add 10.0.0.2/30 dev gre1 sudo ip link set gre1 up

    Шаг 2.1 — Тест Ping

    Шаг 3 — Добавить новые маршруты

    Маршрут необходим для правильной обработки данных, поступающих через туннель GRE.
    На сервере B выполните:

    sudo echo '100 GRE' >> /etc/iproute2/rt_tables sudo ip rule add from 10.0.0.0/30 table GRE sudo ip route add default via 10.0.0.1 table GRE

    Шаг 4 — Настройте NAT

    iptables -t nat -A POSTROUTING -s 10.0.0.0/30 ! -o gre+ -j SNAT --to-source 198.51.100.1

    Чтобы проверить исходящее соединение, выполните на сервере B следующие команды:
    для дистрибутивов на основе Debian:

    curl http://www.cpanel.net/showip.cgi --interface 10.0.0.2

    Шаг 5 — Порты пересылки

    На сервере A выполните следующие команды, чтобы разрешить все данные, поступающие на сервер B и поступающие с него:

    sudo iptables -A FORWARD -d 10.0.0.2 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT sudo iptables -A FORWARD -s 10.0.0.2 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
    sudo iptables -t nat -A PREROUTING -d 198.51.100.1 -p PROTO -m PROTO --dport PORT -j DNAT --to-destination 10.0.0.2

    замена PROTO и PORT вашими фактическими.
    Например, чтобы переслать все данные на веб-сервер (порт TCP 80), нам нужно выполнить:

    sudo iptables -t nat -A PREROUTING -d 198.51.100.1 -p TCP -m TCP --dport 80 -j DNAT --to-destination 10.0.0.2

    Шаг 6 — Сделай постоянным

    На сервере перезагрузите все, что мы сделали, будут уничтожены. Чтобы убедиться, что туннель GRE и все остальное будет работать после перезапуска, мы должны отредактировать файл /etc/rc.local и добавить все команды, которые мы сделали (кроме эхо!) Перед exit 0 .

    Заключение

    Теперь, если мы подключаемся к серверу A, используя настроенные порты (например, порт TCP 80), мы вместо этого собираемся подключиться к серверу B, не зная об этом.
    Примечание: если вы используете CSF для управления iptables, вам, возможно, придется поместить все команды iptables в свой /etc/csf/csfpost.sh и вставить IP-адрес обоих серверов (также GRE) в свой /etc/csf/csf.allow

    Источник

    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.

    On host A, run the following command.

    $ sudo ip tunnel add gre0 mode gre remote 172.168.10.25 local 192.168.233.204 ttl 255 $ sudo ip link set gre0 up $ sudo ip addr add 10.10.10.1/24 dev gre0

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

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

    default via 135.112.29.1 dev eth0 proto static 10.10.10.0/24 dev gre0 proto kernel scope link src 10.10.10.1

    On host B, run similar commands as follows.

    $ sudo ip tunnel add gre0 mode gre remote 192.168.233.204 local 172.168.10.25 ttl 255 $ sudo ip link set gre0 up $ sudo ip addr add 10.10.10.2/24 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 10.10.10.2 (10.10.10.2) 56(84) bytes of data. 64 bytes from 10.10.10.2: icmp_req=1 ttl=64 time=0.619 ms 64 bytes from 10.10.10.2: icmp_req=2 ttl=64 time=0.496 ms 64 bytes from 10.10.10.2: icmp_req=3 ttl=64 time=0.587 ms

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

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

    Support Xmodulo

    This website is made possible by minimal ads and your gracious donation via PayPal or credit card

    Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

    Источник

    Простой Ethernet-туннель на Linux в четыре-шесть команд

    Когда требуется объединить несколько компьютеров в псевдолокальную сеть через интернет часто это решается настройкой OpenVPN.

    Решение хорошо работает, но не лишено недостатков:
    1. Нужно ставить дополнительный софт и его настраивать. Причем с первого раза настраивается он не очень просто — надо посидеть и поразбираться.
    2. Шифрование трафика происходит в пользовательском режиме и вносит дополнительные задержки, это не всегда важно но для IP-телефонии может быть заметно.
    3. Шифрование не всегда нужно. Например в моем случае все соединения и так защищенные (ssh), мне нужна просто удобная плоская адресация между несколькими компьютерами так как будто они объединены в локальную сеть.

    В Linux GRE-туннели настраиваются до неприличия просто (если не нужно шифрование), из требований Linux и по публичному IP на каждый.
    В интернете на эту тему информации как-то исчезающе мало — в основном объясняются IP (а не ethernet) туннели и сразу вкупе с шифрованием трафика (которое нужно не всегда). man ip тоже очень обширный и информацию по

    Пусть у нас есть два хоста:
    HOST1 с внешним адресом 1.2.3.4
    HOST2 с внешним адресом 2.3.4.5

    Хочется сделать между ними ethernet-сеть, ну и для примера поверх нее можно настроить IP-адреса 192.168.0.1 192.168.0.2, но можно и любые другие или IPv6 или что угодно еще — получится обычная сеть как через коммутатор.

    Все команды выполняются от ROOT, после перезагрузки — теряются. Чтобы не терялись нужно прописать команды в скрипты автозагрузки или в конфиги (у каждого дистрибутива свои).

    1. Добавить виртуальную сетевую карту-шлюз на HOST1:

    HOST1: ip link add grelan type gretap local 1.2.3.4 remote 2.3.4.5 

    На HOST1 он будет выглядеть как обычная сетевая карта — можно назначать IP-адреса, запускать DHCP-сервер, включать в Bridge и т.п.

    2. Включить добавленную карту

    2. Если запущен IP-tables — разрешаем GRE-трафик

    HOST1: iptables -I INPUT -p gre -s 2.3.4.5 -j ACCEPT 

    3. Симметричная настройка на втором хосте:

    HOST2: ip link add grelan type gretap local 2.3.4.5 remote 1.2.3.4 HOST2: ip link set grelan up HOST2: iptables -I INPUT -p gre -s 1.2.3.4 -j ACCEPT 

    В этот момент ethernet-сеть уже работает. Для проверки этого можно настроить приватные IP-адреса на каждой стороне туннеля и пустить пинги.

    HOST1: ip addr add 192.168.0.1/24 HOST2: ip addr add 192.168.0.2/24 
    root@ubuntu:~# ping 192.168.0.1 PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data. 64 bytes from 192.168.0.1: icmp_req=1 ttl=64 time=2.45 ms 64 bytes from 192.168.0.1: icmp_req=2 ttl=64 time=1.19 ms 64 bytes from 192.168.0.1: icmp_req=3 ttl=64 time=2.45 ms 

    Для пингов возможно тоже придется добавить правила в iptables (или вообще выключить его на время экспериментов).

    Туннель спокойно настраивается между разными версиями linux, пока писал этот пост один конец был на ubuntu, второй на centos, разницы в настройке нет абсолютно никакой.

    Повторяю — это туннель не дает никакой защиты от прослушивания/внедрения трафика.

    Источник

    Читайте также:  Zip archive with password linux
Оцените статью
Adblock
detector