Open Source How-To

You are here: Home > 4. Ubuntu 18.04 Server > การเพิ่ม vlan interface บน ubuntu 18.04 server

การเพิ่ม vlan interface บน ubuntu 18.04 server

 
 

บันทึกนี้ปรับปรุงล่าสุดเมื่อวันที่ 14-11-2561
ดูแลโดย WIBOON

  • ที่ network switch ได้ตั้งค่า port vlan6 untagged และ vlan7 tagged ไว้เสร็จแล้ว
  • เพื่อให้ physical network เดิม มี IP ของอีกวงแลนเพิ่มขึ้นมา
  • ตัวอย่างเดิมมี physical network interface ชื่อ enp2s0 ตั้งค่า static IP 192.168.6.17 (vlan6) เราจะเพิ่ม interface ชื่อว่า vlan7 ให้เป็น IP 192.168.6.201 CIDR subnet /25
  • ควรอ่านเพิ่มเติมด้วยคำสั่ง man vconfig


1. ติดตั้งซอฟต์แวร์

sudo apt install vlan

 

2. ทดสอบเพิ่ม vlan (ชั่วคราว)

sudo modprobe 8021q
sudo vconfig set_name_type VLAN_PLUS_VID_NO_PAD
sudo vconfig add enp2s0 7
sudo ip addr add 192.168.6.201/25 dev vlan7
sudo ip link set up vlan7

ตรวจสอบด้วยคำสั่ง ifconfig

3. ตั้งค่าเพิ่ม vlan ถาวรเมื่อ boot

sudo su -c 'echo "8021q" >> /etc/modules'

และแก้ไขที่ไฟล์ /etc/netplan/01-netcfg.yaml

sudo nano /etc/netplan/01-netcfg.yaml

ตัวอย่าง

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    enp2s0:
      dhcp4: no
      dhcp6: no
      addresses: [ 192.168.6.17/25 ]
      gateway4: 192.168.6.1
      nameservers:
          addresses:
              - "192.100.77.10"
  vlans:
    vlan7:
      id: 7
      link: enp2s0
      addresses: [ 192.168.6.201/25 ]
      gateway4: 192.168.6.129

 

4. เพิ่ม script ให้ตั้งค่า iproute2 เพื่อให้เครื่องใน net อื่น ping IP ของ server ได้ (ถ้าต้องการ)

sudo nano /etc/iproute2/rt_tables

เพิ่มต่อท้ายไฟล์

1 rt1
2 rt2

และเพิ่มไฟล์ post-up

sudo nano /etc/networkd-dispatcher/routable.d/post-up

เพิ่ม script ดังนี้

#!/bin/bash
if [ "$IFACE" == "enp2s0" ]; then
  ip route add 192.168.6.0/25 dev enp2s0 src 192.168.6.17 table rt1
  ip route add default via 192.168.6.1 dev enp2s0 table rt1
  ip rule add from 192.168.6.17/32 table rt1
  ip rule add to 192.168.6.17/32 table rt1
else
  if [ "$IFACE" == "vlan7" ]; then
    ip route add 192.168.6.128/25 dev vlan7 src 192.168.6.201 table rt2
    ip route add default via 192.168.6.129 dev vlan7 table rt2
    ip rule add from 192.168.6.201/32 table rt2
    ip rule add to 192.168.6.201/32 table rt2
  fi
fi

เปลี่ยนให้เป็น executable file

sudo chmod +x /etc/networkd-dispatcher/routable.d/post-up

 

5. สั่งให้มีผลทันที

sudo netplan apply

 

6. ตรวจสอบด้วย ifconfig จะได้ผลประมาณนี้

enp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.6.17  netmask 255.255.255.128  broadcast 192.168.6.127
        ether 88:55:39:85:91:xx  txqueuelen 1000  (Ethernet)
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
vlan7: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.6.201  netmask 255.255.255.128  broadcast 192.168.6.255
        ether 88:55:39:85:91:xx  txqueuelen 1000  (Ethernet)



การตั้งค่า network bridge interface on top of vlan
หลังจากที่เรามี vlan7 แล้ว หากจะปรับเปลี่ยนให้เป็น bridge interface ชื่อ br1 เพื่อใช้ประโยชน์ในการทำ virtualization ก็ให้แก้ไข 2 ไฟล์ คือ

sudo nano /etc/netplan/01-netcfg.yaml

แก้ไขประมาณนี้

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    enp2s0:
      dhcp4: no
      dhcp6: no
  bridges:
    br0:
      dhcp4: no
      dhcp6: no
      interfaces:
        - enp2s0
      addresses: [ 192.168.6.17/25 ]
      gateway4: 192.168.6.1
      nameservers:
          addresses:
              - "192.100.77.10"
  vlans:
    vlan7:
      id: 7
      link: enp2s0
  bridges:
    br1:
      dhcp4: no
      dhcp6: no
      interfaces:
        - vlan7
      addresses: [ 192.168.6.201/25 ]
      gateway4: 192.168.6.129

และแก้ไขไฟล์ script

sudo nano /etc/networkd-dispatcher/routable.d/post-up

เปลี่ยนชื่อ interface เป็น br0 กับ br1

#!/bin/bash
if [ "$IFACE" == "br0" ]; then
  ip route add 192.168.6.0/25 dev br0 src 192.168.6.17 table rt1
  ip route add default via 192.168.6.1 dev br0 table rt1
  ip rule add from 192.168.6.17/32 table rt1
  ip rule add to 192.168.6.17/32 table rt1
else
  if [ "$IFACE" == "br1" ]; then
    ip route add 192.168.6.128/25 dev br1 src 192.168.6.201 table rt2
    ip route add default via 192.168.6.129 dev br1 table rt2
    ip rule add from 192.168.6.201/32 table rt2
    ip rule add to 192.168.6.201/32 table rt2
  fi
fi

แล้ว สั่งให้มีผลทันที

sudo netplan apply

 

ตรวจสอบด้วย ifconfig จะได้ผลประมาณนี้

br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.6.17  netmask 255.255.255.128  broadcast 192.168.6.127
        ether ee:ee:9e:eb:11:xx  txqueuelen 1000  (Ethernet)
br1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.6.201  netmask 255.255.255.128  broadcast 192.168.6.255
        ether 44:ff:e5:18:a5:xx  txqueuelen 1000  (Ethernet)
enp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 88:55:39:85:91:xx  txqueuelen 1000  (Ethernet)
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
vlan7: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 88:55:39:85:91:xx  txqueuelen 1000  (Ethernet)
nach oben