Linux add vlan interface

VLAN

This article explains how to configure a VLAN using iproute2 and systemd-networkd or netctl.

Instant Configuration

Previously, Arch Linux used the vconfig command to setup VLANs. This command was superseded by the ip command. Make sure you have iproute2 installed.

In the following examples, let us assume the interface is eth0 , the assigned name is eth0.100 and the vlan id is 100 .

Create the VLAN device

Add the VLAN with the following command:

# ip link add link eth0 name eth0.100 type vlan id 100

Run ip link to confirm that it has been created.

This interface behaves like a normal interface. All traffic routed to it will go through the master interface (in this example, eth0 ) but with a VLAN tag. Only VLAN-aware devices can accept them if configured correctly, else the traffic is dropped.

Using a name like eth0.100 is just convention and not enforced; you can alternatively use eth0_100 or something descriptive like IPTV . To see the VLAN ID on an interface, in case you used an unconventional name:

The -d flag shows full details of an interface:

# ip -d addr show 4: eth0.100@eth0: mtu 1500 qdisc noqueue state UP group default link/ether 96:4a:9c:84:36:51 brd ff:ff:ff:ff:ff:ff promiscuity 0 vlan protocol 802.1Q id 100 inet6 fe80::944a:9cff:fe84:3651/64 scope link valid_lft forever preferred_lft forever

Add an IP

Now add an IPv4 address to the just created VLAN link, and activate the link:

# ip addr add 192.168.100.1/24 brd 192.168.100.255 dev eth0.100 # ip link set dev eth0.100 up

Turning down the device

To cleanly shut down the setting before you remove the link, you can do:

# ip link set dev eth0.100 down

Removing the device

Removing a VLAN interface is significantly less convoluted

Persistent Configuration

systemd-networkd

Single interface

Use the following number-prefixed configuration files (Remember the file contents are case sensitive and the number-prefix can be changed):

/etc/systemd/network/10-eth0.network
[Match] Name=eth0 [Network] DHCP=ipv4 ;these are arbitrary names, but must match the *.netdev and *.network files VLAN=eth0.100 VLAN=eth0.200
/etc/systemd/network/20-eth0.100.netdev
[NetDev] Name=eth0.100 Kind=vlan [VLAN] Id=100
/etc/systemd/network/21-eth0.200.netdev
[NetDev] Name=eth0.200 Kind=vlan [VLAN] Id=200

You will have to have associated .network files for each .netdev to handle addressing and routing. For example, to set the eth0.100 interface with a static IP and the eth0.200 interface with DHCP (but ignoring the supplied default route), use:

/etc/systemd/network/30-eth0.100.network
[Match] Name=eth0.100 [Network] DHCP=no [Address] Address=192.168.0.25/24
/etc/systemd/network/31-eth0.200.network
[Match] Name=eth0.200 [Network] DHCP=yes [DHCP] UseRoutes=false

Then enable systemd-networkd.service . See systemd-networkd for details.

Читайте также:  Linux чем распаковать exe

Single interface with multiple VLANs each with its own gateway

Each vlan gets its own routing table and a RoutingPolicyRule that specifies which source ip addresses this routing applies to.

/etc/systemd/network/10-eth0.network
[Match] Name=eth0 [Network] VLAN=eth0.10 VLAN=eth0.11 DNS=192.168.100.101 DNS=192.168.100.102
/etc/systemd/network/20-eth0.10.netdev
[NetDev] Name=eth0.10 Kind=vlan [VLAN] Id=10
/etc/systemd/network/30-eth0.10.network
[Match] Name=eth0.10 [Network] Address=192.168.1.14/24 Address=192.168.1.24/24 [Route] Gateway=192.168.1.1 Table=10 [RoutingPolicyRule] From=192.168.1.0/24 Table=10
/etc/systemd/network/21-eth0.11.netdev
[NetDev] Name=eth0.11 Kind=vlan [VLAN] Id=11
/etc/systemd/network/31-eth0.11.network
[Match] Name=eth0.11 [Network] Address=192.168.100.54/24 [Route] Gateway=192.168.100.1 Table=11 [RoutingPolicyRule] From=192.168.100.0/24 Table=11
Checks
0: from all lookup local 0: from 192.168.1.0/24 lookup 10 0: from 192.168.100.0/24 lookup 11 32766: from all lookup main 32767: from all lookup default

Use ip route list table . E.g.:

default via 192.168.1.1 dev enp1.10 proto static
default via 192.168.100.1 dev enp1.11 proto static

