How to use command line to change volume?

212,063

Solution 1

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).

Solution 2

To mute:

amixer -D pulse sset Master mute

To unmute:

amixer -D pulse sset Master unmute

To turn volume up 5%:

amixer -D pulse sset Master 5%+

To turn volume down 5%:

amixer -D pulse sset Master 5%-

Solution 3

pactl/pacmd (unlike amixer) allows increasing volume over 100% :-).

pactl set-sink-mute 0 toggle  # toggle mute, also you have true/false
pactl set-sink-volume 0 0     # mute (force)
pactl set-sink-volume 0 100%  # max
pactl set-sink-volume 0 +5%   # +5% (up)
pactl set-sink-volume 0 -5%   # -5% (down)

Manual settings over 100% is possible in pavucontrol (unlike alsamixer).

Note: If you want to share the same commands on different hosts with different sinks, you can use @DEFAULT_SINK@ as a sink instead of number 0:

pactl set-sink-volume @DEFAULT_SINK@ +5%

You set your default sink with pactl set-default-sink my-sink-name (list names with pactl list short sinks).

Source: askubuntu.com, wiki.archlinux.org.

Solution 4

Higher volume:

amixer set Master 3%+

Lower volume:

amixer set Master 3%-

Mute toggle:

amixer set Master toggle

Example keybindings for i3/sway, the commands are after exec:

bindsym XF86AudioRaiseVolume exec amixer set Master 3%+
bindsym XF86AudioLowerVolume exec amixer set Master 3%-
bindsym XF86AudioMute exec amixer set Master toggle
bindsym Ctrl+$alt+Up  exec amixer set Master 3%+
bindsym Ctrl+$alt+Down exec amixer set Master 3%-

Solution 5

amixer worked for me but I didn't get the nice animation that I get when I press the volume up button on my keyboard.

I decided to use xte to directly press that key from the command line:

Volume up: xte 'key 0x1008ff13'

Volume down: xte 'key 0x1008ff11'

Mute: xte 'key 0x1008ff12'

I figured out the keysym (that hex number) by using xev.

sudo apt-get install xbindkeys xautomation
xev

and then press the volume up button on your keyboard to get the keysym. The key sym may vary from system to system so finding it from xev will be the most reliable way.

The result looks like this for me:

KeyRelease event, serial 37, synthetic NO, window 0x2c00001, root 0xef, subw 0x0, time 6660080, (566,573), root:(664,651), state 0x0, keycode 123 (keysym 0x1008ff13, XF86AudioRaiseVolume), same_screen YES, XLookupString gives 0 bytes: XFilterEvent returns: False

Share:
212,063

Related videos on Youtube

Gilles 'SO- stop being evil'
Author by

Gilles 'SO- stop being evil'

Updated on September 18, 2022

Comments

  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 1 year

    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.

    • rhombidodecahedron
      rhombidodecahedron over 7 years
      If anyone's coming here from lubuntu to fix their volume control buttons, putting <command>amixer -D pulse sset Master 3%+ unmute</command> in the relevant keybind of ~/.config/openbox/lubuntu-rc.xml and then running openbox --reconfigure fixed it for me
    • Martian2020
      Martian2020 over 2 years
      This link is currently first one that google offers on the subject. However, I consider following QA as listing more options: askubuntu.com/questions/97936/…
  • Admin
    Admin over 12 years
    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;
  • Dario Seidl
    Dario Seidl over 12 years
    @89899.3K Thanks for the info, xdotool looks interesting.
  • fentick
    fentick about 12 years
    And as an addition, you can increase or decrease the volume by amixer set Master 10%+ and amixer set Master 10%-
  • Cerin
    Cerin almost 12 years
    This does not work in Ubuntu.
  • Andre Morua
    Andre Morua almost 11 years
    1. Use option -M for intuitive numbers 2. Maybe Ubutu uses PulseAudio 3. Ubuntu su***
  • ashes999
    ashes999 about 10 years
    I disagree @Cerin. This works on Ubuntu LTS 12 right now. Note that the M in master is case sensitive (must be uppercase).
  • shivams
    shivams about 9 years
    Cerin is right. This does not work. What works is : amixer -D pulse sset Master 50%
  • user2469006
    user2469006 almost 8 years
    To toggle un/mute: amixer set Master toggle.
  • piegames
    piegames almost 7 years
    You might need to replace "Master" with the device "amixer" tells you, for me it was "Speaker"
  • Morten
    Morten almost 7 years
    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?
  • Aquarius Power
    Aquarius Power over 6 years
    very good thx, that makes it perfect when we change window managers a lot on the fly for better gaming fps, so I use xbindkeys-config and I dont even have to restart X :) (lol the smile... no pun intended lol)
  • Roman Golyshev
    Roman Golyshev over 5 years
    You can also add unmute to latter commands to automatically unmute when you change volume (for convenience): amixer -D pulse sset Master unmute 5%+
  • ssokolow
    ssokolow over 5 years
    @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.
  • Michael
    Michael about 5 years
    If I try to set the volume > 41%, it always sets it to 1% higher than I requested... any idea why?
  • Dario Seidl
    Dario Seidl about 5 years
    @Michael, try with the -M switch. I edited the answer for an explanation.
  • Kirollos Morkos
    Kirollos Morkos about 4 years
    Does anyone know the difference to amixer here, as to why amixer does not allow more than 100%?
  • Nick Bull
    Nick Bull almost 4 years
    This may not work, as the 0 index sink is not always the currently active sink (e.g., bluetooth headphones tend to be index 1), and for whatever reason @DEFAULT_SINK@ didn't always work to return the active sink. You can find the active sink manually through the following: pactl list short sinks | awk '/RUNNING$/{print $1}
  • Nick Bull
    Nick Bull almost 4 years
    For e.g., pactl set-sink-mute 0 toggle, that becomes pactl set-sink-mute "$(pactl list short sinks | awk '/RUNNING$/{print $1})" toggle
  • Alexander
    Alexander over 3 years
    amixer set Headphone toggle can also be useful