204

Every time I boot up, I have to switch my audio output device to my "Line Out" headphones because for some reason my microphone defaults as a speaker.

See this screenshot:

See this screenshot

tiddlesticks
  • 2,143
  • I use this solution with Ubuntu 20 => https://askubuntu.com/a/908534/292232 – Kevin Robatel May 25 '20 at 19:30
  • At least you got the screenshot done when the dropdown list was shown - that's more than I can do on Ubuntu 20.04 ... – Trunk Feb 20 '21 at 14:23
  • 7
    Audio on Ubuntu couldn't be more broken. Its 2021 and reliably switching between USB / line in / external audio cards / etc etc is still a nightmare. – datUser Sep 28 '21 at 20:06

10 Answers10

173

Simple fix in 18.04 to at least 22.04 that worked for me and commenters:

I couldn't get the solution @singrium proposed to persist on Ubuntu 19.10. It worked with device numbers in /etc/pulse/default.pa but as I connected for example my headset device numbers were changing and things stopped working. It didn't work with device names in /etc/pulse/default.pa.

The simple workaround I found is adding the pactl set default sink command in startup applications.

  1. Run: pactl list short sinks
  2. Note the device name you want to use as default
  3. Try to run: pactl set-default-sink <Your_Device_Name>
    This should work without giving you an error message.
  4. Open the application "Startup Applications" (Should be preinstalled on Ubuntu)
  5. Click on "Add"
  6. Give your startup item a name
  7. Copy your command from above into the command field but adding /bin/bash -c at the start and your command between brackets:
    /bin/bash -c "pactl set-default-sink <Your_Device_Name>"
  8. Click on "Close".

You are now good to go. Your default audio device will be set on each boot and as such be persistent. If you want to change the default device simply edit the device name in startup applications command.

