One place for hosting & domains

      Установка Tinc и настройка базового VPN в Ubuntu 18.04


      Введение

      Tinc — демон виртуальной частной сети (VPN) с открытым исходным кодом, имеющий много полезных функций, включая шифрование, поддержку сжатия и автоматическую ячеистую маршрутизацию, позволяющую перенаправлять трафик VPN между серверами напрямую. Эти возможности отличают tinc от других решений VPN и делают его отличным выбором для создания сети VPN из множества больших географически распределенных сетей.

      В этом обучающем руководстве мы рассмотрим использование tinc для создания защищенных сетей VPN, где ваши серверы смогут взаимодействовать друг с другом, как если бы они находились в одной локальной сети. Также мы продемонстрируем использование tinc для создания защищенного туннеля в частную сеть. Мы будем использовать серверы Ubuntu 18.04, однако описанные конфигурации можно адаптировать для использования с любой другой ОС.

      Цели

      В этом обучающем руководстве охватываются различные варианты использования, в частности подключение одного клиентского узла к сети VPN через частную сеть, а другого — через публичную сеть. Вы можете адаптировать схему подключения в соответствии со своими потребностями. Для этого нужно просто спланировать доступ серверов друг к другу и адаптировать приведенные в этом обучающем руководстве примеры к вашим потребностям. Если вы собираетесь адаптировать примеры к своим серверам, обязательно замените выделенные значения в примерах собственными значениями. Однако возможно лучше будет предварительно пройти это обучающее руководство в изначальном виде, чтобы лучше понять компоненты и процессы, прежде чем что-то изменять.

      Для удобства мы будем ссылаться на серверы в этом обучающем руководстве следующим образом:

      • server-01: все узлы VPN будут подключаться к этому серверу, и соединение с ним должно поддерживаться для нормальной работы сети VPN. При необходимости так можно настроить дополнительные серверы, чтобы обеспечить избыточность.
      • client-01: подключается к узлу VPN server-01 через его интерфейс частной сети.
      • client-02: подключается к узлу VPN server-01 через интерфейс публичной сети.

      Примечание. Tinc не делает отличий между серверами (компьютеры, где размещаются и предоставляются службы VPN) и клиентами (компьютеры, которые подключаются к защищенной частной сети и используют ее), однако такое представление серверов поможет лучше понять и визуализировать работу tinc.

      Так выглядит схема сети VPN, которую мы хотим настроить:

      Настройка VPN в Tinc

      Синий прямоугольник представляет нашу сеть VPN, а розовый — лежащую в ее основе частную сеть. Все три сервера могут взаимодействовать через сеть VPN, хотя частная сеть недоступна компьютеру client-02.

      Предварительные требования

      Если вы хотите выполнить это обучающее руководство в точном соответствии с указаниями, разместите два сервера Ubuntu 18.04 (server-01 и client-01) в одном вычислительном центре и активируйте на каждом из них частные сети. Затем создайте еще один сервер Ubuntu 18.04 (client-02) в отдельном вычислительном центре. На каждом сервере должен быть пользователь с правами администратора и брандмауэр, настроенный с помощью ufw. Для подготовки следуйте указаниям руководства по начальной настройке сервера Ubuntu 18.04.

      На последующих шагах данного обучающего руководства нам нужно будет переместить несколько файлов между компьютерами с помощью scp. Для этого вам нужно будет сгенерировать ключи SSH на каждом из ваших серверов, добавить ключи SSH компьютеров client-01 и client-02 на сервер server-01 в файл authorized_keys, а затем добавить ключ SSH server-01 на компьютеры client-01 и client-02 в файлы authorized_keys. Дополнительную информацию по настройке можно найти в обучающем руководстве Настройка ключей SSH в Ubuntu 18.04.

      Шаг 1 — Установка Tinc

      Tinc устанавливается из репозиториев по умолчанию Ubuntu APT, то есть для установки требуется всего несколько команд.

      Если вы еще не делали этого, запустите следующую команду на каждом сервере для обновления соответствующих индексов пакетов:

      All servers

      Затем установите tinc на каждом сервере с помощью следующей команды:

      All servers

      Вы установили tinc на каждом из ваших серверов. Однако для запуска и использования VPN нам потребуется внести некоторые изменения в конфигурацию tinc на каждом компьютере. Начнем с обновления server-01.

      Шаг 2 — Настройка сервера Tinc

      Tinc требует, чтобы каждый компьютер в сети VPN имел следующие три компонента конфигурации:

      • Файлы конфигурации Tinc: это три отдельных файла, определяющие конфигурацию демона tinc:
        • tinc.conf, определяющий имя сети, сетевое устройство, на котором будет работать VPN, а также другие параметры VPN;
        • tinc-up, скрипт для активации сетевого устройства, определенного в файле tinc.conf, после запуска tinc;
        • tinc-down, скрипт для отключения сетевого устройства при остановке tinc.
      • Пары открытых/закрытых ключей: Tinc использует пары открытых/закрытых ключей, чтобы доступ к VPN был только у пользователей с действующими ключами.
      • Файлы конфигурации хоста: каждый компьютер (или хост) в сети VPN имеет собственный файл конфигурации, где хранятся фактический IP-адрес хоста и подсеть, где tinc будет его обслуживать.

      Tinc использует имя сети для различения сетей tinc VPN. Это полезно в случаях, когда нужно настроить несколько сетей VPN, однако имя сети рекомендуется использовать и в случае настройки только одной сети VPN. Сети VPN можно присвоить любое имя, но для удобства мы присвоим нашей сети VPN имя netname.

      Создайте на сервере server-01 структуру директорий для сети VPN:

      server-01

      • sudo mkdir -p /etc/tinc/netname/hosts

      Используйте предпочитаемый текстовый редактор для создания файла tinc.conf. Мы будем использовать nano:

      server-01

      • sudo nano /etc/tinc/netname/tinc.conf

      Добавьте в пустой файл следующие строки. Эти строки настраивают узел tinc с именем server_01 с сетевым интерфейсом tun0, который будет использовать IPv4:

      server-01:/etc/tinc/netname/tinc.conf

      Name = server_01
      AddressFamily = ipv4
      Interface = tun0
      

      Предупреждение. Следует отметить, что значение после директивы Name содержит символ подчеркивания (_), а не дефис (-). Это важно, поскольку tinc требует, чтобы значение Name содержало только буквенно-числовые символы и символы подчеркивания. Если вы используете дефис, вы получите сообщение об ошибке при попытке запуска сети VPN на последующих шагах.

      После добавления этих строк сохраните и закройте файл. Если вы используете nano, нажмите CTRL+X, Y, затем ENTER.

      Затем создайте файл конфигурации хоста с именем server_01 в субдиректории hosts. Клиентские узлы будут использовать этот файл для взаимодействия с server-01:

      server-01

      • sudo nano /etc/tinc/netname/hosts/server_01

      Следует еще раз отметить, что имя этого файла содержит символ подчеркивания, а не дефис. Так он соответствует директиве Name в файле tinc.conf, что позволяет tinc автоматически добавлять в этот файл открытый ключ RSA для сервера, который мы сгенерируем на следующих шагах.

      Добавьте в файл следующие строки и обязательно укажите публичный IP-адрес компьютера server-01:

      server-01:/etc/tinc/netname/hosts/server_01

      Address = server-01_public_IP_address
      Subnet = 10.0.0.1/32
      

      В поле Address указывается, как другие узлы будут подключаться к этому серверу, а в поле Subnet указывается подсеть, которую будет обслуживать этот демон. Сохраните и закройте файл.

      Далее следует сгенерировать пару открытых и закрытых ключей RSA для этого хоста с помощью следующей команды:

      server-01

      • sudo tincd -n netname -K4096

      После запуска этой команды вам будет предложено ввести имена файлов, где tinc будет сохранять открытые и закрытые ключи RSA:

      Output

      . . . Please enter a file to save private RSA key to [/etc/tinc/netname/rsa_key.priv]: Please enter a file to save public RSA key to [/etc/tinc/netname/hosts/server_01]:

      Нажмите ENTER для использования значений по умолчанию в каждом диалоге. Так tinc будет сохранять закрытый ключ в файле с именем rsa_key.priv и добавлять открытый ключ в файл конфигурации хоста server_01.

      Затем создайте скрипт tinc-up, который будет запускаться при каждом запуске сети VPN с именем netname:

      server-01

      • sudo nano /etc/tinc/netname/tinc-up

      Добавьте следующие строки:

      server-01:/etc/tinc/netname/tinc-up

      #!/bin/sh
      ip link set $INTERFACE up
      ip addr add 10.0.0.1/32 dev $INTERFACE
      ip route add 10.0.0.0/24 dev $INTERFACE
      

      Вот что делает каждая из этих строк:

      • ip link …: задает для интерфейса виртуальной сети tinc статус up
      • ip addr …: добавляет IP-адрес 10.0.0.1 с маской сети 32 в интерфейс виртуальной сети tinc, в результате чего другие компьютеры в сети VPN будут видеть у сервера server-01 IP-адрес 10.0.0.1
      • ip route …: добавляет маршрут (10.0.0.0/24), доступный через интерфейс виртуальной сети tinc

      После добавления этих строк сохраните и закройте файл.

      Далее создайте скрипт для удаления интерфейса виртуальной сети при остановке вашей сети VPN:

      server-01

      • sudo nano /etc/tinc/netname/tinc-down

      Добавьте следующие строки:

      server-01:/etc/tinc/netname/tinc-down

      #!/bin/sh
      ip route del 10.0.0.0/24 dev $INTERFACE
      ip addr del 10.0.0.1/32 dev $INTERFACE
      ip link set $INTERFACE down
      

      Эти строки имеют противоположное действие строкам в скрипте tinc-up:

      • ip route …: удаляет маршрут 10.0.0.0/24
      • ip addr …: удаляет IP-адрес 10.0.0.1 из интерфейса виртуальной сети tinc
      • ip link …: задает для интерфейса виртуальной сети tinc статус down

      Сохраните и закройте этот файл и сделайте оба новых сетевых скрипта исполняемыми:

      server-01

      • sudo chmod 755 /etc/tinc/netname/tinc-*

      В качестве заключительного шага настройки server-01 следует добавить правило брандмауэра, разрешающее трафик через порт 655, используемый tinc по умолчанию:

      server-01

      Теперь server-01 полностью настроен, и мы можем перейти к настройке клиентских узлов.

      Шаг 3 — Настройка клиентских узлов

      Для обоих клиентских компьютеров потребуется не совсем такая конфигурация, как для сервера, хотя в целом процесс будет очень похожим.

      В этом обучающем руководстве мы ориентируемся на определенную модель и поэтому настроим client-01 и client-02 практически одинаково с очень небольшими отличиями. Поэтому многие из указанных на этом шаге команд необходимо запускать на обоих компьютерах. Следует отметить, что если для client-01 или client-02 потребуется определенная команда или особая конфигурация, эти указания будут показаны в синем или красном блоке команд соответственно.

      Проведите на компьютерах client-01 и client-02 репликацию структуры директорий, созданную на сервере server-01:

      client-01 & client-02

      • sudo mkdir -p /etc/tinc/netname/hosts

      Затем создайте файл tinc.conf:

      client-01 & client-02

      • sudo nano /etc/tinc/netname/tinc.conf

      Добавьте в файл следующие строки на обоих компьютерах:

      client-01 & client-02 /etc/tinc/netname/tinc.conf

      Name = node_name
      AddressFamily = ipv4
      Interface = tun0
      ConnectTo = server_01
      

      Обязательно замените node_name именем соответствующего клиентского узла. В этом имене должен использоваться символ подчеркивания (_), а не дефис.

      Этот файл содержит директиву ConnectTo с указанием на server_01, а на сервере server-01 файл tinc.conf не содержит такой директивы. Отсутствие выражения ConnectTo на server-01 означает, что server-01 будет только прослушивать входящие соединения. Это подходит для нашей схемы, поскольку сервер не будет подключаться к другим компьютерам.

      Сохраните и закройте файл.

      Затем следует создать файл конфигурации хоста на каждом клиентском узле. Поэтому следует убедиться, что в имени файла используется символ подчеркивания вместо дефиса:

      client-01 & client-02

      • sudo nano /etc/tinc/netname/hosts/node_name

      Для компьютера client-01 добавьте следующую строку:

      client-01:/etc/tinc/netname/hosts/client_01

      Subnet = 10.0.0.2/32
      

      Для компьютера client-02 добавьте следующую строку:

      client-02:/etc/tinc/netname/hosts/client_02

      Subnet = 10.0.0.3/32
      

      Обратите внимание, что у каждого клиента своя подсеть, обслуживаемая tinc. Сохраните и закройте файл.

      Затем сгенерируйте пары ключей на каждом клиентском компьютере:

      client-01 & client-02

      • sudo tincd -n netname -K4096

      Как и в случае с server-01, в диалогах выбора файлов для сохранения ключей RSA нажимайте ENTER для использования вариантов по умолчанию.

      После этого создайте на каждом клиенте скрипт запуска сетевого интерфейса:

      client-01 & client-02

      • sudo nano /etc/tinc/netname/tinc-up

      Для client-01 добавьте следующие строки:

      client-01:/etc/tinc/netname/tinc-up

      #!/bin/sh
      ip link set $INTERFACE up
      ip addr add 10.0.0.2/32 dev $INTERFACE
      ip route add 10.0.0.0/24 dev $INTERFACE
      

      Для client-02 добавьте следующие строки:

      client-02:/etc/tinc/netname/tinc-up

      #!/bin/sh
      ip link set $INTERFACE up
      ip addr add 10.0.0.3/32 dev $INTERFACE
      ip route add 10.0.0.0/24 dev $INTERFACE
      

      Сохраните и закройте каждый файл.

      Затем создайте скрипт остановки сетевого интерфейса для каждого клиента:

      client-01 & client-02

      • sudo nano /etc/tinc/netname/tinc-down

      Для client-01 добавьте в пустой файл следующее:

      client-01:/etc/tinc/netname/tinc-down

      #!/bin/sh
      ip route del 10.0.0.0/24 dev $INTERFACE
      ip addr del 10.0.0.2/32 dev $INTERFACE
      ip link set $INTERFACE down
      

      Для client-02 добавьте следующее:

      client-02:/etc/tinc/netname/tinc-down

      #!/bin/sh
      ip route del 10.0.0.0/24 dev $INTERFACE
      ip addr del 10.0.0.3/32 dev $INTERFACE
      ip link set $INTERFACE down
      

      Сохраните и закройте файлы.

      Сделайте сетевые скрипты исполняемыми, запустив следующую команду на каждом клиентском компьютере:

      client-01 & client-02

      • sudo chmod 755 /etc/tinc/netname/tinc-*

      В завершение откройте порт 655 на каждом клиенте:

      client-01 & client-02

      Теперь клиентские узлы практически настроены, хотя и не до конца. Им необходим открытый ключ, созданный на server-01 на предыдущем шаге, для аутентификации подключения к VPN.

      Шаг 4 — Распределение ключей

      Каждый узел, который хочет напрямую взаимодействовать с другим узлом, должен обменяться с ним открытыми ключами, содержащимися в файлах конфигурации хоста. В нашем случае server-01 требуется произвести обмен открытыми ключами с другими узлами.

      Обмен ключами между server-01 и client-01

      На client-01 скопируйте файл конфигурации хоста на server-01. Поскольку client-01 и server-01 находятся в одном вычислительном центре, и на них включены частные сети, вы можете использовать частный IP-адрес server-01:

      client-01

      • scp /etc/tinc/netname/hosts/client_01 sammy@server-01_private_IP:/tmp

      Затем на server-01 скопируйте файл конфигурации хоста client-01 в директорию /etc/tinc/netname/hosts/:

      server-01

      • sudo cp /tmp/client_01 /etc/tinc/netname/hosts/

      Оставаясь на server-01, скопируйте его файл конфигурации хоста на client-01:

      server-01

      • scp /etc/tinc/netname/hosts/server_01 user@client-01_private_IP:/tmp

      На client-01 скопируйте файл server-01 в соответствующую директорию:

      client-01

      • sudo cp /tmp/server_01 /etc/tinc/netname/hosts/

      На client-01 измените файл конфигурации хоста server-01 так, чтобы в поле Address был задан частный IP-адрес server-01. Так client-01 будет подключаться к VPN через частную сеть:

      client-01

      • sudo nano /etc/tinc/netname/hosts/server_01

      Измените директиву Address так, чтобы она указывала на** server-01** через частный IP-адрес:

      client-01:/etc/tinc/netname/hosts/server_01

      Address = server-01_private_IP
      Subnet = 10.0.0.1/32
      

      Сохраните и закройте файл. Теперь перейдем к оставшемуся узлу client-02.

      Обмен ключами между server-01 и client-02

      На client-02 скопируйте файл конфигурации хоста на server-01:

      client-02

      • scp /etc/tinc/netname/hosts/client_02 sammy@server-01_public_IP:/tmp

      Оставаясь на server-01, скопируйте файл конфигурации хоста client_02 в соответствующую директорию:

      server-01

      • sudo cp /tmp/client_02 /etc/tinc/netname/hosts/

      Затем скопируйте файл конфигурации хоста server-01 на client-02:

      server-01

      • scp /etc/tinc/netname/hosts/server_01 user@client-02_public_IP:/tmp

      На client-02 скопируйте файл server-01 в соответствующую директорию:

      client-02

      • sudo cp /tmp/server_01 /etc/tinc/netname/hosts/

      Если вы настраиваете только два клиентских узла, распределение открытых ключей завершено. Если же вы создаете более крупную сеть VPN, сейчас можно провести обмен ключами между другими узлами. Если вы хотите сделать так, чтобы два узла могли напрямую взаимодействовать друг с другом (без сервера-посредника), они должны обменяться ключами/файлами конфигурации хостов, а также должны иметь доступ к реальным сетевым интерфейсам друг друга. Также можно просто скопировать каждый файл конфигурации хоста на каждый узел сети VPN.

      Шаг 5 — Тестирование конфигурации

      На каждом узле, начиная с server-01, запустите tinc с помощью следующей команды:

      All servers

      • sudo tincd -n netname -D -d3

      Эта команда включает флаг -n, указывающий на имя нашей сети VPN, netname. Это полезно, если у вас настроено несколько сетей VPN и вы хотите указать, какую именно сеть нужно запустить. Также команда включает флаг -D, который предотвращает ветвление и отсоединение tinc и отключает механизм автоматического перезапуска tinc. Наконец, она включает флаг -d, предписывающий запустить tinc в режиме отладки с уровнем отладки 3.

      Примечание. Для демона tinc уровень отладки 3 показывает все запросы, которыми обмениваются любые два сервера, включая запросы аутентификации, обмен ключами и обновление списка соединений. Более высокие уровни отладки показывают дополнительную информацию о сетевом трафике, но сейчас нас интересует только возможность связи узлов друг с другом, поэтому уровень 3 будет достаточным. В условиях производственной среды можно использовать более низкий уровень отладки, чтобы не заполнять диски файлами журнала.

      Дополнительную информацию об уровнях отладки tinc можно найти в официальной документации.

      После запуска демона на каждом узле мы увидим имена каждого узла, подключенного к server-01. Теперь протестируем подключение через VPN.

      Отправьте в отдельном окне на client-02 эхо-запрос IP-адреса VPN client-01. Ранее мы назначили адрес 10.0.0.2:

      client-02

      Эхо-запрос должен быть выполнен, и вы увидите данные отладки в других окнах по подключению VPN. Это показывает, что client-02 может взаимодействовать через VPN через server-01 с client-01. Нажмите CTRL+C для прекращения эхо-запросов.

      Также вы можете использовать интерфейсы VPN для взаимодействия по сети, например подключений приложений, копирования файлов и запросов SSH.

      В каждом окне отладки демона tinc выполните выход из демона, нажав CTRL+.

      Шаг 6 — Настройка Tinc для запуска при загрузке

      Серверы Ubuntu используют systemd в качестве диспетчера системы по умолчанию для управления запуском и выполнением процессов. В связи с этим мы можем активировать сеть VPN netname для автоматического запуска при загрузке с помощью одной команды systemctl.

      Запустите следующую команду на каждом узле, чтобы настроить tinc VPN для запуска при загрузке компьютеров:

      All servers

      • sudo systemctl enable tinc@netname

      Теперь мы настроили Tinc для запуска на каждом компьютере и можем контролировать запуск с помощью команды systemctl. Если вы хотите запустить его сейчас, выполните следующую команду на каждом из ваших узлов:

      All servers

      • sudo systemctl start tinc@netname

      Примечание. Если вы используете несколько сетей VPN, вы можете активировать или запустить их одновременно:

      All servers

      • sudo systemctl start tinc@natename_01 tinc@netname_02 … tinc@netname_n

      Теперь ваша сеть tinc VPN полностью настроена и работает на каждом из ваших узлов.

      Заключение

      После прохождения этого обучающего руководства вы должны быть готовы развернуть сеть VPN для ваших собственных целей. Tinc — очень гибкий инструмент, позволяющий настроить любой узел для подключения к любому другому узлу (доступному ему через сеть). Это дает возможность создавать ячеистые сети VPN, не полагаясь на отдельные серверные узлы.



      Source link

      How to Install Tinc and Set Up a Basic VPN on Ubuntu 18.04


      Introduction

      Tinc is an open-source Virtual Private Network (VPN) daemon with useful features like encryption, optional compression, and automatic mesh routing that can opportunistically route VPN traffic directly between servers. These features differentiate tinc from other VPN solutions, and make it a good choice for creating a VPN out of many small, geographically distributed networks.

      In this tutorial, we will go over how to use tinc to create a secure VPN on which your servers can communicate as if they were on a local network. We will also demonstrate how to use tinc to set up a secure tunnel into a private network. We will be using Ubuntu 18.04 servers, but the configurations can be adapted for use with any other OS.

      Goals

      In order to cover multiple use cases, this tutorial outlines how to connect one client node to the VPN over a private network interface and another over a public one. You can, however, adapt this setup to suit your own needs. You’ll just need to plan out how you want your servers to access each other and adapt the examples presented in this tutorial to your own needs. If you are adapting this to your own setup, be sure to substitute the highlighted values in the examples with your own values. It may be in your interest, though, to first follow the tutorial as it’s written to make sure you understand the components and processes involved before modifying these instructions.

      To help keep things clear, this tutorial will refer to the servers like this:

      • server-01: All of the VPN nodes will connect to this machine, and the connection must be maintained for proper VPN functionality. Additional servers can be configured in the same way as this one to provide redundancy, if desired
      • client-01: Connects to the server-01 VPN node using its private network interface
      • client-02: Connects to the server-01 VPN node over the public network interface

      Note: Tinc itself doesn’t differentiate between servers (machines that host and deliver VPN services) and clients (the machines that connect to and use the secure private network), but it can be helpful to understand and visualize how tinc works by thinking of your servers like this.

      Here is a diagram of the VPN that we want to set up:

      Tinc VPN Setup

      The blue box represents our VPN and the pink represents the underlying private network. All three servers can communicate on the VPN, even though the private network is otherwise inaccessible to client-02.

      Prerequisites

      If you would like to follow this tutorial exactly, provision two Ubuntu 18.04 servers (server-01 and client-01) in the same datacenter and enable private networking on each. Then, create another Ubuntu 18.04 server (client-02) in a separate datacenter. Each server should have an administrative user and a firewall configured with ufw. To set this up, follow our initial server setup guide for Ubuntu 18.04.

      Additionally, later on in this tutorial we’ll need to transfer a few files between each machine using scp. Because of this, you’ll need to generate SSH keys on each of your servers, add both client-01 and client-02’s SSH keys to server-01’s authorized_keys file, and then add server-01’s SSH key to both client-01 and client-02’s authorized_keys files. For help setting this up, see our guide on How to Set Up SSH Keys on Ubuntu 18.04.

      Step 1 — Installing Tinc

      Tinc is available from the default Ubuntu APT repositories, which means we can install it with just a few commands.

      If you’ve not done so recently, run the following command on each server to update their respective package indexes:

      All servers

      Then install tinc on each server by running the following command:

      All servers

      With that, you’ve installed tinc on each of your servers. However, you’ll need to make some changes to tinc’s configuration on each machine in order to get your VPN up and running. Let’s begin with updating server-01.

      Step 2 — Configuring the Tinc Server

      Tinc requires that every machine that will be part of the VPN has the following three configuration components:

      • Tinc configuration files: There are three distinct files that configure the tinc daemon:
        • tinc.conf, which defines the netname, the network device over which the VPN will run, and other VPN options;
        • tinc-up, a script that activates the network device defined in tinc.conf after tinc is started;
        • tinc-down, which deactivates the network device whenever tinc stops.
      • Public/private key pairs: Tinc uses public/private key pairs to ensure that only users with valid keys are able to access the VPN.
      • Host configuration files: Each machine (or host) on the VPN has its own configuration file that holds the host’s actual IP address and the subnet where tinc will serve it

      Tinc uses a netname to distinguish one tinc VPN from another. This is helpful in cases where you want to set up multiple VPNs, but it’s recommended that you use a netname even if you are only planning on configuring one VPN. You can give your VPN whatever netname you like, but for simplicity we will call our VPN netname.

      On server-01, create the configuration directory structure for the VPN:

      server-01

      • sudo mkdir -p /etc/tinc/netname/hosts

      Use your preferred text editor to create a tinc.conf file. Here, we’ll use nano:

      server-01

      • sudo nano /etc/tinc/netname/tinc.conf

      Add the following lines to the empty file. These configure a tinc node named server_01 with a network interface called tun0 which will use IPv4:

      server-01:/etc/tinc/netname/tinc.conf

      Name = server_01
      AddressFamily = ipv4
      Interface = tun0
      

      Warning: Note how the value after the Name directive includes an underscore (_) rather than a hyphen (-). This is important, since tinc requires that the Name value contain only alphanumeric or underscore characters. If you use a hyphen here, you’ll encounter an error when you try to start the VPN later in this guide.

      Save and close the file after adding these lines. If you used nano, do so by pressing CTRL+X, Y, then ENTER.

      Next, create a host configuration file named server_01 in the hosts subdirectory. Ultimately, the client nodes will use this file to communicate with server-01:

      server-01

      • sudo nano /etc/tinc/netname/hosts/server_01

      Again, note that the name of this file contains an underscore rather than a hyphen. This way, it aligns with the Name directive in the tinc.conf file which will allow tinc to automatically append the server’s public RSA key to this file when we generate later on.

      Add the following lines to the file, making sure to include server-01’s public IP address:

      server-01:/etc/tinc/netname/hosts/server_01

      Address = server-01_public_IP_address
      Subnet = 10.0.0.1/32
      

      The Address field specifies how other nodes will connect to this server, and Subnet specifies which subnet this daemon will serve. Save and close the file.

      Next, generate a pair of public and private RSA keys for this host with the following command:

      server-01

      • sudo tincd -n netname -K4096

      After running this command, you’ll be prompted to enter filenames where tinc will save the public and private RSA keys:

      Output

      . . . Please enter a file to save private RSA key to [/etc/tinc/netname/rsa_key.priv]: Please enter a file to save public RSA key to [/etc/tinc/netname/hosts/server_01]:

      Press ENTER to accept the default locations at each prompt; doing so will tell tinc to store the private key in a file named rsa_key.priv and append the public key to the server_01 host configuration file.

      Next, create tinc-up, the script that will run whenever the netname VPN is started:

      server-01

      • sudo nano /etc/tinc/netname/tinc-up

      Add the following lines:

      server-01:/etc/tinc/netname/tinc-up

      #!/bin/sh
      ip link set $INTERFACE up
      ip addr add 10.0.0.1/32 dev $INTERFACE
      ip route add 10.0.0.0/24 dev $INTERFACE
      

      Here’s what each of these lines do:

      • ip link …: sets the status of tinc’s virtual network interface as up
      • ip addr …: adds the IP address 10.0.0.1 with a netmask of 32 to tinc’s virtual network interface, which will cause the other machines on the VPN to see server-01’s IP address as 10.0.0.1
      • ip route …: adds a route (10.0.0.0/24) which can be reached on tinc’s virtual network interface

      Save and close the file after adding these lines.

      Next, create a script to remove the virtual network interface when your VPN is stopped:

      server-01

      • sudo nano /etc/tinc/netname/tinc-down

      Add the following lines:

      server-01:/etc/tinc/netname/tinc-down

      #!/bin/sh
      ip route del 10.0.0.0/24 dev $INTERFACE
      ip addr del 10.0.0.1/32 dev $INTERFACE
      ip link set $INTERFACE down
      

      These lines have the opposite effects as those in the tinc-up script:

      • ip route …: deletes the 10.0.0.0/24 route
      • ip addr …: deletes the IP address 10.0.0.1 from tinc’s virtual network interface
      • ip link …: sets the status of tinc’s virtual network interface as down

      Save and close the file, then make both of these new network scripts executable:

      server-01

      • sudo chmod 755 /etc/tinc/netname/tinc-*

      As a final step of configuring server-01, add a firewall rule that will allow traffic through port 655, tinc’s default port:

      server-01

      server-01 is now fully configured and you can move on to setting up your client nodes.

      Step 3 — Configuring the Client Nodes

      Both of your client machines will require a slightly different configuration than the server, although the process will generally be quite similar.

      Because of the setup we’re aiming for in this guide, we will configure client-01 and client-02 almost identically with only a few slight differences between them. Hence, many of the commands given in this step must be run on both machines. Note, though, that if client-01 or client-02 require a specific command or special configuration, those instructions will be shown in a blue or red command block, respectively.

      On both client-01 and client-02, replicate the directory structure you created on server-01:

      client-01 & client-02

      • sudo mkdir -p /etc/tinc/netname/hosts

      Then create a tinc.conf file:

      client-01 & client-02

      • sudo nano /etc/tinc/netname/tinc.conf

      Add the following lines to the file on both machines:

      client-01 & client-02 /etc/tinc/netname/tinc.conf

      Name = node_name
      AddressFamily = ipv4
      Interface = tun0
      ConnectTo = server_01
      

      Be sure to substitute node_name with the respective client node’s name. Again, make sure this name uses an underscore (_) rather than a hyphen.

      Note that this file contains a ConnectTo directive pointing to server_01, while server-01’s tinc.conf file didn’t include this directive. By not including a ConnectTo statement on server-01, it means that server-01 will only listen for incoming connections. This works for our setup since it won’t connect to any other machines.

      Save and close the file.

      Next, create a host configuration file on each client node. Again, make sure the file name is spelled with an underscore instead of a hyphen:

      client-01 & client-02

      • sudo nano /etc/tinc/netname/hosts/node_name

      For client-01, add this line:

      client-01:/etc/tinc/netname/hosts/client_01

      Subnet = 10.0.0.2/32
      

      For client-02, add this line:

      client-02:/etc/tinc/netname/hosts/client_02

      Subnet = 10.0.0.3/32
      

      Note that each client has a different subnet that tinc will serve. Save and close the file.

      Next, generate the keypairs on each client machine:

      client-01 & client-02

      • sudo tincd -n netname -K4096

      Again as you did with server-01, when prompted to select files to store the RSA keys, press ENTER to accept the default choices.

      Following that, create the network interface start script on each client:

      client-01 & client-02

      • sudo nano /etc/tinc/netname/tinc-up

      For client-01, add these lines:

      client-01:/etc/tinc/netname/tinc-up

      #!/bin/sh
      ip link set $INTERFACE up
      ip addr add 10.0.0.2/32 dev $INTERFACE
      ip route add 10.0.0.0/24 dev $INTERFACE
      

      For client-02, add the following:

      client-02:/etc/tinc/netname/tinc-up

      #!/bin/sh
      ip link set $INTERFACE up
      ip addr add 10.0.0.3/32 dev $INTERFACE
      ip route add 10.0.0.0/24 dev $INTERFACE
      

      Save and close each file.

      Next, create the network interface stop script on each client:

      client-01 & client-02

      • sudo nano /etc/tinc/netname/tinc-down

      On client-01, add the following content to the empty file:

      client-01:/etc/tinc/netname/tinc-down

      #!/bin/sh
      ip route del 10.0.0.0/24 dev $INTERFACE
      ip addr del 10.0.0.2/32 dev $INTERFACE
      ip link set $INTERFACE down
      

      On client-02, add the following::

      client-02:/etc/tinc/netname/tinc-down

      #!/bin/sh
      ip route del 10.0.0.0/24 dev $INTERFACE
      ip addr del 10.0.0.3/32 dev $INTERFACE
      ip link set $INTERFACE down
      

      Save and close the files.

      Make networking scripts executable by running the following command on each client machine:

      client-01 & client-02

      • sudo chmod 755 /etc/tinc/netname/tinc-*

      Lastly, open up port 655 on each client:

      client-01 & client-02

      At this point, the client nodes are almost, although not quite, set up. They still need the public key that we created on server-01 in the previous step in order to authenticate the connection to the VPN.

      Step 4 — Distributing the Keys

      Each node that wants to communicate directly with another node must have exchanged public keys, which are inside of the host configuration files. In our case, server-01 needs to exchange public keys with the other nodes.

      Exchange Keys Between server-01 and client-01

      On client-01, copy its host configuration file to server-01. Because both client-01 and server-01 are in the same data center and both have private networking enabled, you can use server01’s private IP address here:

      client-01

      • scp /etc/tinc/netname/hosts/client_01 sammy@server-01_private_IP:/tmp

      Then on server-01, copy the client-01 host configuration file into the /etc/tinc/netname/hosts/ directory:

      server-01

      • sudo cp /tmp/client_01 /etc/tinc/netname/hosts/

      Then, while still on server-01, copy its host configuration file to client-01:

      server-01

      • scp /etc/tinc/netname/hosts/server_01 user@client-01_private_IP:/tmp

      On client-01, copy server-01’s file to the appropriate location:

      client-01

      • sudo cp /tmp/server_01 /etc/tinc/netname/hosts/

      On client-01, edit server-01’s host configuration file so the Address field is set to server-01’s private IP address. This way, client-01 will connect to the VPN via the private network:

      client-01

      • sudo nano /etc/tinc/netname/hosts/server_01

      Change the Address directive to point to server-01’s private IP address:

      client-01:/etc/tinc/netname/hosts/server_01

      Address = server-01_private_IP
      Subnet = 10.0.0.1/32
      

      Save and quit. Now let’s move on to our remaining node, client-02.

      Exchange Keys Between server-01 and client-02

      On client-02, copy its host configuration file to server-01:

      client-02

      • scp /etc/tinc/netname/hosts/client_02 sammy@server-01_public_IP:/tmp

      Then on server-01, copy the client_02 host configuration file into the appropriate location:

      server-01

      • sudo cp /tmp/client_02 /etc/tinc/netname/hosts/

      Then copy server-01’s host configuration file to client-02:

      server-01

      • scp /etc/tinc/netname/hosts/server_01 user@client-02_public_IP:/tmp

      On client-02, copy server-01’s file to the appropriate location:

      client-02

      • sudo cp /tmp/server_01 /etc/tinc/netname/hosts/

      Assuming you’re only setting up two client nodes, you’re finished distributing public keys. If, however, you’re creating a larger VPN, now is a good time to exchange the keys between those other nodes. Remember that if you want two nodes to directly communicate with each other (without a forwarding server between), they need to have exchanged their keys/hosts configuration files, and they need to be able to access each other’s real network interfaces. Also, it is fine to just copy each host’s configuration file to every node in the VPN.

      Step 5 — Testing the Configuration

      On each node, starting with server-01, start tinc with the following command:

      All servers

      • sudo tincd -n netname -D -d3

      This command includes the -n flag, which points to the netname for our VPN, netname. This is useful if you have more than one VPN set up and you need to specify which one you want to start. It also includes the -D flag, which prevents tinc from forking and detaching, as well as disables tinc’s automatic restart mechanism. Lastly, it includes the -d flag, which tells tinc to run in debug mode, with a debug level of 3.

      Note: When it comes to the tinc daemon, a debug level of 3 will show every request exchanged between any two of the servers, including authentication requests, key exchanges, and connection list updates. Higher debug levels show more information regarding network traffic, but for now we’re only concerned with whether the nodes can communicate with one another, so a level of 3 will suffice. In a production scenario, though, you would want to change to a lower debug level so as not to fill disks with log files.

      You can learn more about tinc’s debug levels by reviewing the official documentation.

      After starting the daemon on each node, you should see output with the names of each node as they connect to server-01. Now let’s test the connection over the VPN.

      In a separate window, on client-02, ping client-01’s VPN IP address. We assigned this to be 10.0.0.2, earlier:

      client-02

      The ping should work correctly, and you should see some debug output in the other windows about the connection on the VPN. This indicates that client-02 is able to communicate over the VPN through server-01 to client-01. Press CTRL+C to quit pinging.

      You may also use the VPN interfaces to do any other network communication, like application connections, copying files, and SSH.

      On each tinc daemon debug window, quit the daemon by pressing CTRL+.

      Step 6 — Configuring Tinc To Start Up on Boot

      Ubuntu servers use systemd as the default system manager to control starting and running processes. Because of this, we can enable the netname VPN to start up automatically at boot with a single systemctl command.

      Run the following command on each node to set the tinc VPN to start up whenever the machines boot:

      All servers

      • sudo systemctl enable tinc@netname

      Tinc is configured to start at boot on each of your machines and you can control it with the systemctl command. If you would like to start it now, run the following command on each of your nodes:

      All servers

      • sudo systemctl start tinc@netname

      Note: If you have multiple VPNs you enable or start each of them at once, like this:

      All servers

      • sudo systemctl start tinc@natename_01 tinc@netname_02 … tinc@netname_n

      With that, your tinc VPN fully configured and running on each of your nodes.

      Conclusion

      Now that you have gone through this tutorial, you should have a good foundation to build out your VPN to meet your needs. Tinc is very flexible, and any node can be configured to connect to any other node (that it can access over the network) so it can act as a mesh VPN without relying on one individual node.



      Source link