Preface
So I have a small PC running Ubuntu Server. I also dual boot into Windows sometimes. I have a single monitor which is also attached to the Macbook Pro. I can’t use my Logitech keyboard and mouse during boot since I connected them to Windows via Bluetooth. The usual flow is: power on, change monitor input, grab another keyboard to choose from grub menu and boot Windows. Another option is, let Ubuntu boot, SSH into the machine, and run below commands to boot to Windows.
sudo efibootmgr --bootnext 0000
sudo reboot
My Windows id is 0000 but yours could be different. Run
efibootmgr -vto learn your id
It’s not a big deal but it’s annoying. Then I had a dumb idea: what if plugging in my headphone before booting could automatically send me to Windows?
Turns out with little bit of fiddling, it works.
Heaphone Status
I didn’t want to install anything extra just to get headphone jack status. I checked the codec state before and after I plugged the heaphone jack.
cat /proc/asound/card*/codec#*
# Headphone UNPLUGGED
Node 0x18 [Pin Complex] wcaps 0x400581: Stereo
Pin-ctls: 0x40: OUT
# Headphone PLUGGED
Node 0x18 [Pin Complex] wcaps 0x400581: Stereo
Pin-ctls: 0x00:
That Pin-ctls value flips when you plug in a headphone. That’s all I needed.
Why Not GRUB?
My first instinct was to do this at the GRUB level. Sounds cool(pun intended) but impossible for this use case though. Since GRUB can detect block devices, you can use USB stick etc for this purpose but for my use case it was unsound(oh I did it again.)
How It Works
The full codec dump lives at /proc/asound/card*/codec#*. After some digging I found that Node 0x18 is the headphone jack on this hardware, and the Pin-ctls line tells you its state:
0x40: OUT→ nothing plugged in0x00:→ headphone is connected
So the detection script is basically just a grep:
#!/bin/bash
CODEC=$(grep -l "Node 0x18" /proc/asound/card*/codec#* 2>/dev/null | head -1)
if [ -z "$CODEC" ]; then
logger "headphone-boot-check: codec not found, skipping"
exit 0
fi
PIN_CTL=$(awk '/Node 0x18/,/Node 0x19/' "$CODEC" | grep "Pin-ctls")
if echo "$PIN_CTL" | grep -q "0x00:$"; then
logger "headphone-boot-check: headphone PLUGGED, rebooting to Windows"
efibootmgr --bootnext 0000
reboot
else
logger "headphone-boot-check: headphone unplugged, booting normally"
fi
efibootmgr --bootnext 0000 sets the next boot entry to Windows without permanently changing the boot order. So if you do nothing, Linux is the default. Plug in headphone → next boot goes to Windows, then it resets back to Linux automatically.
Wiring It Into Boot
Drop the script at /usr/local/bin/headphone-boot-check.sh, make it executable, then create a systemd service:
# /etc/systemd/system/headphone-boot-check.service
[Unit]
Description=Headphone boot selector
After=sound.target systemd-modules-load.service
DefaultDependencies=no
[Service]
Type=oneshot
ExecStart=/usr/local/bin/headphone-boot-check.sh
[Install]
WantedBy=sysinit.target
sudo systemctl daemon-reload
sudo systemctl enable headphone-boot-check.service
That’s it. The service runs as root so you don’t need sudo inside the script.
Notes:
The node number might be different on your machine. Node 0x18 is specific to this codec on my machine. To find yours, dump the codec and look for the pin marked as headphone:
grep -i "headphone\|jack\|pin complex" /proc/asound/card*/codec#*
Check your Windows boot entry number. 0000 might not be Windows on your machine. Run efibootmgr -v and look for Windows Boot Manager.
Does This Work on Other Hardware?
Maybe. The codec state approach works on any machine with HD Audio. But:
- The node number will likely be different
- The
Pin-ctlsvalues might differ depending on the codec chip
Just dump /proc/asound/card*/codec#*, plug and unplug a headphone, and diff the output.
What I Actually Use This For
My setup: Ubuntu Server headless most of the time, Windows occasionally for gaming. Instead of keeping a keyboard plugged in just to hit an arrow key at GRUB, I just grab my headphones off the desk and plug them into the PC before pressing the power button. Machine boots into Windows. When I’m done and reboot back, headphones go back on the desk, machine comes back to Linux.
It’s a small thing. But the best hacks usually are.