Diogo Ferreira


Prevent Ubuntu 20.04 from changing audio device to USB headset on boot

This was bothering me.

I'm using an wireless headset (HyperX Cloud Flight Wireless Gaming Headset) that has an USB receptor and some old Logitech speakers on my desktop, currently running Ubuntu 20.04 (in dual boot with my Windows 10).

Every time I reboot or turn on my computer and boot Ubuntu the default output audio device is set to my headset, which most of the time is off (I only use it when gaming).

Going to Settings - Sound and choosing the correct output device (Line Out) only changes it for the current session.

You can solve this by doing the following:

Edit the file default.pa:

$ sudo vim /etc/pulse/default.pa

And comment the following lines:

#.ifexists module-switch-on-connect.so
#load-module module-switch-on-connect

Reboot. Now your computer should leave the output device you select in settings alone. My guess is that every time you turn on the computer it finds the USB receptor and change to it.

How to change the audio output between headphones and speakers on Fedora 25

EDIT: I’ve corrected the script and added a small explanation on how to find out your sound card id.

In the last few days I’ve been busy installing and configuring the new Fedora 25 Fedora 25 on my computers. I’ve installed it on my desktop and on my MacBook as well, and so far everything as been running quite smoothly! Nevertheless, nothing is ever 100% perfect, and I’ve run into a few quirks here and there. One of those quirks is that I couldn’t find a way to change the audio output between my headphones and my speakers, having them connected at the same time!

The symptoms were:

  • If I plugged the headphones in, sound started to play and the speakers were muted, as intended.

  • If I unplugged the headphones, sound started to play on the speakers. So far so good.

There is one problem with this approach. I really don’t want to plug and unplug the headphones every time I want to use them. So, after trying a few things without success, I’ve decided to write a script that takes care of everything! Run once, it changes from the current playing output to the other. Run it again, and it goes back. No sound coming from both ends, no strange mutes, everything works as expected! Let’s see how to do that.

First steps:


$ pacmd list-sinks | grep analog-output

and check your sinks names.

The output on my computer is:

analog-output-lineout: Line Out (priority 9900, latency offset 0 usec, available: no)analog-output-headphones: Headphones (priority 9000, latency offset 0 usec, available: yes)active port: <analog-output-lineout>

Now I know that I want to change between analog-output-lineout and analog-output-headphones. Check your output to find out which naming scheme you’re using.

Now run:

$ pacmd list-cards | grep alsa_output

And you should get an output like this:

alsa_output.pci-0000_00_1b.0.analog-stereo/#1: Built-in Audio Analog Stereoalsa_output.pci-0000_00_1b.0.analog-stereo.monitor/#1: Monitor of Built-in Audio Analog Stereo

My device is alsa_output.pci-0000_00_1b.0.analog-stereo, since I’m not using HDMI sound. You have to look at yours and see if you find something similar. It should be easy to identify.

The script:


$ vim audio-switcher

Copy the following script and replace DEVICE FIRSTDEVICE and SECONDDEVICE with the values from the previous steps.

EDIT: You must replace ID with your own sound card id. You can find it with $ cat /proc/asound/cards and looking at the leftmost number.


ACTIVE_SINK=$(pacmd list-sinks | grep 'active port' | awk '{ print $3 }')

if [ "$ACTIVE_SINK" = "<analog-output-headphones>" ]; then
  echo "[*] Enabling all analog output on $DEVICE."
  pacmd set-sink-port "$DEVICE" "$FIRSTDEVICE" > /dev/null
  amixer -c "$ID" sset "Auto-Mute Mode" Disabled > /dev/null
  echo "[*] Enabling headphones only on $DEVICE."
  pacmd set-sink-port $DEVICE $SECONDDEVICE > /dev/null
  amixer -c "$ID" sset "Auto-Mute Mode" Enabled > /dev/null

exit 0

Mark the script as executable:

$ chmod +x audio-switcher

And you’re done! Just run it with $ ./audio-switcher and watch as your sound jumps from the speakers to the headphones and the other way around each time you run it.

You can move the script to /usr/local/bin/:

$ mv audio-switcher /usr/local/bin

Or symlink it:

$ sudo ln -s /path/to/audio-switcher /usr/local/bin/

Now you can call it from anywhere with $ audio-switcher.

You can also assign a keyboard shortcut by going to System Settings > Keyboard > + and setting a name of your choice, the audio-switcher command and a shortcut you like.