Practically every intro book will show you how to poke around the GUI to start, stop, and generally manage the various different VMs you have running.
This is fine, but not ideal for everything.
Compared to the workflow that tools like Vagrant provide, I tend to find VMware to be far less command-line workflow focused. What I mean by this is: it’s typical that you won’t find many people scripting their VMware workflow… or genuinely taking an absurd amount of pride in manually doing this tedious work. I’ve seen this in the classroom so many times, it hurts.
It hurts because I really like automation. No. I love automation.
So, while I understand Vagrant and VMware are not competing products ( you can even use VMware with Vagrant ) — I wanted to bring that Vagrant-like, scriptable workflow to my VMware experience without even using Vagrant.
Why not, right?
No one ever told me about the
vmrun command. I wish someone had. Because it has quickly become one of my most-used commands. On your specific system, you will need to find where the command is installed to use it.
I’ve opted to make a simple BASH alias ( on macOS, in my case ) to use it without referencing its full path. The official documentation has support for Linux, macOS and Windows on how to find and use the
alias vmrun="/Applications/VMWare\ Fusion.app/Contents/Library/vmrun"
This command allows you to do all sorts of nifty things. Let’s go over just a few functions to get you started!
vmruncommands require VMware tools or
open-vm-toolsto be installed on the VM. This may not be ideal for certain situations, but doesn’t hurt in many others. Others commands may require a username/password to interact with the VM.
Starting a VM (without a GUI)
To simply start a VM you’ve provisioned through the GUI. All you will need to do is point to the
.vmx file that exists in the VM’s directory contents. You’ll find this nestled along a bunch of other
.vmxf files wherever you saved your VM. To disable the GUI, simple add the
nogui option on the same line.
vmrun start /path/to/example.vmx nogui
Listing Running VMs
To a get list of the paths to the currently running VMs, you can use the
list command option. When there are no VMs running, it’ll also let you know. This is a simple, awesome command and helpful for wrangling a bunch of VMs that are running without a GUI to stop’em.
Check VM Tools
Maybe you don’t know if you’ve installed something like
open-vm-tools and don’t want to use
yum or whatever on the VM to check it. The
checkToolsState command is what you want.
vmrun checkToolsState /path/to/example.vmx
When the VM tools are installed on the box, you’ll get a little message “
installed” printed to STDOUT that lets you know you’re good to go.
What kind of things can we easily do with the VM when these tools are installed? Seriously, just go read the documentation.
List Running Processes
You can lists all processes running in the VM with the
listProcessesInGuest option. This requires VM tools and a valid guest login on the target VM. The login can be passed at the command-line or you can interactively type in the parameters as they are required at the command-line very simply.
vmrun listProcessesInGuest /path/to/example.vmx
When prompted, type in your username and password. With valid credentials, this command will give you simple output with is formatted like so, separated by newlines for each process:
pid = 31337, owner=user, cmd=/usr/bin/vmtoolsd
What’s the IP Address?
You’ve got a VM running. Cool, but, you don’t know the IP address because it was dynamically assigned with something like DHCP. Moreover, you’ve started the box without a GUI, because you’re now a gangster that can do that — so you can’t just login with the VMware graphical terminal you would normally use to run
getGuestIPAddress command is what you need to quickly do this.
vmrun getGuestIPAddress /path/to/example.vmx
This’ll spit back out the first IP address available on that box, or will error out on you if you don’t have VM tools installed. To ensure this is the IP address of the interface you can SSH into, for example, you’ll want to setup the first interface on that box to be a shared network interface between you guest and host systems. This is just one way to do this.
Stopping the VM
We’ve done all the work we needed to do for the day on our VM and we’re ready to shut it down. Remember the
start command? It’s very similar, but
stop instead. You won’t need to specify if the VM started with or without a GUI to keep it simple.
vmrun stop /path/to/example.vmx
I recommend looking into VMware’s products some more to learn all about its (sometimes less documented) command-line tools which you can use to make your lives easier and, of course, automated!
Until next time, that’s all folks!