[TUTORIAL] Wake On LAN (WOL) for VMs and Containers

EpicLPer

Member
Sep 7, 2022
47
6
8
29
Austria
epiclper.com
I've been searching around for a solution to use Wake-On-LAN (WOL) for VMs and Containers in Proxmox, in my case this makes a Windows 11 VM I use when out and about via TeamViewer very easy to boot and doesn't require waking up via Home Assistant first :)

All of the following resource are taken from THIS THREAD talking about WOL, individual resources and credits are given below.



Create Wake-On-LAN script​

1. Click on your Proxmox Node and go to "Shell"​
2. Type in cd ~ followed by mkdir scripts, then navigate into that folder with cd scripts
3. Next type in nano wol_hack.sh and paste the following script into that file (credit goes to @henr0 - Source):​
Bash:
#!/bin/bash

# Attempts to start Proxmox VM or LXC that matches MAC address received on WOL message
# This could be dangerous if abused by spamming the interface with many packages
# so I would like to try no more than once per 5 seconds.
# In my case useful with Moonlight client
# Uses tcpdump on default proxmox interface, change the interface if needed.
while true; do
  sleep 5
  wake_mac=$(tcpdump -c 1 -UlnXi vmbr1 ether proto 0x0842 or udp port 9 2>/dev/null |\
  sed -nE 's/^.*20:  (ffff|.... ....) (..)(..) (..)(..) (..)(..).*$/\2:\3:\4:\5:\6:\7/p')
  echo "Captured magic packet for address: \"${wake_mac}\""
  echo -n "Looking for existing VM: "
  matches=($(grep -il ${wake_mac} /etc/pve/qemu-server/*))
  if [[ ${#matches[*]} -eq 0 ]]; then
    echo "${#matches[*]} found"
  echo -n "Looking for existing LXC: "
  matches=($(grep -il ${wake_mac} /etc/pve/lxc/*))
  if [[ ${#matches[*]} -eq 0 ]]; then
    echo "${#matches[*]} found"
    continue
  elif [[ ${#matches[*]} -gt 1 ]]; then
    echo "${#matches[*]} found, using first found"
  else
    echo "${#matches[*]} found"
  fi
  vm_file=$(basename ${matches[0]})
  vm_id=${vm_file%.*}
  details=$(pct status ${vm_id} -verbose | egrep "^name|^status")
  name=$(echo ${details} | awk '{print $2}')
  status=$(echo ${details} | awk '{print $4}')
  if [[ "${status}" != "stopped" ]]; then
    echo "SKIPPED CONTAINER ${vm_id} : ${name} is ${status}"
  else
    echo "STARTING CONTAINER ${vm_id} : ${name} is ${status}"
    pct start ${vm_id}
  fi
    continue
  elif [[ ${#matches[*]} -gt 1 ]]; then
    echo "${#matches[*]} found, using first found"
  else
    echo "${#matches[*]} found"
  fi
  vm_file=$(basename ${matches[0]})
  vm_id=${vm_file%.*}
  details=$(qm status ${vm_id} -verbose | egrep "^name|^status")
  name=$(echo ${details} | awk '{print $2}')
  status=$(echo ${details} | awk '{print $4}')
  if [[ "${status}" != "stopped" ]]; then
    echo "SKIPPED VM ${vm_id} : ${name} is ${status}"
  else
    echo "STARTING VM ${vm_id} : ${name} is ${status}"
    qm start ${vm_id}
  fi
done
4. Save by pressing Ctrl + X and then Enter
5. Make the script executable via chmod +x wol_hack.sh


Add new auto-start service​

1. Create a new systemd service file with the following command: nano /etc/systemd/system/pve-wakeonlanhack.service
2. Paste the following code into this file (credit goes to @jacobxtyler - Source):​
Code:
[Unit]
Description=Wake-on-LAN for Proxmox Virtual Environments
After=network.target

[Service]
Type=simple
Restart=always
User=root
ExecStart=/root/scripts/wol_hack.sh

[Install]
WantedBy=multi-user.target
3. Save by pressing Ctrl + X and then Enter
4. Enable and Start this service via the following commands:​
systemctl enable pve-wakeonlanhack
systemctl start pve-wakeonlanhack



WOL should now work :) If something doesn't work or if a VM/Container doesn't want to wake up you can check the log output via systemctl status pve-wakeonlanhack and see what it does. A successful boot attempt should look like this:
Code:
wol_hack.sh[2000563]: Captured magic packet for address: "xx:xx:xx:xx:xx:x"
wol_hack.sh[2000563]: Looking for existing VM: 1 found
wol_hack.sh[2000563]: STARTING VM 900 : Win11Desktop is stopped
qm[2001050]: <root@pam> starting task UPID:pve2:XXXXXXXX:XXXXXXXX:XXXXXXXX:qmstart:900:root@pam:
qm[2001051]: start VM 900: UPID:pve2:XXXXXXXX:XXXXXXXX:XXXXXXXX:qmstart:900:root@pam:
wol_hack.sh[2001050]: swtpm_setup: Not overwriting existing state file.
qm[2001050]: <root@pam> end task UPID:pve2:XXXXXXXX:XXXXXXXX:XXXXXXXX:qmstart:900:root@pam: OK



Troubleshooting​

  • error: stdbuf: failed to run command ‘xxd’: No such file or directory
    • (Might not be an issue anymore as the script above got rewritten, but mentioning this just in case)
      Some people have reported this in the original thread which seems to be an issue with older Proxmox installs. Try to install the following package which should resolve this: apt install xxd
  • WOL packets from pfSense aren't working
    • pfSense sends WOL packets on 40000 so change the vmbr1 to vmbr0 (the Proxmox default bridge) and port 9 to 40000 and restart the script. All devices woke up within a second of sending the WOL packet.
 
Last edited:
Tried this with Proxmox 8.1.10 and changed vmbr1 to vmbr0 and saved.
ran ./wol_hack.sh and get a blank response and send wol packet from pfsense.
nothing happens.

created nano /etc/systemd/system/pve-wakeonlanhack.service

tried to start and status shows running but cant get any of the vms to start up
 
Tried this with Proxmox 8.1.10 and changed vmbr1 to vmbr0 and saved.
ran ./wol_hack.sh and get a blank response and send wol packet from pfsense.
nothing happens.

created nano /etc/systemd/system/pve-wakeonlanhack.service

tried to start and status shows running but cant get any of the vms to start up
Heya, it would probably be better to report this issue or ask questions over at the original thread the script came from: https://forum.proxmox.com/threads/wake-on-lan-for-vm.84355/
The people there can most likely help you better with this issue.
 
If anyone comes searching here and finds this like I did. pfSense send WOL packets on 40000 so change the vmbr1 to vmbr0 (the proxmox default bridge) and port 9 to 40000 and restart the script. All devices woke up within a second of sending the WOL packet.

Thanks to all who contributed to this.
 
  • Like
Reactions: EpicLPer
If anyone comes searching here and finds this like I did. pfSense send WOL packets on 40000 so change the vmbr1 to vmbr0 (the proxmox default bridge) and port 9 to 40000 and restart the script. All devices woke up within a second of sending the WOL packet.

Thanks to all who contributed to this.
I've added this to the "Troubleshooting" section now! Thanks :)
 
Ive run everything as described and checked but when I run
systemctl enable pve-wakeonlanhack
I get the following error message:
Invalid unit name "pve-wakeonlanhack" escaped as "pve-wakeonlanhack\xe2\x80\x8b" (maybe you should use systemd-escape?).
Failed to enable unit: Unit file pve-wakeonlanhack\xe2\x80\x8b.service does not exist.

What could be the problem of this?
 
this does not work on proxmox 8.1.4

Bash:
root@pve:/etc/systemd/system# systemctl status pve-wakeonlanhack
× pve-wakeonlanhack.service - Wake-on-LAN for Proxmox Virtual Environments
     Loaded: loaded (/etc/systemd/system/pve-wakeonlanhack.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Sun 2024-04-21 11:45:58 CEST; 7s ago
   Duration: 1ms
    Process: 1776497 ExecStart=/root/scripts/wol_hack.sh (code=exited, status=203/EXEC)
   Main PID: 1776497 (code=exited, status=203/EXEC)
        CPU: 1ms

Apr 21 11:45:58 pve systemd[1]: pve-wakeonlanhack.service: Scheduled restart job, restart counter is at 5.
Apr 21 11:45:58 pve systemd[1]: Stopped pve-wakeonlanhack.service - Wake-on-LAN for Proxmox Virtual Environments.
Apr 21 11:45:58 pve systemd[1]: pve-wakeonlanhack.service: Start request repeated too quickly.
Apr 21 11:45:58 pve systemd[1]: pve-wakeonlanhack.service: Failed with result 'exit-code'.
Apr 21 11:45:58 pve systemd[1]: Failed to start pve-wakeonlanhack.service - Wake-on-LAN for Proxmox Virtual Environments.

something is not ok with the service deamon standard settings or why the script does fail?
i've just changed the standard proxmox interface as it is vmbr0 on my settings

Bash:
systemctl daemon-reload

does not help
as well as
Bash:
systemctl reset-failed pve-wakeonlanhack
does not help.

any help is highly welcome!
 
Ive run everything as described and checked but when I run
systemctl enable pve-wakeonlanhack
I get the following error message:
Invalid unit name "pve-wakeonlanhack" escaped as "pve-wakeonlanhack\xe2\x80\x8b" (maybe you should use systemd-escape?).
Failed to enable unit: Unit file pve-wakeonlanhack\xe2\x80\x8b.service does not exist.

What could be the problem of this?

dude you have copyed the command with a space _ behind pve-wakeonlanhack to your bash, therefore it is not executable...
 
  • Like
Reactions: Mauriskes
this does not work on proxmox 8.1.4

Bash:
root@pve:/etc/systemd/system# systemctl status pve-wakeonlanhack
× pve-wakeonlanhack.service - Wake-on-LAN for Proxmox Virtual Environments
     Loaded: loaded (/etc/systemd/system/pve-wakeonlanhack.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Sun 2024-04-21 11:45:58 CEST; 7s ago
   Duration: 1ms
    Process: 1776497 ExecStart=/root/scripts/wol_hack.sh (code=exited, status=203/EXEC)
   Main PID: 1776497 (code=exited, status=203/EXEC)
        CPU: 1ms

Apr 21 11:45:58 pve systemd[1]: pve-wakeonlanhack.service: Scheduled restart job, restart counter is at 5.
Apr 21 11:45:58 pve systemd[1]: Stopped pve-wakeonlanhack.service - Wake-on-LAN for Proxmox Virtual Environments.
Apr 21 11:45:58 pve systemd[1]: pve-wakeonlanhack.service: Start request repeated too quickly.
Apr 21 11:45:58 pve systemd[1]: pve-wakeonlanhack.service: Failed with result 'exit-code'.
Apr 21 11:45:58 pve systemd[1]: Failed to start pve-wakeonlanhack.service - Wake-on-LAN for Proxmox Virtual Environments.

something is not ok with the service deamon standard settings or why the script does fail?
i've just changed the standard proxmox interface as it is vmbr0 on my settings

Bash:
systemctl daemon-reload

does not help
as well as
Bash:
systemctl reset-failed pve-wakeonlanhack
does not help.

any help is highly welcome!
solved,

easy stupid misstake: my script wol_hack.sh had a blank space at the end of the name. recreated the file, chmod +x and systemctl reset-failed pve-wakeonlanhack and systemctl restart pve-wakeonlanhack does work

Bash:
Apr 21 12:00:28 pve systemd[1]: Started pve-wakeonlanhack.service - Wake-on-LAN for Proxmox Virtual Environments.
root@pve:~/scripts# systemctl status pve-wakeonlanhack
● pve-wakeonlanhack.service - Wake-on-LAN for Proxmox Virtual Environments
     Loaded: loaded (/etc/systemd/system/pve-wakeonlanhack.service; enabled; preset: enabled)
     Active: active (running) since Sun 2024-04-21 12:00:28 CEST; 13s ago
   Main PID: 1787142 (wol_hack.sh)
      Tasks: 3 (limit: 76945)
     Memory: 127.8M
        CPU: 1.093s
     CGroup: /system.slice/pve-wakeonlanhack.service
             ├─1787142 /bin/bash /root/scripts/wol_hack.sh
             ├─1787299 /usr/bin/perl /usr/sbin/qm start 2xx
             └─1787300 "task UPID:pve:xxxxxxxxx:qmstart:2xx:root@pam:"

Apr 21 12:00:28 pve systemd[1]: Started pve-wakeonlanhack.service - Wake-on-LAN for Proxmox Virtual Environments.
Apr 21 12:00:40 pve wol_hack.sh[1787142]: Captured magic packet for address: "xxxxxxxxx"
Apr 21 12:00:40 pve wol_hack.sh[1787142]: Looking for existing VM: 1 found
Apr 21 12:00:40 pve wol_hack.sh[1787142]: STARTING VM 2xx : Truenas-Scale is stopped
Apr 21 12:00:41 pve qm[1787299]: <root@pam> starting task UPID:pve:xxxxxxxxx:qmstart:2xx:root@pam:
Apr 21 12:00:41 pve qm[1787300]: start VM 2xx: UPID:pve:xxxxxx:qmstart:2xx:root@pam:

truenas-scale wakes up directly. awesome work! thank you for this tutorial and the community effort!
 
dude you have copyed the command with a space _ behind pve-wakeonlanhack to your bash, therefore it is not executable...
I'm sorry I'm pretty new, I can't find what you mentioned. Can you explain where I can find this?

Edit:
NVM.. I already found it..
 
Last edited:

EpicLPer

Troubleshooting​

  • error: stdbuf: failed to run command ‘xxd’: No such file or directory
    • (Might not be an issue anymore as the script above got rewritten, but mentioning this just in case)
      Some people have reported this in the original thread which seems to be an issue with older Proxmox installs. Try to install the following package which should resolve this: apt install xxd
  • WOL packets from pfSense aren't working
    • pfSense sends WOL packets on 40000 so change the vmbr1 to vmbr0 (the Proxmox default bridge) and port 9 to 40000 and restart the script. All devices woke up within a second of sending the WOL packet.

In Pfsense: the Mac addresses are lower case, have you tested it with the vm mac addresses to lowercase in pve?
 
Last edited:
Ive run everything as described and checked but when I run
systemctl enable pve-wakeonlanhack
I get the following error message:
Invalid unit name "pve-wakeonlanhack" escaped as "pve-wakeonlanhack\xe2\x80\x8b" (maybe you should use systemd-escape?).
Failed to enable unit: Unit file pve-wakeonlanhack\xe2\x80\x8b.service does not exist.

What could be the problem of this?
you cut and pasted from this page, its taken some stray characters :D
 
Hello,

This is not working for me.....
I Don't know what i am doing wrong.....

The one time i get the error:

Invalid unit name "pve-wakeonlanhack" escaped as "pve-wakeonlanhack\xe2\x80\x8b" (maybe you should use systemd-escape?).
Failed to enable unit: Unit file pve-wakeonlanhack\xe2\x80\x8b.service does not exist.


The other time i get:

Apr 21 11:45:58 pve systemd[1]: pve-wakeonlanhack.service: Scheduled restart job, restart counter is at 5.
Apr 21 11:45:58 pve systemd[1]: Stopped pve-wakeonlanhack.service - Wake-on-LAN for Proxmox Virtual Environments.
Apr 21 11:45:58 pve systemd[1]: pve-wakeonlanhack.service: Start request repeated too quickly.
Apr 21 11:45:58 pve systemd[1]: pve-wakeonlanhack.service: Failed with result 'exit-code'.
Apr 21 11:45:58 pve systemd[1]: Failed to start pve-wakeonlanhack.service - Wake-on-LAN for Proxmox Virtual Environments.

I typed everything out and not did copy-paste and still it's not working.
Can you explain this a little bit more or make a video....?

Thanks
 
This might be a zombie thread, but wanted to see if anyone has had this issue:

I have the script and service up and running, but it is capturing a packet without an actual MAC address being returned...or so the systemctl status reports...

Code:
Sep 12 09:13:14 prox2 wol_hack.sh[1592114]: Captured magic packet for address: ""
Sep 12 09:13:14 prox2 wol_hack.sh[1592114]: Looking for existing VM: 0 found
Sep 12 09:13:14 prox2 wol_hack.sh[1592114]: Looking for existing LXC: 0 found

Any ideas?
 
This might be a zombie thread, but wanted to see if anyone has had this issue:

I have the script and service up and running, but it is capturing a packet without an actual MAC address being returned...or so the systemctl status reports...

Code:
Sep 12 09:13:14 prox2 wol_hack.sh[1592114]: Captured magic packet for address: ""
Sep 12 09:13:14 prox2 wol_hack.sh[1592114]: Looking for existing VM: 0 found
Sep 12 09:13:14 prox2 wol_hack.sh[1592114]: Looking for existing LXC: 0 found

Any ideas?
Seems like copy/pasta fail on my part...working now.
 
My proxmox node has a dedicated interface on vmbr0 and all my vms and containers use vmbr2. ive tried wol_hack.sh with both interfaces and it wont capture incoming magic packets from any client (moonlight in my case). no error messages either. having said that my pfsense instance (on the same node) can send wol packets successfully to vms and wakes them up in 2 secs.

any ideas?
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!