VLAN: Difference between revisions

From Alpine Linux
(added info on using multiple gateways)
Line 67: Line 67:
bridge_fd 0.0
bridge_fd 0.0
</pre>
</pre>
== Example with two interfaces on the same adapter. One with vlan and one without ==
Since linux doesn't allow multiple default gateways we need to use a second routing table using iproute2
{{Cmd|apk add iproute2}}
Then we'll add a new routig table to the config file
{{Cmd|echo "1 rt2" >> /etc/iproute2/rt_tables}}
Only thing that's left is to
<pre>
auto lo
iface lo inet loopback
# the native interface without a vlan (also called untagged)
auto eth0
iface eth0 inet static
        address 192.168.1.100
        netmask 255.255.255.0
        gateway 192.168.1.1
# second interface with the vlan tag 5
# note that we're adding the gateway through the post-up commands
auto eth0.5
iface eth0.5 inet static
    address 192.168.5.100
    netmask 255.255.255.0
    post-up ip route add 192.168.5.0/24 dev eth0.5 src 192.168.5.100 table rt2
    post-up ip route add default via 192.168.5.1 dev eth0.5 table rt2 # the actual gateway for this interface
    post-up ip rule add from 192.168.5.100/32 table rt2
    post-up ip rule add to 192.168.5.100/32 table rt2
</pre>
Note that if you want to add a third interface this way, you'll have to add another routing table


[[Category:Networking]]
[[Category:Networking]]

Revision as of 18:27, 28 December 2021

This article shows how to configure a network interface as an IEEE 802.1q VLAN trunk.

Note: Alpine Linux v2.4 or later is required

Installation

First, install the vlan package. This will give you support for vlans in the /etc/network/interfaces file.

apk add vlan

Configuration

Edit the /etc/network/interfaces file:

auto eth0.8
iface eth0.8 inet static
	address 192.168.0.2
	netmask 255.255.255.0
	gateway 192.168.0.1

With the vlan package installed, ifup will find the trailing .8 in eth0.8 and will create a vlan interface with vid 8 over eth0.

Alternativly with vlan8 over eth0:

auto vlan8
iface vlan8 inet static
	address 192.168.0.2
	netmask 255.255.255.0
	gateway 192.168.0.1
	vlan-raw-device eth0

A static ip address was used in the examples shown above, but dhcp can be used as well.

Example with bridges associated with VLANs over bonding with differing MTUs on the various VLANs

This serves as an example of some of the more complicated networking possible. Particularly, this would work well for a hypervisor attached to a dedicated storage VLAN. Less complicated implementations can be achieved by merely removing the non-applicable parts.

auto lo
iface lo inet loopback

auto bond0
iface bond0 inet manual
bond_slaves eth0 eth1
bond_mode 802.3ad
bond_miimon 100
bond_xmit_hash_policy layer2+3
post-up ip link set dev bond0 mtu 9000

iface bond0.1 inet manual

auto br1
iface br1 inet static
address 192.168.1.196
netmask 255.255.255.0
gateway 192.168.1.1
bridge_ports bond0.1
bridge_stp off
bridge_fd 0.0
post-up ip link set dev bond0.1 mtu 1500

iface bond0.10 inet manual

auto br10
iface br10 inet static
address 192.168.10.1
netmask 255.255.255.0
bridge_ports bond0.10
bridge_stp off
bridge_fd 0.0

Example with two interfaces on the same adapter. One with vlan and one without

Since linux doesn't allow multiple default gateways we need to use a second routing table using iproute2

apk add iproute2

Then we'll add a new routig table to the config file

echo "1 rt2" >> /etc/iproute2/rt_tables

Only thing that's left is to

auto lo
iface lo inet loopback

# the native interface without a vlan (also called untagged)

auto eth0
iface eth0 inet static
        address 192.168.1.100
        netmask 255.255.255.0
        gateway 192.168.1.1

# second interface with the vlan tag 5
# note that we're adding the gateway through the post-up commands
auto eth0.5
iface eth0.5 inet static
    address 192.168.5.100
    netmask 255.255.255.0
    post-up ip route add 192.168.5.0/24 dev eth0.5 src 192.168.5.100 table rt2
    post-up ip route add default via 192.168.5.1 dev eth0.5 table rt2 # the actual gateway for this interface
    post-up ip rule add from 192.168.5.100/32 table rt2
    post-up ip rule add to 192.168.5.100/32 table rt2

Note that if you want to add a third interface this way, you'll have to add another routing table