Networknet archive Blog

ESX: Create port groups and assign VLANs with esxcfg-vswitch

Originally published Last updated 2 comments

By Ivan Versluis

Creating five or more virtual port groups through the VI client can mean repeating the same work. If you prefer the command line, sign in to the Service Console and use the following commands.

Create a virtual port group:

Code1 line
1
esxcfg-vswitch -A NAME-TO-GIVE vSwitch0

Assign a specific VLAN to the port group:

Code1 line
1
esxcfg-vswitch -v VLANID -p PORTGROUP vSwitch0

With these two commands in a shell script, I created the same networking configuration on a dozen ESX servers:

Code12 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[root@ESX-02 root]# esxcfg-vswitch -A VLAN282 vSwitch0
[root@ESX-02 root]# esxcfg-vswitch -A VLAN283 vSwitch0
[root@ESX-02 root]# esxcfg-vswitch -A VLAN284 vSwitch0
[root@ESX-02 root]# esxcfg-vswitch -A VLAN285 vSwitch0
[root@ESX-02 root]# esxcfg-vswitch -A VLAN299 vSwitch0
[root@ESX-02 root]# esxcfg-vswitch -A VLAN220 vSwitch0
[root@ESX-02 root]# esxcfg-vswitch -v 282 -p VLAN282 vSwitch0
[root@ESX-02 root]# esxcfg-vswitch -v 283 -p VLAN283 vSwitch0
[root@ESX-02 root]# esxcfg-vswitch -v 284 -p VLAN284 vSwitch0
[root@ESX-02 root]# esxcfg-vswitch -v 285 -p VLAN285 vSwitch0
[root@ESX-02 root]# esxcfg-vswitch -v 299 -p VLAN299 vSwitch0
[root@ESX-02 root]# esxcfg-vswitch -v 220 -p VLAN220 vSwitch0

Command reference

Code25 lines
 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
esxcfg-vswitch [options] [vswitch[:ports]]
  -a|--add              Add a new virtual switch.
  -d|--delete           Delete the virtual switch.
  -l|--list             List all the virtual switches.
  -L|--link=pnic        Set pnic as an uplink for the vswitch.
  -U|--unlink=pnic      Remove pnic from the uplinks for the vswitch.
  -M|--add-pg-uplink    Add an uplink to the list of uplinks for a portgroup
  -N|--del-pg-uplink    Delete an uplink from the list of uplinks for a portgroup
  -p|--pg=portgroup     Specify a portgroup for operation
                        Use ALL to set VLAN IDs on all portgroups
  -v|--vlan=id          Set vlan id for portgroup specified by -p
                        0 would disable the vlan
  -c|--check            Check to see if a virtual switch exists.
                        Program outputs a 1 if it exists, 0 otherwise.
  -A|--add-pg=name      Add a new portgroup to the virtual switch.
  -D|--del-pg=name      Delete the portgroup from the virtual switch.
  -C|--check-pg=name    Check to see if a portgroup exists.  Program
                        outputs a 1 if it exists, 0 otherwise.
  -B|--set-cdp          Set the CDP status for a given virtual switch.
                        To set pass one of "down", "listen", "advertise", "both".
  -b|--get-cdp          Print the current CDP setting for this switch.
  -m|--mtu=MTU          Set MTU for the vswitch. 
  -r|--restore          Restore all virtual switches from the configuration file
                        (FOR INTERNAL USE ONLY).
  -h|--help             Show this message.

2 comments

Comments from the original WordPress site, kept read-only.

  1. Celia

    My OS is ESXi 5.5 and is using web client to mange the host. I can list out the new portgroup by "esxcfg-vswitch -l" but I can't see the new port group from web client. I refresh the web client several times and it still get nothing. Please help.