There are a number of reasons why you might want to create vSwitches via the command line on a VMware ESXi host. Creating a script to setup vSwitches (and other networking settings) in the command line can be useful when rolling out many ESXi hosts or creating a fast recovery plan for DR.
To create vSwitches in the command line, name them, allocate VLANs and physical NICs to those vSwitches, do the following:
- Connect to the ESXi host you want to setup the networking for. You can do this using Putty over SSH
- Log in to the VMware ESXi server
- Run the following command to create a new vSwitch:
esxcfg-vswitch -a <vSwitch Name>
- Run the following command to add a new portgroup to the vSwitch:
esxcfg-vswitch -A “<Portgroup Name>” <vSwitch Name>
- Run the following command to add a VLAN to the portgroup:
esxcfg-vswitch -v <VLAN Number> -p “<Portgroup Name>” <vSwitch Name>
- Run the following command to restart VMware’s services:
If you have the network settings page open in the vSphere Client on the ESXi host you just ran the script against, you will have to navigate to another page and return to the networking tab to see the newly added vSwitch and associated network settings. If the vSwitch you added in the command line still isn’t showing, you may have to hit the refresh button in the vSphere Client.