161

Ubuntu's desktop gui is great for changing audio settings (System->Preferences->Sound), like the default input/output device and setting the volume.

However, I would like to be able to do these things from the command-line. What tool is the gui using behind the scenes?

Kees Cook
  • 17,473
paleozogt
  • 1,713

5 Answers5

196

You can control PulseAudio thoroughly through the command line using pacmd and pactl commands. For options see pacmd --help or the wiki at PulseAudio:

pacmd list-sinks for name or index number of possible sinks

pacmd list-sources for name or index number of possible sources

pacmd set-default-sink "SINKNAME" | index to set the default output sink

pacmd set-default-source "SOURCENAME" | index to set the default input

pacmd set-sink-volume index volume

pacmd set-source-volume index volume for volume control (65536 = 100 %, 0 = mute; or a bit more intuitive 0x10000 = 100 %, 0x7500 = 75 %, 0x0 = 0 %)

and many many more CLI options.


Note: Changing the output sink through the command line interface can only take effect if stream target device reading is disabled. This can be done by editing the corresponding line in /etc/pulse/default.pa to:

load-module module-stream-restore restore_device=false

Restart PulseAudio for changes to take effect:

pulseaudio -k


For a more elaborate tutorial on how to do this, and for instructions on how to change the sink during playback see this answer.

k_o_
  • 125
  • 6
Takkat
  • 142,284
  • 3
    Pulseaudio has changed, the first command now should be pacmd list-sinks I think – Anwar Jun 14 '12 at 18:59
  • How would one go about setting an output device to the default input device? I have a Line6 ToneportUX1 (used for moulding guitar tones) that shows up as an output device in Ubuntu's sound control panel, but it should be an input device. –  Apr 30 '13 at 01:44
  • @MikeDtrick: this can't be done with these commands. I have no experience with USB devices, but I believe it may be a profile/driver/port/ALSA issue you may have. Is the device recognized as input in ALSA? – Takkat Apr 30 '13 at 06:46
  • I've just started using AlsaMixer, so I'm probably not going to be much help. Under /proc/asound/cards/ it lists the Toneport as a USB device and under /proc/asound/pcm it says playback 1: capture 1. This is all after running alsamixer and clicking [f2]. –  May 01 '13 at 19:15
  • If your pacmd list-sinks lists only one sink but you can see several output devices in System Settings > Sound, check this answer: http://askubuntu.com/questions/63599/configuring-hdmi-audio-via-command-line – Severo Raz Jul 20 '14 at 21:59
  • The page behind the link "stream target device reading" is now at https://www.freedesktop.org/wiki/Software/PulseAudio/Documentation/User/DefaultDevice/ – scai Sep 11 '16 at 17:25
  • @scai; great! Thanks for this. I edited my answer for the new link. – Takkat Sep 11 '16 at 17:47
  • 1
    I couldn't switch from headphone to HDMI output this way. Here is how I solved it. – Pablo Bianchi Mar 30 '17 at 23:24
  • See also https://wiki.archlinux.org/index.php/PulseAudio/Examples#Set_default_input_sources where I found something very useful: how to find the right source and make it default ALL time time, through reboots, without running any commands or GUI every again. – miguev Nov 02 '17 at 16:39
  • can also do pactl list short sinks for a simpler list of sinks. You need the symbolic name. – Tim Richardson Jan 06 '18 at 20:37
  • @TimRichardson thank you for that suggestion - this was not yet possible in 2010 (when I wrote that answer). – Takkat Jan 06 '18 at 20:43
  • PulseAudio has added another hoop to jump through: you must change the stream-restore module in /etc/pulse/default.pa to read load-module module-stream-restore restore_device=false. If you do not, then PulseAudio will ignore the default sink for any program you have used before (even if it is no longer running). PulseAudio is trying to be clever and now treats the default sink as only a "fallback", to only be used if it doesn't know better. And, PulseAudio always seems to knows better. – hackerb9 May 05 '20 at 23:27
  • Thanks, it works! – jellycsc Sep 20 '20 at 01:07
  • If it wasn't immediately obvious, the sink index can be used in place of "SINKNAME". Thank you for your answer! – kas Nov 01 '20 at 17:41
  • This is how I changed to HDMI pacmd set-default-sink $(pactl list short sinks | grep hdmi | awk '{print $2}') && pulseaudio -k – Anand Rockzz Jun 13 '21 at 15:04
  • sink = output (speaker) source = input (microphone) – RenRen Nov 07 '22 at 12:17
11

Yes there is type alsamixer in terminal

alt text

Press ,

F1 - Help

F2 - System Information

F6 - Select Sound Card

Esc - Exit the menu

karthick87
  • 81,947
7

I found these instructions to be clear and easy to follow. It explains how to set the default audio output device (speakers, etc), from the command line, as well as how to make your chosen setting the system default, so that it is set after the machine is rebooted.

Set the default output sink

To list the output sinks available, type the following command:

$ pacmd list-sinks | grep -e 'name:' -e 'index:'

  • index: 0

    name: <alsa_output.pci-0000_04_01.0.analog-stereo>

    index: 1

    name: <combined>

The * in front of the index indicates the current default output.

To set a system wide default, add the source name in the default.pa file:

/etc/pulse/default.pa

...

set-default-sink alsa_output.pci-0000_04_01.0.analog-stereo

...

When done then you can logout/login or restart PulseAudio manually for these changes to take effect.

from Set the default output sink

Gino
  • 250
  • 3
  • 6
4
pavucontrol

Not the actual Ubuntu volume control, but better! It lets you control default devices, and even devices per application/stream.

You have to have the app actually be recording/playing for it to appear in the 'Playback' or 'Recording' tabs, but once you choose the device for that app, it seems to remember it forever.

Also, (on a side note), for old (non pulse) applications, run them after padsp, eg:

padsp some_old_app

pavucontrol

matiu
  • 207
  • 14
    The question is how to change the audio device from the command line. This is not an application for the command line. – xorinzor Jan 19 '19 at 17:55
  • 1
    also, pavucontrol is showing only connected devices, not the paired but disconnected ones. – cipricus Jun 26 '19 at 07:58
2

In the case of WirePlumber (a PipeWire session manager), wpctl can help you.

Get the available audio devices with wpctl status (default is shown with a *):

PipeWire 'pipewire-0' [0.3.77, machitgarha@machitgarha, cookie:2827478639]
 └─ Clients:
...

Audio ├─ Devices: │ 46. Built-in Audio [alsa] │ 78. Headset H390 [alsa] │
├─ Sinks: │ * 49. Built-in Audio Analog Stereo [vol: 0.90] │ 84. Headset H390 Pro [vol: 1.00] │
├─ Sink endpoints: │
├─ Sources: │...

...

Extract the number of the device from the "Sinks" section, and change the default audio sink (i.e. device) using wpctl set-default. For example, to change it to "Headset H390 Pro":

wpctl set-default 84

Check if the default sink is changed with wpctl status.