ESXCLI Commands

useful esxcli commands

4 August 2017   4 min read

Useful commands for managing VMs and vSwitches from ESX console. If the command has been executed successfully there will be no output returned to screen (no output = success!!!).

If a command is not complete and you press enter it will return all the options available (equivalent of ? on Cisco).

esxcli                                                         See all options
esxcli network ip               See ip options

Networking

Interface and vSwitch commands

esxcli network ip interface list                               List vmkernel interfaces and their config
esxcli network nic list                        Display listing of physical adapters and link state
esxcli network ip interface ipv4 get See list of VMK IPs

esxcli network vswitch standard portgroup list          List port groups
esxcli network vswitch standard list          List current vswitch config
esxcli network vswitch dvs vmware list     List Distributed Switch config
esxcfg-vswitch -l                                    Check the uplinks and VM interfaces on a DvS

Add or remove network cards (vmnics) to or from a DvS:

esxcli network vswitch dvs vmware list                         List Distributed Switch config
esxcfg-vswitch -Q vmnic -V dvPort_ID_of_vmnic dvSwitch    Unlink/remove a DvS uplink
esxcfg-vswitch -P vmnic -V unused_dvPort_ID dvSwitch      Add a DvS uplink

Create vSwitch and add NIC and remove a Port-group

esxcli network vswitch standard list
esxcli network vswitch standard add vswitch-name=vSwitch1
esxcli network vswitch standard uplink add uplink-name=vmnic32 vswitch-name=vSwitch

Create port-group and assign a VLAN

esxcli network vswitch standard portgroup list
esxcli network vswitch standard portgroup add -p pg_name -v vSwitch1
esxcli network vswitch standard portgroup set -p pg_name -v 10

To remove port-groups. When I tried deleting the default VM Network port-group would get the error ‘Host did not have any virtual network defined’ when deploying an OVF. Not sure if can delete this or it needs to stay.

esxcli network vswitch standard portgroup remove -p pg_name -v vSwitch0

Create vmkernal interface and give it IP details

esxcli network ip interface list
esxcli network ip interface add interface-name=vmk1 portgroup-name=mgmt
esxcli network ip interface ipv4 set interface-name=vmk1 ipv4=10.10.10.55 netmask=255.255.255.0 type=static
esxcli network ip interface set -e true -i vmk1                     To enable the interface

Add a default route or static route

esxcli network ip route ipv4 list
esxcli network ip route ipv4 add -g gateway -n default
esxcli network ip route ipv4 add -g gateway -n network/mask

Virtual Machines

Move Datastore and register the VMs. When discovering a DS it will show in the directory /vmfs/volumes], it can take 5 minutes to populate. For the VMs to appear in the GUI inventory they each need to be re-registered (.vmx file).

esxcli scsi adapter list                                               Check the iSCSI adapter name, usually vmhba3x
esxcli iscsi adapter discovery rediscover -A vmhba34           Discover the DS

vim-cmd vmsvc/register /vmfs/volumes/ds2/vm_name/vm_name.vmx         To re-register a VM, reference the VMs .vmx file
vim-cmd vmsvc/getallvms     List all registered VMs
vim-cmd vmsvc/unregister /vmfs/volumes/ds2/vm_name/vm_name.vmx To un-register a VM

Powering on, off or restarting VMs. First need to get the VMID and then that is referenced in the vim-cmd.

vim-cmd vmsvc/getallvms                                                List all VMIDs
vim-cmd vmsvc/getallvms | grep vm name                  Get a specific VMs VMID
esxcli vm process list                                                List all powered on VMs
vim-cmd vmsvc/power.getstate vm_name                   Check the power state of the VM

vim-cmd vmsvc/power.on vmid                                       Power on a vm
vim-cmd vmsvc/reload Vmid                                            Restart a vm

THis can also be done using esxcli, but rather than using the VMID need the ‘World ID’. soft is the most graceful, hard performs an immediate shutdown and force should only be used as a last resort.

esxcli vm process list | grep -C5 vm_name                              Find the World ID of the VM
esxcli vm process kill -t soft or hard or force -w WorldNumber    Power on or off the VM

Change HW version. Need to unregister it, change the version in the .vmx file and re-register it.

vim-cmd vmsvc/getallvms                                                List all registered VMs
vim-cmd vmsvc/unregister /vmfs/volumes/ds2/vm_name/vm_name.vmx unregister vm
vi /vmfs/volumes/ds2/vm_name/vm_name.vmx Edit the 'virtualHW.version' dictionary
vim-cmd solo/registervm /vmfs/volumes/ds2/vm_name/vm_name.vmx

Some other useful vim-cmd commands for editing VMs.

Other

Globally enable or disable the ESX firewall

esxcli network firewall set -e false
esxcli network firewall set -e true

Hardrive/ Storage

esxcli storage filesystem list                                       List all mounted volumes, can unmount/ mount here
ls -lha /vmfs/devices/disks/                See all hdds & partitions
esxcli storage nmp device list           List all hdds