Managing Vsphere From The Linux Command Line

For the past year I’ve been working quite a bit with VMWare’s vSphere virtualization suite. Managing vSphere through the web UI has been extremely frustrating though. The bugs, constant crashes and sluggishness have really impeded my ability to be productive. Being a command line user who loves to automate I decided to test out the govc command line utility to see if this would improve my experience. After a good deal of use I can affirmatively say that this gem makes a HUGE difference in productivity!

To get going with govc all you need is a Linux machine. The govc binary is opensource and the build team provides binary builds on their release page. Installing it is easy:

$ wget

$ gunzip govc_linux_amd64.gz && mv govc_linux_amd64 /usr/local/bin/govc && chmod 755 /usr/local/bin/govc

If you want to live on the cutting edge you can also build govc from source:

$ mkdir govc

$ export GOPATH=/home/matty/govc

$ cd govc

$ go get -u

$ cp bin/govc /usr/local/bin/govc && chmod 755 /usr/local/bin/govc

Once installed you can verify the binary works by running it with the about command:

$ govc about

Name:         VMware vCenter Server
Vendor:       VMware, Inc.
Version:      6.5.0
Build:        7119157
OS type:      linux-x64
API type:     VirtualCenter
API version:  6.5
Product ID:   vpx
UUID:         93c8b2a5-3715-4a27-8a3a-4758848ddce0

To list all of the commands govc can be run without an argument:

$ govc

Usage of govc:

To connect govc to a vCenter instance you will need to export a couple of variables (if you specify a root CA certificate bundle with the GOVC_TLS_CA_CERTS option you can leave out the first export):

$ export GOVC_INSECURE=true

$ export GOVC_URL=''


To see the true utility of govc let me show a couple of examples. Govc takes a command as the first argument followed by one or more options. The following examples show how to list all of your vCenter objects and retrieve information about your data center:

$ govc ls


$ govc

Name:                DC
  Path:              /DC
  Hosts:             2
  Clusters:          1
  Virtual Machines:  2
  Networks:          7
  Datastores:        6

To view all of your data stores you can use the command:

$ govc

Name:        ds01
  Path:      /DC/datastore/ds01
  Type:      VMFS
  URL:       ds:///vmfs/volumes/586a9fb0-4b44f5b6-942c-001b21b97588/
  Capacity:  225.2 GB
  Free:      224.3 GB

To find an object you can use the find command (this is super handy):

$ govc find vm -name *kub*


To get information about an ESXi host you can pass the name of the host to the command:

$ govc

Name:              esx01
  Path:            /DC/host/ProdCluster/esx01
  Manufacturer:    MSI
  Logical CPUs:    4 CPUs @ 3192MHz
  Processor type:  Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz
  CPU usage:       182 MHz (1.4%)
  Memory:          65411MB
  Memory usage:    14433 MB (22.1%)
  Boot time:       2017-12-19 13:23:03.394348 +0000 UTC
  State:           connected

To retrieve all of the information about a VM you can use the command:

$ govc kub1

Name:           kub1
  Path:         /DC/vm/kub1
  UUID:         420fd911-0f20-0898-34ed-2b055519a54e
  Guest name:   Red Hat Enterprise Linux 7 (64-bit)
  Memory:       4096MB
  CPU:          1 vCPU(s)
  Power state:  poweredOn
  Boot time:    2017-12-19 13:19:08.926664 +0000 UTC
  IP address:
  Host:         esx02

Govc also provides access to the host and VM metrics. You can get a list of metrics for an object with the command:

$ govc vm/kub1


To collect a sample you can use the metric.sample command:

$ while :; do govc metric.sample vm/kub1 cpu.usagemhz.average; sleep 1; done

kub1  -  cpu.usagemhz.average    1777,17,8,17,11,9  MHz
kub1  0  cpu.usagemhz.average    1774,5,4,4,4,4     MHz
kub1  -  cpu.usagemhz.average    1777,17,8,17,11,9  MHz
kub1  0  cpu.usagemhz.average    1774,5,4,4,4,4     MHz

In addition to the commands listed above I’ve found the following just as useful (you can get a full list of commands and their options on the govc github page):

datastore.upload - upload object to a data store - get DVS port group info
events - view events
folder.create - create a folder
host.maintenance.enter - put a host into maintenance mode
host.maintenance.exit - take a host out of maintenance mode
logs - view specific logs
snapshot.create - create a snapshot
snapshot.remove - delete a snapshot
tasks - view vCenter tasks
vm.power - power a VM on and off

Huge thanks to the govc team as well as the maintainers of the govmomi library!

This article was posted by Matty on 2017-12-19 09:35:40 -0500 EST