Bonded interface

Similar to above, you are just going to stack more of the concepts in place. You will want to ensure that you have got a bond set up in your switch and also make sure its a trunk with tagged vlans corresponding to what you create below. Convention would be to create a bond interface with the name bond0 , however there is a known issue where the bonding module, when loaded, creates a bond device of the name bond0 which systemd then refuses to configure (as systemd tries to respectfully leave alone any device it did not create).

Tip: To prevent the bonding module to create an initial bond0 interface, set the max_bonds option of the bonding module to 0 (default value is 1 ):

options bonding max_bonds=0

For the purposes of this write up, we are going to use bondname and you can make the choice yourself.

First, we create the bond device:

/etc/systemd/network/bondname.netdev
[NetDev] Name=bondname Kind=bond [Bond] Mode=802.3ad LACPTransmitRate=fast

Now create a .network directive that references the vlans and interface carriers. In this case we will use the convention for a dual port fiber module:

/etc/systemd/network/bondname.network
[Match] Name=bondname [Network] VLAN=vlan10 VLAN=vlan20 VLAN=vlan30 BindCarrier=enp3s0f0 enp3s0f1

We are using the vlan naming convention here, you can use something else but realize that this is a named reference so you will have to have a corresponding set of files with the same name.

We will now set up the physical network interfaces:

/etc/systemd/network/enp3s0f0.network
[Match] Name=enp3s0f0 [Network] Bond=bondname
/etc/systemd/network/enp3s0f1.network
[Match] Name=enp3s0f1 [Network] Bond=bondname

At this time you could reboot, and likely should, because the bonded interface is created at boot time. Restarting systemd-networkd will consume changes from these files typically, but device creation seems to occur at startup.

Читайте также:  Linux ping есть интернета нет

We will now set up the VLANs. You should be aware that having multiple VLANs can result in a situation where your machine has multiple default routes, so you will need to specify a Destination directive in the network directives to ensure that only one VLAN is being used for a default route. In this case we will use the VLAN with an ID of 10 as our default route.

/etc/systemd/network/vlan10.netdev
[NetDev] Name=vlan10 Kind=vlan [VLAN] Id=10

Now create the associated network directive to set an address:

/etc/systemd/network/vlan10.network
[Match] Name=vlan10 [Network] VLAN=vlan10 [Address] Address=10.10.10.2/24 [Route] Destination=0.0.0.0/0 Gateway=10.10.10.1

We will create a similar pair of files for the VLAN with an ID of 20:

/etc/systemd/network/vlan20.netdev
[NetDev] Name=vlan20 Kind=vlan [VLAN] Id=20
/etc/systemd/network/vlan20.network
[Match] Name=vlan20 [Network] VLAN=vlan20 [Address] Address=10.10.20.2/24 [Route] Destination=10.10.20.0/24 Gateway=10.10.20.1

And again for the VLAN with an ID of 30:

/etc/systemd/network/vlan30.netdev
[NetDev] Name=vlan30 Kind=vlan [VLAN] Id=30
/etc/systemd/network/vlan30.network
[Match] Name=vlan30 [Network] VLAN=vlan30 [Address] Address=10.10.30.2/24 [Route] Destination=10.10.30.0/24 Gateway=10.10.30.1

Note that the Destination on vlan10 is set to 0.0.0.0/0 , which will match all outbound, becoming the default route.

netctl

You can use netctl for this purpose, see the self-explanatory example profiles in <>>.

Setting bridge IP

Sometimes you might want to configure the bridge ip on which docker operates, for example when the default ip clashes with other ip addresses in the network. Docker has a straight forward way of setting the bip (bridge IP) via the /etc/docker/daemon.json . When this file does not exist yet you can create it.

Troubleshooting

udev renames the virtual devices

An annoyance is that udev may try to rename virtual devices as they are added, thus ignoring the name configured for them (in this case eth0.100 ).

For instance, if the following commands are issued:

# ip link add link eth0 name eth0.100 type vlan id 100 # ip link show

This could generate the following output:

1: lo: mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0: mtu 1500 qdisc mq state UP qlen 1000 link/ether aa:bb:cc:dd:ee:ff brd ff:ff:ff:ff:ff:ff 3: rename1@eth0: mtu 1500 qdisc noqueue state DOWN link/ether aa:bb:cc:dd:ee:ff brd ff:ff:ff:ff:ff:ff

udev has ignored the configured virtual interface name eth0.100 and autonamed it rename1.

