How to use command line to change volume?

  • I am trying to control the volume using my programming script. How can I do the following in Fedora 15, Ubuntu linux?

    1. Mute/ Unmute
    2. Volume up and volume down

    Note: Please note that I use a web USB microphone/speaker and also Analogue microphone/speaker. I want to apply to all to be sure.

    If anyone's coming here from lubuntu to fix their volume control buttons, putting `amixer -D pulse sset Master 3%+ unmute` in the relevant keybind of `~/.config/openbox/lubuntu-rc.xml` and then running `openbox --reconfigure` fixed it for me

  • Dario Seidl

    Dario Seidl Correct answer

    9 years ago

    You can use amixer. It's in the alsa-utils package on Ubuntu and Debian.

    Run amixer without parameters to get an overview about your controls for the default device.

    You can also use alsamixer without parameters (from the same package) to get a more visual overview. Use F6 to see and switch between devices. Commonly you might have PulseAudio and a hardware soundcard to select from.

    Then use amixer with the set command to set the volume. For example, to set the master channel to 50%:

    amixer set Master 50%
    

    Master is the control name and should match one that you see when running without paramters.

    Note the % sign, without it it will treat the value as 0 - 65536 level.

    If PulseAudio is not your default device, you might can use the -D switch:

    amixer -D pulse set Master 50%
    

    Other useful commands pointed out in the comments:

    To increase/decrease the volume use +/- after the number, use

    amixer set Master 10%+
    amixer set Master 10%-
    

    To mute, unmute or toggle between muted/unmuted state, use

    amixer set Master mute
    amixer set Master unmute
    amixer set Master toggle
    

    Also note that there might be two different percentage scales, the default raw and for some devices a more natural scale based on decibel, which is also used by alsamixer. Use -M to use the latter.

    Finally, if you're interested in PulseAudio only, you might want to check out pactl (see one of the other answers).

    Thanks! there are two ways to do this 1. As your excellent example: amixer set Master mute; amixer set Master unmute; 2. yum -y install xdotool; xdotool key XF86AudioRaiseVolume; xdotool key XF86AudioLowerVolume;

    @89899.3K Thanks for the info, xdotool looks interesting.

    And as an addition, you can increase or decrease the volume by `amixer set Master 10%+` and `amixer set Master 10%-`

    This does not work in Ubuntu.

    1. Use option -M for intuitive numbers 2. Maybe Ubutu uses PulseAudio 3. Ubuntu su***

    I disagree @Cerin. This works on Ubuntu LTS 12 right now. Note that the `M` in master is **case sensitive** (must be uppercase).

    Cerin is right. This does not work. What works is : `amixer -D pulse sset Master 50%`

    To toggle un/mute: `amixer set Master toggle`.

    You might need to replace "Master" with the device "amixer" tells you, for me it was "Speaker"

    I'm curious, where is the volume setting actually stored - does a kernel module have ways to persist data that is hidden for the user?

    @Morten It used to be done via an init script named `/etc/init.d/alsa-utils` which saved the mixer settings on shutdown and restored them on boot. Some distros (eg. Raspbian) still do it that way. I'm still preparing to upgrade my main machine off Kubuntu 14.04 LTS, so I can't tell you the paths under systemd, but upstart handles it via `/etc/init/alsa-store.conf` and `/etc/init/alsa-restore.conf`.

    If I try to set the volume > 41%, it always sets it to 1% higher than I requested... any idea why?

    @Michael, try with the `-M` switch. I edited the answer for an explanation.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM