How to configure VLAN network interface in CentOS

Share this article

Introduction

In How to configure VLAN network in Ubuntu 20.04 in starting section we discussed what is VLAN and why we use VLANs and how it has made our life easy.

To summarize VLAN it is a logical concept of breaking large broadcast domains into smaller domains. The VLAN protocol is based on IEEE 802.1Q.  VLAN can be considered as a subnet. It provides the following advantages.

  • Logically divide the broadcast domain which reduces the size of the domains.
  • Add additional layer of security.
  • Device management is easier with VLAN.
  • QoS or other netowrk policies are easy to implement.
  • Provide network scalability.

In this article, we will show how to create the VLAN on the CentOS system.

Loading VLAN kernel Module

To use VLAN in CentOS make sure you have loaded the 8021q kernel module. In CentOS it is by default loaded.

Verify that module is loaded by using the following command:

modinfo 8021q

Output of the above command in my setup

[centos@test ~]$ modinfo 8021q
filename:       /lib/modules/3.10.0-1127.19.1.el7.x86_64/kernel/net/8021q/8021q.ko.xz
version:        1.8
license:        GPL
alias:          rtnl-link-vlan
retpoline:      Y
rhelversion:    7.8
srcversion:     1DD872AF3C7FF7FFD5B14D5
depends:        mrp,garp
intree:         Y
vermagic:       3.10.0-1127.19.1.el7.x86_64 SMP mod_unload modversions 
signer:         CentOS Linux kernel signing key
sig_key:        B1:6A:91:CA:C9:D6:51:46:4A:CB:7A:D9:B8:DE:D5:57:CF:1A:CA:27
sig_hashalgo:   sha256
[centos@test ~]$ 

If the module is not loaded you can use the following command to load 8021q module.

modprobe --first-time 8021q

VLAN configuration to assign

We want to assign the following IP address to our VLAN interface.

  • IP address: 10.0.200.2
  • Subnet Mask : 255.255.255.0
  • Default gateway192.168.100.1
  • VLAN ID : 200
  • Device: eth0.200

Configure VLAN with IP command

We will first create the VLAN interface eth0.200. Then we will add the IP address to the interface. The following commands will be used.

sudo ip link add link eth0 name eth0.200 type vlan id 200

Use ip a command to check does above command has added the VLAN interface, the output has mentioned below:

[centos@test ~]$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc pfifo_fast state UP group default qlen 1000
    link/ether fa:16:3e:96:e1:64 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.31/8 brd 10.255.255.255 scope global dynamic eth0
       valid_lft 80126sec preferred_lft 80126sec
    inet6 fe80::f816:3eff:fe96:e164/64 scope link 
       valid_lft forever preferred_lft forever
3: eth0.200@eth0: <BROADCAST,MULTICAST> mtu 1450 qdisc noop state DOWN group default qlen 1000
    link/ether fa:16:3e:96:e1:64 brd ff:ff:ff:ff:ff:ff
[centos@test ~]$ 

Add IP assignment with the following command

sudo ip addr add 10.0.200.2/24 dev eth0.200

Verify

Use the ip a command to list all interfaces in the machine.

[centos@test ~]$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc pfifo_fast state UP group default qlen 1000
    link/ether fa:16:3e:96:e1:64 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.31/8 brd 10.255.255.255 scope global dynamic eth0
       valid_lft 80017sec preferred_lft 80017sec
    inet6 fe80::f816:3eff:fe96:e164/64 scope link 
       valid_lft forever preferred_lft forever
3: eth0.200@eth0: <BROADCAST,MULTICAST> mtu 1450 qdisc noop state DOWN group default qlen 1000
    link/ether fa:16:3e:96:e1:64 brd ff:ff:ff:ff:ff:ff
    inet 10.0.200.2/24 scope global eth0.200
       valid_lft forever preferred_lft forever
[centos@test ~]$ 

This configuration is not permanent for permanent configuration use NetworkManager or network-scripts configuration.

Configure VLAN with NetworkManager or network-scripts

NetworkManager or network-scripts related Configuration files can be found in the /etc/sysconfig/network-scripts/ directory. This directory has multiple YAML files which are responsible for interface configuration also contain network scripts.

In the directory we will create ifcfg-eth0.200 yaml file and put all the configurational stuff inside that yaml file.

See the below mention /etc/sysconfig/network-scripts/ifcfg-eth0.200 file’s content to configure eth0.200 VLAN interface.

DEVICE="eth0.200"
BOOTPROTO="none"
ONBOOT="yes"
TYPE="Ethernet"
USERCTL="no"
PEERDNS="no"
PEERNTP="no"
IPADDR="10.0.200.2"
NETMASK="255.255.255.0"
GATEWAY="10.0.200.254"
VLAN="yes"

Once done, save the file and apply the changes by running the following command:

ifup eth0.200

Verify

Use the ip a command to list all interfaces in the machine.

[centos@test ~]$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc pfifo_fast state UP group default qlen 1000
    link/ether fa:16:3e:96:e1:64 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.31/8 brd 10.255.255.255 scope global dynamic eth0
       valid_lft 80017sec preferred_lft 80017sec
    inet6 fe80::f816:3eff:fe96:e164/64 scope link 
       valid_lft forever preferred_lft forever
3: eth0.200@eth0: <BROADCAST,MULTICAST> mtu 1450 qdisc noop state DOWN group default qlen 1000
    link/ether fa:16:3e:96:e1:64 brd ff:ff:ff:ff:ff:ff
    inet 10.0.200.2/24 brd 10.0.200.255 scope global eth0.200
       valid_lft forever preferred_lft forever
    inet6 fe80::f816:3eff:fe96:e164/64 scope link
       valid_lft forever preferred_lft forever
[centos@test ~]$ 

2 thoughts on “How to configure VLAN network interface in CentOS”

    1. Thank you for your comment. For now, this blog site is in starting phase and nowadays I am quite busy I will provide the RSS feed soon in the future.

Leave a Comment

Your email address will not be published. Required fields are marked *