The solution is to edit /etc/udev/rules.d/network_persistent.rules and append DRIVERS==»?*» to the end of the physical interface’s configuration line.

For example, for the interface aa:bb:cc:dd:ee:ff (eth0):

/etc/udev/rules.d/network_persistent.rules
SUBSYSTEM=="net", ATTR=="aa:bb:cc:dd:ee:ff", NAME="eth0", DRIVERS=="?*"

A reboot should mean that VLANs configure correctly with the names assigned to them.

Источник

Linux add vlan interface

В статье расcмотрен вопрос, как включить и настроить в Linux (Debian, Ubuntu, xUbuntu, Linux Mint, Knoppix и т.п.) поддержку VLAN 802.1Q на сетевом интерфейсе.

Читайте также:  Dr web workstation linux

Недавно у меня возникла задача работать сразу в нескольких виртуальных локальных сетях (VLAN). В ОС Linux сделать это оказалось довольно просто. Раcсмотрим все по порядку.

Включение поддержки vlan в Linux Mint, Debian, Ubuntu

Первым делом установим пакет «vlan» для настройки VLAN-ов в Linux. Данный пакет будет содержать в себе утилиту «vconfig» для настройки VLAN.

Допустим, нам нужно подключить сеть VLAN200. Для этого создадим логический интерфейс «eth0.200», привязанный к физическому интерфейсу «eth0», который будет обрабатывать пакеты с тегом 200.

Если выскочит ошибка «. Maybe you need to load the 8021q module. «, надо будет подгрузить модуль 8021q в ядро Linux следующей командой:

Добавление vlan в Linux с помощью утилиты vconfig

Пробуем еще раз добавить vlan с тегом 200, затем выводим командой «ifconfig -a» список всех интерфейсов:

sudo vconfig add eth0 200 ifconfig -a eth0 Link encap:Ethernet HWaddr XX:XX:XX:XX:XX:XX inet addr:192.168.10.10 Bcast:192.168.10.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:79966 errors:0 dropped:256 overruns:0 frame:0 TX packets:48599 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:25422338 (25.4 MB) TX bytes:7961027 (7.9 MB) eth0.200 Link encap:Ethernet HWaddr XX:XX:XX:XX:XX:XX BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) lo Link encap:Локальная петля (Loopback) inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:189 errors:0 dropped:0 overruns:0 frame:0 TX packets:189 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:39060 (39.0 KB) TX bytes:39060 (39.0 KB)

Из вывода видно, что появился еще один логический интерфейс «eth0.200», который будет обрабатывать все пакеты, помеченные тегом 200 (принадлежащие сети VLAN200).

Повесим ip-адрес на новый интерфейс «eth0.200».

sudo ifconfig eth0.200 192.168.8.10 netmask 255.255.255.0 up sudo ifconfig -a . eth0.500 Link encap:Ethernet HWaddr XX:XX:XX:XX:XX:XX inet addr:192.168.8.10 Bcast:192.168.8.255 Mask:255.255.255.0 .

На этом настройка завершена. Следует отметить, что после перезагрузки интерфейс «eth0.200» слетит.

Добавление vlan в Linux Mint, Debian, Ubuntu через файл interfaces

Для того, чтобы интерфейс поднимался каждый раз после перезагрузки системы, его надо добавить в файл «/etc/network/interfaces».

Пример содержимого файла interfases.

#interface loopback auto lo iface lo inet loopback #Interface eth0 - untagged auto eth0 iface eth0 inet static address 192.168.10.10 netmask 255.255.255.0 gateway 192.168.10.1 dns-nameservers 8.8.8.8 #Interface eth0.200 - tagged vlan 200 auto eth0.200 iface eth0.200 inet static address 192.168.8.10 netmask 255.255.255.0 #Interface eth1 - untagged #auto eth1 #iface eth1 inet dhcp

Физический интерфейс «eth0» работает в обычном режиме т.е. все нетегированные (untagged) пакеты обрабатываются на этом интерфейсе.

Логический интерфейс «eth0.200» работает в режиме обработки только тегированных (tagged) пакетов с тегом 200, т.е. принадлежит VLAN200.

По аналогии можно создавать и другие VLAN интерфейсы.

На этом все. Рассмотрение процесса настройки VLAN в ОС Linux (Debian, xUbuntu, Linux Mint, Knoppix) завершено. Надеюсь, что эта статья была полезной. Комментируем, подписываемся, ну и всем пока:)

Источник

Оцените статью
Adblock
detector