Setting Up VLANs in Linux - /etc/network/interfaces

Learn how to configure VLANs in Linux using both the command line and the /etc/network/interfaces file for flexibility and convenience.

Virtual LANs (VLANs) provide network segmentation and management flexibility in Linux. In this comprehensive guide, we'll walk you through the process of setting up VLANs using both the command line and the /etc/network/interfaces file. We'll also demonstrate how to configure a VLAN with an assigned IP address, empowering you to effectively manage your network infrastructure.

Using Command Line:

  1. Check VLAN Support:

    Ensure that your Linux distribution supports VLANs by verifying the availability of the 8021q module:

    sudo modprobe 8021q
    lsmod | grep 8021q
  2. Create and Activate VLAN Interface:

    Create a VLAN interface associated with a physical network interface (e.g., eth0) and assign a VLAN ID (e.g., VLAN 100):

    sudo ip link add link eth0 name eth0.100 type vlan id 100
    sudo ip link set dev eth0.100 up
  3. Configure IP Address:

    Assign an IP address to the VLAN interface (e.g., 192.168.1.100/24):

    sudo ip addr add 192.168.1.100/24 dev eth0.100
  4. Test Connectivity:

    Verify the VLAN configuration and test connectivity by pinging a device within the VLAN or external networks:

    ping 192.168.1.1
    ping google.com

Using /etc/network/interfaces:

  1. Edit the Interfaces File:

    Open the /etc/network/interfaces file using a text editor:

    sudo nano **/etc/network/interfaces**
  2. Add VLAN Configuration:

    Add the following lines to define the VLAN interface (e.g., eth0.100) and assign an IP address (e.g., 192.168.1.100/24):

    auto eth0.100
    iface eth0.100 inet static
        address 192.168.1.100
        netmask 255.255.255.0
  3. Save and Exit the File.

  4. Restart Networking Service:

    Restart the networking service to apply the changes:

    sudo systemctl restart networking