Score:2

Sound works as root but no sound as www-data

lk flag

I'm trying to play a .wav file on my server (which is an old laptop with Ubuntu server installed) through a website using a shell script.

Now the problem is that when I play the .wav file through mplay on root, everything works just fine, but if I try to play the sound through the user www-data I get following error message:

$ sudo -u www-data sh script.sh
MPlayer 1.4 (Debian), built with gcc-11 (C) 2000-2019 MPlayer Team
do_connect: could not connect to socket
connect: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.

Playing bell.wav.
libavformat version 58.76.100 (external)
Audio only file format detected.
Load subtitles in ./
==========================================================================
Opening audio decoder: [mpg123] MPEG 1.0/2.0/2.5 layers I, II, III
AUDIO: 48000 Hz, 2 ch, s16le, 40.0 kbit/2.60% (ratio: 5000->192000)
Selected audio codec: [mpg123] afm: mpg123 (MPEG 1.0/2.0/2.5 layers I, II, III)
==========================================================================
AO: [pulse] Init failed: Connection refused
Failed to initialize audio driver 'pulse'
Could not open/initialize audio device -> no sound.
Audio: no sound
Video: no video

with root however it works just fine:

$ sh script.sh
MPlayer 1.4 (Debian), built with gcc-11 (C) 2000-2019 MPlayer Team
do_connect: could not connect to socket
connect: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.

Playing /var/www/html/bell.wav.
libavformat version 58.76.100 (external)
Audio only file format detected.
Load subtitles in /var/www/html/
==========================================================================
Opening audio decoder: [mpg123] MPEG 1.0/2.0/2.5 layers I, II, III
AUDIO: 48000 Hz, 2 ch, s16le, 40.0 kbit/2.60% (ratio: 5000->192000)
Selected audio codec: [mpg123] afm: mpg123 (MPEG 1.0/2.0/2.5 layers I, II, III)
==========================================================================
AO: [pulse] 48000Hz 2ch s16le (2 bytes per sample)
Video: no video
Starting playback...
A:   2.8 (02.8) of 3.0 (03.0)  0.5%

and this is how the shell script looks like:

$ cat script.sh
#!/bin/sh
mplayer '/var/www/html/bell.wav'

Now after looking through countless threads I made sure that www-data has all the permission it needs and additionally added them to the usergroups:

$ sudo -u www-data groups
www-data cdrom audio video pulse-access

I tried to just use mplay on it's own $ sudo -u www-data mplayer bell.wav to test if there might be issues with the script but I get the same error message mentioned above.

I looked in the dmsg with sudo dmesg | grep pulse, but there seems to be no messages for pulse. I just can't figure out what I do wrong, I am guessing it has to do something with the access permission for www-data since everything works fine if I use the exact same shell script with root.

Score:3
jp flag

The user www-data is by default a member of only the www-data group for security reasons.

Only, members of the audio group can play audio.

You can test by adding the www-data user to the audio group like so:

sudo adduser www-data audio

Then it should be able to play audio.

Although, the permissions inherited from the audio group can be considered of low security risk, but you need to know that all your scripts running under your web-server and owned by the www-data user will also inherit those permissions ... Therefore, you need to weigh all security risks against benefits and decide for yourself.

You can, later, remove the www-data user from the audio group like so:

sudo deluser www-data audio
Raffa avatar
jp flag
@memoki Test with full path to file like so `sudo -u www-data mplayer /var/www/html/bell.wav` first then if it works check with your script later … The error message might still display but the audio should play and therefore ignore the error message … If you made modifications to pulse audio, you might need to undo them first.
Score:0
lk flag

I solved the problem by starting a PulseAudio daemon before playing the sound in the script.sh and it worked just as intended.

The correct script now looks like this:

#!/bin/sh
pulseaudio -D
mplayer './bell.wav'
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.