Mute audio device on Linux when wired headphones are unplugged.
cargo install headphone-mute # Install CLI
sudo usermod -aG input "$USER" # Add user to the input group
# log out and log back in so the new group membership is applied
headphone-mute # Test CLIAfter installing, log out and log back in so the new group membership is applied.
If you want a temporary workaround without logging out, run:
newgrp input
headphone-muteVerify that the current session has the required group:
id -nG | tr ' ' '\n' | grep '^input$'If this prints nothing, start a new login session by logging out and back in.
For a temporary shell-only workaround, run newgrp input and then start the
program from that shell.
On many Linux desktops, add a .desktop file to ~/.config/autostart/:
mkdir -p ~/.config/autostart
cat > ~/.config/autostart/headphone-mute.desktop <<'EOF'
[Desktop Entry]
Type=Application
Name=headphone-mute
Exec=/home/YOUR_USERNAME/.cargo/bin/headphone-mute
Terminal=false
X-GNOME-Autostart-enabled=true
EOFReplace YOUR_USERNAME with your Linux username.
Use pgrep to check whether it is running:
pgrep -af headphone-muteIf it started successfully, the command prints the process ID and command line. If it prints nothing, inspect your desktop environment's autostart logs or run the command manually to see the error:
/home/YOUR_USERNAME/.cargo/bin/headphone-muteCreate a user service file:
mkdir -p ~/.config/systemd/user
nano ~/.config/systemd/user/headphone-mute.serviceAdd:
[Unit]
Description=Headphone mute monitor
[Service]
Type=simple
ExecStart=%h/.cargo/bin/headphone-mute
Restart=on-failure
[Install]
WantedBy=default.targetThen enable it:
systemctl --user daemon-reload
systemctl --user enable --now headphone-mute.serviceCheck its status and recent logs with:
systemctl --user status headphone-mute.service
journalctl --user -u headphone-mute.serviceThe service should show active (running). The log should include
Starting headphone monitor... and, after a device is found, Found headphone switch device.
This makes the tool start automatically after login.
I developed it to use a package that will mute sound when I unplug my wired headphones.
You might use it for that reason too...
^_^
It tries to mute using wpctl, first. If it fails, it tries with pactl.
Note
It was tested on Ubuntu 24.04.4 LTS (x86_64)
- Build using cargo:
cargo build - Run executable with sudo:
sudo ./target/debug/headphone-mute
Just run cargo run with user that does not have sudo privilege.
It should show: Permission denied error in terminal, since /dev/input files require sudo privilege.
If you see a message about /dev/input permissions, add your user to the input group:
sudo usermod -aG input "$USER"Then log out and back in, or reboot.
Make sure one of these is installed and available in your session:
- wpctl (WirePlumber)
- pactl (PulseAudio)
You can usually install them via your distro package manager.
MIT OR Apache-2.0
If you have idea, feel free to open PR.