如何通过终端切换音频输出?GUI选项不可点击求替代方案
Absolutely! When your GUI audio settings are stuck and unclickable, the terminal is your best friend for switching audio outputs. Let’s walk through how to do this step by step, and even set up one-click shortcuts for easy toggling between your built-in speaker and HDMI.
Step 1: Identify Your Audio Sinks
First, you need to find the unique identifiers (names or indexes) of your audio outputs—these are called "sinks" in Pulseaudio (the audio system used by most Linux desktops). Run this command in your terminal:
pactl list short sinks
You’ll get output that looks something like this:
0 alsa_output.pci-0000_00_1f.3.analog-stereo module-alsa-card.c s16le 2ch 44100Hz SUSPENDED 1 alsa_output.pci-0000_01_00.1.hdmi-stereo-extra1 module-alsa-card.c s16le 2ch 44100Hz SUSPENDED
Jot down the index number or full name of your built-in speaker (usually labeled analog-stereo) and HDMI output (labeled hdmi-stereo or similar).
Step 2: Direct Terminal Switch Commands
Once you have your sink IDs, use these commands to switch outputs instantly.
Switch to Built-In Speaker
Replace the sink name/index with the one you noted for your speaker:
# Using sink name pactl set-default-sink alsa_output.pci-0000_00_1f.3.analog-stereo # Or using index (faster) pactl set-default-sink 0
Switch to HDMI Output
Same idea, use your HDMI sink’s ID:
# Using sink name pactl set-default-sink alsa_output.pci-0000_01_00.1.hdmi-stereo-extra1 # Or using index pactl set-default-sink 1
Bonus: Move Active Audio Streams (Instant Switch)
The above commands set the default output for future audio, but if you want currently playing audio to switch immediately too, add this part to the command:
# For speaker (replace 0 with your speaker index) pactl set-default-sink 0 && pactl list short sink-inputs | awk '{print $1}' | while read -r input; do pactl move-sink-input "$input" 0; done # For HDMI (replace 1 with your HDMI index) pactl set-default-sink 1 && pactl list short sink-inputs | awk '{print $1}' | while read -r input; do pactl move-sink-input "$input" 1; done
Step 3: Create One-Click Shortcuts
Tired of typing commands every time? Create desktop shortcuts for instant toggling:
Option 1: Separate Shortcuts for Each Output
- Open your desktop’s shortcut creator (look for "Create Launcher" in your application menu, or right-click the panel/dashboard and select "Add to Panel").
- For the Speaker shortcut:
- Name:
Switch to Speaker - Command: Paste the full speaker switch command from above (including the stream move part)
- Icon: Pick a speaker icon (check
/usr/share/icons/for system icons, or use a custom one)
- Name:
- For the HDMI shortcut:
- Name:
Switch to HDMI - Command: Paste the full HDMI switch command
- Icon: Pick an HDMI/display icon
- Name:
Option 2: Single Toggle Shortcut (Switch Between Both)
If you want one shortcut that flips between speaker and HDMI automatically, make a tiny bash script:
- Create a file named
audio-toggle.shwith this content (replace the sink indexes with yours):
#!/bin/bash # Update these with your sink indexes/names SPEAKER_SINK="0" HDMI_SINK="1" # Get current default sink current_sink=$(pactl get-default-sink) # Toggle to the other sink if [ "$current_sink" = "$SPEAKER_SINK" ]; then new_sink="$HDMI_SINK" else new_sink="$SPEAKER_SINK" fi # Set default and move active streams pactl set-default-sink "$new_sink" pactl list short sink-inputs | awk '{print $1}' | while read -r input; do pactl move-sink-input "$input" "$new_sink" done
- Make the script executable:
chmod +x audio-toggle.sh
- Create a desktop shortcut that runs this script—now one click toggles between your two outputs!
Note for PipeWire Users
If your system uses PipeWire (common in newer Ubuntu, Fedora, and other distros), don’t worry—pactl still works perfectly. Alternatively, you can use PipeWire’s native tool wpctl:
- List sinks:
wpctl status - Set default sink:
wpctl set-default <sink-id> - Move all active streams:
wpctl move-playstream all <sink-id>
内容的提问来源于stack exchange,提问作者Nephilim

