Add vlan to interface with unique mac - different syntax for different Linux distros

10,188

Solution 1

Please note that a vlan and a macvlan are two different things. A vlan is a common standard (802.1q) for separating broadcast domains. This means you can create several logical networks on a physical network. All used components, including network switches need to be compatible.

Macvlan's are used on Linux systems to provide a separated virtual interface. Those can be created on physical as well as on vlan interfaces. Macvlan's are non-standard and are not related to other network devices.

First, you should create your vlan interface, as shown in your example. After this, you should add one or more macvlan interfaces like this:

ip link add macvlan1 link eth0.1 type macvlan
ip link add macvlan2 link eth0.1 type macvlan

Check with the ip link command if the new interfaces are created. If so, most probably you should assign IP addresses using the ip addr command and bring the interface up with:

ip link set dev macvlan1 up
ip link set dev macvlan2 up

According to the question, I used vlan 1 in my examples. However, please don't use vlan 1 in a production environment; this id can be interpreted as having no vlan at all by some devices.

Macvlans are introduced in kernel 2.6.23 as experimental and in 3.9 as production. The iproute2 (ip command) package has supported for macvlans for a long time, so this should work with all recent Linux distros, even including Red Hat 6.

Solution 2

You can assign VLAN MAC address with ip command:

# ip link add link <interface> name <vlan interface name> address <mac> type vlan id <vlan ID>

Example:

# ip link add link ens33 name myvlan123 address 00:0c:29:ed:ff:ff type vlan id 123

Check:

# ip a
....
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:ed:ff:ea brd ff:ff:ff:ff:ff:ff
    inet 192.168.101.180/24 brd 192.168.101.255 scope global ens33
       valid_lft forever preferred_lft forever
3: myvlan123@ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:0c:29:ed:ff:ff brd ff:ff:ff:ff:ff:ff
    inet6 fe80::2fda:8335:9f2:b9c7/64 scope link 
       valid_lft forever preferred_lft forever

Main interface is 00:0c:29:ed:ff:ea and vlan is now 00:0c:29:ed:ff:ff.

You can see more help with:

# ip link help

Also check that you have vlan kernel module loaded:

# lsmod | grep -i 8021q

Some distributions may have it under vlan name. You might also have to install the vlan or 8021q package (pacman, yum, apt, ..).

Share:
10,188

Related videos on Youtube

TSG
Author by

TSG

Updated on September 18, 2022

Comments

  • TSG
    TSG over 1 year

    I need to add a subinterface to an existing interface, which is on a VLAN. Furthermore, the subinterface must have it's own MAC address. Through the man pages I found this syntax which works fine on CentOS/RH:

    /usr/sbin/ip link add link eth0 name eth0.1 address 00:11:22:33:44:5F type vlan id 1
    

    Through googling I discovered that some distros use the keyword 'macvlan' instead of 'vlan' for this command. Does anyone know which Linux distros use the macvlan keyword instead of vlan keyword?

  • TSG
    TSG over 7 years
    The question does not make reference to "MACVLAN", it reference adding a unique MAC address to a subinterface. You response seems off topic.
  • code_dredd
    code_dredd about 6 years
    @TSG MACVLANs assign different MAC addresses to the interfaces. It does its work at Layer 2 instead of Layer 3 (where vlans have different IPs but all with the same MAC address). This answer may not be completely off topic.