Ssh – How to shutdown a virtual machine on ESXi5 over an ssh terminal session

command-line-interfaceremote-accesssshvmware-esxi

How can I shutdown a virtual machine when I am on the commandline of an ESXi5 host over ssh?


Sidenote:

To enable scripts on other machines to login to the ESXi without password, add the public key of the user on the other machine to the ESXi's "/etc/ssh/keys-root/authorized_keys" if you want to login as root. If you want to login as user x add the key to "/etc/ssh/keys-x/authorized_keys".

Best Answer

From the section 'Using the ESXi 5.0 esxcli command to power off a virtual machine' in KB1014165 at the VMWare Knowledge base:

The esxcli command can be used locally or remotely to power off a virtual machine running on ESXi 5.0. For more information, see the esxcli vm Commands section of the vSphere Command-Line Interface Reference.

Open a console session where the esxcli tool is available, either in the ESXi Shell, the vSphere Management Assistant (vMA), or the location where the vSphere Command-Line Interface (vCLI) is installed.

Get a list of running virtual machines, identified by World ID, UUID, Display Name and path to the .vmx configuration file, using the command:

esxcli vm process list

Power off one of the virtual machines from the list using the command:

esxcli vm process kill --type=[soft,hard,force] --world-id=WorldNumber

Note: Three power-off methods are available. Soft is the most graceful, hard performs an immediate shutdown, and force should be used as a last resort.

Repeat step 2 and validate that the virtual machine is no longer running.

I hope this helps :)