3

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".

mit
  • 1,914
  • 6
  • 29
  • 42

2 Answers2

8

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 :)

Kvisle
  • 4,193
  • 24
  • 25
  • Brilliant, saves the need for an additional windows machine and a lot of time in many occasions. Especially when there are shell scripts prepared to start or shutdown some of the machines we need often. – mit Oct 16 '11 at 13:46
  • and my question is how turn on , or restart that machine or another online machin, with ssh ? .. but this is also very useful for me ... thanks – saber tabatabaee yazdi Apr 20 '13 at 13:43
  • Explained in great detail here: http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1038043 – Kvisle Apr 20 '13 at 13:53
  • For some reason i get `Error: Unknown command or namespace vm process kill –t=soft –w=67909` when trying this on 6.5 embedded host even though `esxcli vm process list` works and `esxcli vm process kill -help` confirms i'm running the correct command for my system. I'm getting closer to switching to proxmox, xen or citrix for basic systems that don't benefit from enterprise licence – jtlindsey Apr 18 '21 at 02:14
2

You can use vim-cmd:

Here is an example (shutdown only one VM: SRV1):

[root@ESX1:~] vim-cmd vmsvc/getallvms
Vmid   Name          File               Guest OS       Version  
1      SRV2   [NAS] SRV2/SRV2.vmx   winLonghornGuest   vmx-08
2      SRV1   [SAN] SRV1/SRV1.vmx   winLonghornGuest   vmx-08

[root@ESX1:~]  vim-cmd vmsvc/power.shutdown 2

You can also shutdown all host VMs:

/sbin/shutdown.sh && /sbin/poweroff

Extracted from: http://www.sysadmit.com/2016/07/vmware-esxi-apagar-todas-las-vms-desde-SSH.html

Fodunamers
  • 21
  • 1
  • This worked for me in 6.5 embedded host. Docs on both methods here https://kb.vmware.com/s/article/1004340 – jtlindsey Apr 18 '21 at 02:11