Standard ESX networking tasks from command line (Part 2)

In part 1 I covered some NIC operations from command line. In part 2 I will cover some standard virtual switch tasks like adding and deleting a virtual switch and making and configuring PortGroups. So here goes for virtual switch operations…

Listing all virtual switches

esxcfg-vswitch -l

This commands gives you a list of all the configured virtual switches with their PortGroups and connected uplinks. Further more a lot of properties are shown about the vSwitches and PortGroups. For vSwitches it shows among other things the name, the uplinks, the number of used ports and the number of configured ports. For PortGroups it shows the PortGroup name, VLAN ID and uplinks.

Add a virtual switch called ‘TestSwitch1’

It’s really simple to add a virtual switch to an ESX server. You simply use the following command:

esxcfg-vswitch -a TestSwitch1

This creates a virtual switch with the name ‘TestSwitch1’. It still has no PortGroups and it has been set with the default amount of configured ports (64). To see all the properties use the command provided earlier to list the virtual switches. If you want to specify the number of configured ports you can use the following command:

esxcfg-vswitch -a TestSwitch1:16

This gives you a virtual switch named ‘TestSwitch1’ with 16 configured ports.

Add a PortGroup to a virtual switch called ‘TestPortGroup1’

Now we want to add a PortGroup to a virtual switch. The following command adds a PortGroup called ‘TestPortGroup1’ to our previously created virtual switch:

esxcfg-vswitch -A TestPortGroup1 TestSwitch1

This will create a PortGroup with VLAN ID 0. Notice that this time we used ‘-A’ to add the PortGroup since ‘-a’ is used for adding virtual switches. When we want to set the VLAN ID of the PortGroup we have to issue a second command. This command will set the VLAN ID of the PortGroup we just created to VLAN ID 2. The parameter ‘-p’ defines the PortGroup and ‘-v’ defines the VLAN ID you want to set it to.

esxcfg-vswitch -p TestPortGroup1 -v 2 TestSwitch1

Add an uplinkto the virtual switch and PortGroup

So now we got a virtual switch and a PortGroup. I guess we would like some connection to the outside world. So when we want to bind a physical NIC to the created PortGroup the thing to do is link the pNIC to the virtual switch and after that we automatically have the link to the PortGroup. First you need to find out what pNIC you want to bind to the virtual switch. You can check the names of the pNIC’s with the command esxcfg-nics -l, which I discussed in part 1. Now that we know the name we can bind it to the virtual switch. With the following command I will bind ‘vmnic4’ to the created virtual switch:

esxcfg-vswitch -L vmnic4 TestSwitch1

Notice that the l is a capital L, the normal l is already used for showing the list. Now we have configured the virtual switch correctly and we have a fully functional virtual switch with a virtual machine port group.

Remove a link from the PortGroup and virtual switch

Well first lets undo the links we just binded to the PortGroupand the virtual switch. It’s al pretty straightforward from here. If you managed to make the links it will be just as easy to undo them. The command for disconnecting the pNIC from the virtual switch is:

esxcfg-vswitch -U vmnic4 TestSwitch1

This will unlink ‘vmnic4’ from the virtual switch and the PortGroup. Notice that the ‘-U’ parameter is with a capital U just like the ‘-L’ for linking the pNIC.

Remove a PortGroup from the virtual switch

Removing the PortGroup is really simple. You just take the command you used to create the PortGroup, but instead of the ‘-A’ parameter you use the ‘-D” parameter (all capital). So the command to accomplish a deletion of the PortGroup ‘TestPortGroup1’ from virtual switch ‘TestSwitch1’ is:

esxcfg-vswitch -D TestPortGroup1 TestSwitch1

Now the virtual switch should be empty (if you didn’t do anything else to the virtual switch). There should be no PortGroups and no connected uplinks.

Removing a virtual switch

Now to set everything we have done back to the original state we have to delete the virtual switch we just made. Again this is really simple if you alter  the create command. Just replace the ‘-a’ parameter with ‘-d’. The same as with the PortGroup only this time no capital characters.

esxcfg-vswitch -d TestSwitch1

And now if you list all the virtual switches again this should give you the same picture as at the beginning.

Ofcourse there are lots of things more you can do from command line networking related, but I thought this was the most basic and standard stuff you would want to do. Scott Lowe wrote some articles about more advanced operations like Setting Load Balancing Policies and Modifying a PortGroup using the CLI. I hope this articles were useful for all of you. At least it has given me a nice reference for the future.


2 responses to “Standard ESX networking tasks from command line (Part 2)

  1. Very nice, and cleanly explained. This will help at one of my customers that is moving to ESX 5.0 and Vcenter 5.0.


  2. What about moving a virtual machine network from one portgroup, to another, in the same or other vSwitch via cli?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s