phanky5
  • 1,886
  • After an hour of trying, this is the only thing that worked for me, with a tweak to add 10 seconds delay: /bin/bash -c "sleep 10 && pactl set-default-sink 'alsa_output.pci-0000_07_00.1.hdmi-stereo-extra2'" – dez93_2000 Mar 16 '20 at 19:59
  • 3
    Marking this answer as the new solution. This method is simpler and doesn't involve editing or deleting config files and I've had some users mention that the earlier marked answer caused issues with their system. – tiddlesticks May 02 '20 at 00:37
  • 3
    which value is the device name? – fIwJlxSzApHEZIl Jun 08 '20 at 23:35
  • 2
    assuming you want your audio-out as hdmi (such as for a TV) and your os keeps picking something else (like a gaming headset), here is a one-liner to put in startup apps (with appropriate delay):

    /usr/bin/pactl set-default-sink $(/usr/bin/pactl list short sinks|/usr/bin/gawk -F'\\s+' '$2 ~ /^.*[Hh][Dd][Mm][Ii].*$/ { print $2 }')

    – zpangwin Jul 12 '20 at 22:48
  • For some reason, while the commands executed from the terminal work well, this doesn’t work as a startup application command. I tried both the manual variant from the answer and @zpangwin’s proposed automaticed solution (with an added sleep command). They seem to have no effect when I reboot. – kleinfreund Sep 12 '20 at 07:51
  • 1
    This solution got me part of the way there. I had to run pacmd list-cards to determine the device name and profile, then I created a executable bash script with these commands: pacmd set-card-profile <device_name> output:<profile> then pactl set-default-sink <device>.<profile>. – James Nov 24 '20 at 23:46
  • Tested on ubuntu 18.04.05 and worked fine. – sam nikzad Dec 13 '20 at 12:54
  • viewed on another thread, as an alternative go and paste the command to your .bashrc this work well – malko Jan 17 '21 at 00:03
  • @kleinfreund: @zpangwin's line should probably be wrapped thus: /bin/bash -c "sleep 10 && pactl set-default-sink $(/usr/bin/pactl list short sinks|/usr/bin/gawk -F'\\s+' '$2 ~ /^.*[Hh][Dd][Mm][Ii].*$/ { print $2 }')" and requires sudo apt install gawk. @zpangwin it'd be great to have a grep version of this if possible. Also if you know your pactl list short sinks card is "hdmi" or "HDMI" I guess the user could simplify that bit to [hdmi]? – dez93_2000 Feb 02 '21 at 00:23
  • How to do this with input device? – Miad Abdi Aug 13 '21 at 10:06
  • 2
    Without using gawk: /usr/bin/pactl set-default-sink $(/usr/bin/pactl list short sinks | grep -i hdmi | cut -f2) – Vinicius Nov 08 '21 at 18:08
  • when I run pactl list short sinks, I got error Connection failure: Connection refused.pa_context_connect() failed: Connection refused`. Any suggestions on this? – doraemon Nov 15 '21 at 07:42
  • @fIwJlxSzApHEZIl the first column is some kind of id, the second one the name. You can also check whether you picked the correct one by opening the sound settings and executing the code from step 3. You should see how the selected output device changes. – Christopher Feb 20 '22 at 16:06
  • Is there a way to set a first choice, 2nd choice, etc? I have a bluetooth headset that I want to be the output device if it's on/connected. If not, then I want the laptop's built-in speaker to be the output device. Right now, if the headset is off, then it chooses my dock's audio device, which doesn't have speakers attached. So every time I tun off my headset, I have to manually change the output device from the dock to the laptop speaker, which is annoying. – JoeMjr2 Apr 07 '22 at 18:12
  • I just added an edit to help out people in the future, but make sure you add small quotes around the device name. You can run without them when testing in the terminal, but the same command without quotes will not work when added to startup apps.

    pactl set-default-sink 'alsa_output.pci-0000_00_1f.3.analog-surround-71'

    – Mark Han May 28 '22 at 06:55
  • Worked on my machine only after adding some delay before the command execution , as explained here: https://askubuntu.com/questions/708012/startup-applications-not-working – nivpeled Sep 05 '22 at 06:32
  • Doesn't work for me. My problem is that when I put headset in jack and then remove it, it still goes back to the wrong audio output (my external monitor, i think) – CPBL Mar 22 '23 at 19:31
  • This doesn't list a sink for line out. – Gamebuster19901 Apr 25 '23 at 15:03
  • You can also add the command as a cron @reboot as per https://unix.stackexchange.com/a/316503 – opyate May 29 '23 at 13:40
  • This solution worked for me for Ubuntu 22.04.1. Thanks! – myyk Nov 16 '23 at 06:53
  • This does not work for me as with pactl list short sinks I see two devices, but in Settings/Sound/Output there are three output devices, and the third one I need to pick – ray_ray_ray Jan 14 '24 at 13:24
94

EDIT (05/03/2020):
It seems that @phanky5 figured out a simpler solution. Please check it before you try this one.


Here is a well explained tutorial to set a default audio input/output.

First: List the audio output devices using

pactl list short sinks

Example output:

43  alsa_output.pci-0000_00_1b.0.analog-stereo  PipeWire    s32le 2ch 48000Hz   IDLE
1076    alsa_output.usb-Logitech_Logitech_Wireless_Headset_4473D63ED97A-00.analog-stereo    PipeWire    s16le 2ch 48000Hz   IDLE

Second: To set a default output device, run the command

pactl set-default-sink <'output_device_name'>

Example: pactl set-default-sink 'alsa_output.pci-0000_00_1f.3.analog-stereo'

If you switch sinks a lot, you can use fzf to select one via the command line and make it the default:

pactl set-default-sink $(pactl list short sinks | cut -f2 | fzf)

To make the sink permanently the default, follow this:

First, open the file /etc/pulse/default.pa using:

sudo -H gedit /etc/pulse/default.pa

Then scroll to the bottom of the file, where two lines starting with set- will be commented out.

Now, uncomment these lines and replace the words input and output with the number of the sink (for output) / source (for input) that you want to be the default.

Example (sets both default input and output):

### Make some devices default
set-default-sink 3
set-default-source 3

PS: As discussed in the comments with Bim, it is also possible (according to him) to put the input/output name in /etc/pulse/default.pa instead of the input/output number.

Example:

### Make some devices default
set-default-sink alsa_output.pci-0000_00_1f.3.analog-stereo
set-default-source alsa_output.pci-0000_00_1f.3.analog-stereo.monitor

After doing this, save and exit. Then, delete the ~/.config/pulse directory by running rm -r ~/.config/pulse, and then reboot the system. Once the system reboots, the appropriate devices should now be set as the defaults.


EDIT:
As mentioned by ahmorris in his answer, some had to comment this line load-module module-switch-on-connect in the file /etc/pulse/default.pa to be # load-module module-switch-on-connect in order to make the changes persistent.

singrium
  • 6,880
  • I dont know what happened but after following this, my ubuntu 18 wouldn't boot at all: stuck at loading screen. I used recover mode with console, commented out the set-lines again, restored the deleted pulse folder, and then it worked. The only thing I did differently is that I only specified the sink (output) and not the source, which I left commented out. Any ideas about what might have happened? – Christopher.L Dec 16 '19 at 00:50
  • I see now that it says: 'Failure: No such entity', when I try pactl set-default-sink 'alsa_output.pci-0000_00_1b.0.hdmi-stereo' , which I just copied from the output of pactl list short sinks. I've also tried without the '-sign. – Christopher.L Dec 16 '19 at 01:06
  • @Christopher.L may be the output you set does not exist or spelled wrongly, check this link for more details – singrium Dec 16 '19 at 08:13
40

To make Singrium's answer work, I had to comment out this line from my /etc/pulse/default.pa by adding the #:

# load-module module-switch-on-connect

Otherwise, on startup my audio device would be set to my default device, but then would switch to my USB hub once it was detected. This prevents that switch and ensures your chosen audio device is the output.

Andrew
  • 561
  • 3
    This was my problem too! I have a wireless headset connected through a hub which gets detected AFTER my default device has been set on start up, cheers for this one. – SidOfc Jul 30 '19 at 17:38
  • 1
    Was necessary for me with a HDMI output / display that can also act as an audio device. – Bim Oct 30 '19 at 13:51
  • 3
    This answer alone does the job if you just need to stop a USB headset stealing your audio on every startup. – Alexander Revo Jan 26 '20 at 10:04
  • 1
    Why can't Ubuntu/Debian just send the audio to every available output, like Windows always did? – Rodrigo Apr 07 '20 at 15:35
13

This worked for me in Ubuntu 20.04.

Like was said before you have to list the audio output devices:

pactl list short sinks

And than you can add your chosen device to /etc/pulse/client.conf file like this:

default-sink = alsa_output.pci-0000_00_1f.3.analog-stereo

NOTE: You have to remove the "!" from the original line. In Ubuntu 20.10, you have to remove the ";" from the original line.

Pablo Bianchi
  • 15,657
Martin
  • 131
  • 2
    Recently had to fix my pulse setup since upgrading from 19.10 to 20.04 brought all kinds of audio weirdness. I decided to remove ~/.config/pulse and was left with one quirk being my headset becoming the default audio device. If this is the case this answer: https://askubuntu.com/a/1115641/177314 works like a charm too and requires less technical knowledge :) – SidOfc May 22 '20 at 09:45
12

This can be done with PulseAudio Volume Control if you prefer to use a GUI.

Once you've opened PulseAudio Volume Control, choose the configuration (the last tab), then select the output and/or input you want to use from the profile dropdown menu. It should look similar to this

PulseAudio Volume Control can also be used to adjust volume levels for each input, output, system sounds and applications that are currently in use.

Here's a post from Linuxhint with more info on PulseAudio.

Starbuck
  • 1,003
  • 8
  • 21
4

You can achieve this just using pulseaudio standard configuration file to do the following:

  • disable automatic switching to new plugged devices
  • set the default device

Steps

List your devices:

pactl list short sinks

Copy the name of the device that you want to make the default. Names are long. Something like alsa_output.pci-0000_00_1f.3.analog-stereo.

Now let's configure pulseaudio. Open the config file with:

sudo nano /etc/pulse/default.pa

And disable the pulseaudio auto switching to the latest plugged device. For this, make sure the following lines are starting with a #:

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

Now go to the bottom of the file and make your device the default:

set-default-sink '<your-device-name-here>'

Save the file and exit.

Restart pulseaudio with pulseaudio -k.

Sometimes you need to delete the entire ~/.config/pulse folder.

3

In my case, on my Bionic/18.04.3 changes in /etc/pulse/default.pa doesn't work (1)!

pactl list short sink
…
2   alsa_output.pci-0000_00_1f.3.analog-stereo …

pactl set-default-sink 2

or

pactl set-default-sink 'alsa_output.pci-0000_00_1f.3.analog-stereo'

works fine in CLI,

make change in my ~/.profile based on post How to startup a script automatically after login, add line:

pactl set-default-sink 'alsa_output.pci-0000_00_1f.3.analog-stereo'

and it works after reboot


(1) I have some error in my /var/log/syslog in all case with number or all text:

[pulseaudio] main.c: Sink output 2 does not exist.  
[pulseaudio] main.c: Sink output 'alsa_output.pci-0000_00_1f.3.analog-stereo' does not exist.
bcag2
  • 460
3

Ubuntu already stores the last selected audio device across reboots. Just things like USB docking stations or HDMI devices might mess up this mechanism, because they are detected as last device during boot and Ubuntu thinks, they just got connected.

So the most simple solution would be in many cases, only to comment the automatic switching on connect (load-module module-switch-on-connect) as outlined in this answer: https://askubuntu.com/a/1115641/39966

All the set-default-sink / -source commands or configurations may be not needed in most of the cases.

At the end of the day this was / is a bug in Ubuntu, which is at least for HDMI devices considered to be solved in recent versions. There is still another open bug for USB devices.

See also Selected Sound Output Device Not Persisting Between Reboots in gnome-control-center

Alex
  • 1,052
3

I had my rear panel and webcam mics. The issue was that every time I turned my PC on, the webcam mic was the default, instead of the desired rear panel mic. So I went ahead and installed pavucontrol and at the Configuration tab I simply turned it off, for god's sake:

sudo apt install pavucontrol

Then run:

pavucontrol

enter image description here

Mauricio
  • 131
2

This worked for me.

You can ignore the bit about having to change via command line, the bit that fixed the audio devices resetting was editing the /etc/pulse/default.pa file. Change line load-module module-stream-restore to load-module module-stream-restore restore_device=false and then restart pulse audio with pulseaudio -k.

You should now be able to update the audio sink and source via settings > sound and they should persist past a log out/in and system restart.

Pablo Bianchi
  • 15,657