Adding vlan to Ubuntu for ABillS

Let me give you an example of massively adding VLANs to Ubuntu Server.

Switch to the root user:

sudo -i

First of all, install the vlan package and load the 8021q module:

apt-get install vlan
modprobe 8021q

To autorun it after restarting the system, open the file /etc/modules, for example, in the text editor nano (Ctrl+X to exit, y/n to save or cancel changes):

nano /etc/modules

And add if it’s not there:

8021q

Create a script:

nano /etc/network/vlan.sh

Add content to it (in IFACE we specify the network interface for vlan, in VLANS – the VLAN list):

#!/bin/bash
IFACE="eth1"
VLANS="51,200-350,700-1000"
 
  /sbin/vconfig set_name_type VLAN_PLUS_VID_NO_PAD
  VLANS=`echo ${VLANS} | sed 'N;s/\n/ /' |sed 's/,/ /g'`
  for i in $VLANS; do
    if [[ $i =~ - ]]; then
      IFS='-' read -a start_stop <<< "$i"
      for cur_iface in `seq ${start_stop[0]} ${start_stop[1]}`;
      do
        echo "${cur_iface}";
        /sbin/vconfig add ${IFACE} ${cur_iface}
        /sbin/ifconfig vlan${cur_iface} up
      done
    else
    echo "$i";
      /sbin/vconfig add ${IFACE} ${i}
      /sbin/ifconfig vlan${i} up
    fi;
  done

We make the script executable:

chmod +x /etc/network/vlan.sh

Run the script:

/etc/network/vlan.sh

To autorun the script, open the configuration of the network interfaces:

nano /etc/network/interfaces

And add a line at the end of the file or in the network interface settings section:

post-up /etc/network/vlan.sh

See also my articles:
Configuring VLANs in Ubuntu
Install and configure accel-ppp (IPoE) for ABillS

Leave a comment

Leave a Reply