Post

Add / Remove port groups and VLANs using the ESXi command line

Add / Remove port groups and VLANs using the ESXi command line

Overview

Useful commands for modifying port groups and VLAN settings on ESXi standard vSwitches. Can be used in scripts of course to do additions / removals / changes in bulk.

Versions

These commands have been tested with the below ESXi versions:

Product Version Update Patch
VMware ESXi 7.0.3 3 120

List vSwitches:

Command

esxcli network vswitch standard list

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[root@vrtx-25-esxi-n1:~] esxcli network vswitch standard list
vSwitch-B
   Name: vSwitch-B
   Class: cswitch
   Num Ports: 8580
   Used Ports: 3
   Configured Ports: 128
   MTU: 1500
   CDP Status: listen
   Beacon Enabled: false
   Beacon Interval: 1
   Beacon Threshold: 3
   Beacon Required By: 
   Uplinks: vmnic1
   Portgroups: VL423, VL422, VL421

vSwitch0
   Name: vSwitch0
   Class: cswitch
   Num Ports: 8580
   Used Ports: 4
   Configured Ports: 128
   MTU: 1500
   CDP Status: listen
   Beacon Enabled: false
   Beacon Interval: 1
   Beacon Threshold: 3
   Beacon Required By: 
   Uplinks: vmnic0
   Portgroups: VL420, Management Network

Create a new standard vSwitch

Command

esxcli network vswitch standard add --vswitch-name=vSwitch3

Example

1
[root@vrtx-25-esxi-n1:~] esxcli network vswitch standard add --vswitch-name=vSwitch3

Verify the new vSwitch has been created

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@vrtx-25-esxi-n1:~] esxcli network vswitch standard list

... output shortened for readability ...

vSwitch3
   Name: vSwitch3
   Class: cswitch
   Num Ports: 8580
   Used Ports: 1
   Configured Ports: 128
   MTU: 1500
   CDP Status: listen
   Beacon Enabled: false
   Beacon Interval: 1
   Beacon Threshold: 3
   Beacon Required By: 
   Uplinks: 
   Portgroups: 
[root@vrtx-25-esxi-n1:~] 

List port groups

Command

esxcli network vswitch standard portgroup list

Example

1
2
3
4
5
6
7
8
9
[root@vrtx-25-esxi-n1:~] esxcli network vswitch standard portgroup list
Name                Virtual Switch  Active Clients  VLAN ID
------------------  --------------  --------------  -------
Management Network  vSwitch0                     1        0
VL420               vSwitch0                     0        0
VL421               vSwitch-B                    0      421
VL422               vSwitch-B                    0      422
VL423               vSwitch-B                    0      423
[root@vrtx-25-esxi-n1:~] 

Add port group named “VLAN-900” to vSwitch0

Command

cfg-vswitch --add-pg=VLAN-900 vSwitch0

Example

1
2
[root@vrtx-25-esxi-n1:~] esxcfg-vswitch --add-pg=VLAN-900 vSwitch0
[root@vrtx-25-esxi-n1:~] 

Verifying that the portgroup has been added

1
2
3
4
5
6
7
8
9
[root@vrtx-25-esxi-n1:~] esxcli network vswitch standard portgroup list
Name                Virtual Switch  Active Clients  VLAN ID
------------------  --------------  --------------  -------
Management Network  vSwitch0                     1        0
VL420               vSwitch0                     0        0
VL421               vSwitch-B                    0      421
VL422               vSwitch-B                    0      422
VL423               vSwitch-B                    0      423
VLAN-900            vSwitch0                     0        0

It’s there but despite its name there is no VL 900 assigned. Let’s fix that.

Assign VLAN 900 to port group “VLAN-900”

Command

esxcfg-vswitch -v 900 -p VLAN-900 vSwitch0

Example

1
2
[root@vrtx-25-esxi-n1:~] esxcfg-vswitch -v 900 -p VLAN-900 vSwitch0
[root@vrtx-25-esxi-n1:~] 

Verifying the VLAN is now assigned

1
2
3
4
5
6
7
8
9
[root@vrtx-25-esxi-n1:~] esxcli network vswitch standard portgroup list
Name                Virtual Switch  Active Clients  VLAN ID
------------------  --------------  --------------  -------
Management Network  vSwitch0                     1        0
VL420               vSwitch0                     0        0
VL421               vSwitch-B                    0      421
VL422               vSwitch-B                    0      422
VL423               vSwitch-B                    0      423
VLAN-900            vSwitch0                     0      900

There it is.

Delete port group VLAN-900 from vSwitch0

Command

esxcfg-vswitch --del-pg=VLAN-900 vSwitch0

Example

1
2
[root@vrtx-25-esxi-n1:~] esxcfg-vswitch --del-pg=VLAN-900 vSwitch0
[root@vrtx-25-esxi-n1:~] 

Verifying the PG has been removed

1
2
3
4
5
6
7
8
9
[root@vrtx-25-esxi-n1:~] esxcli network vswitch standard portgroup list
Name                Virtual Switch  Active Clients  VLAN ID
------------------  --------------  --------------  -------
Management Network  vSwitch0                     1        0
VL420               vSwitch0                     0        0
VL421               vSwitch-B                    0      421
VL422               vSwitch-B                    0      422
VL423               vSwitch-B                    0      423
[root@vrtx-25-esxi-n1:~] 
This post is licensed under CC BY 4.0 by the author.