Skip to content

Instantly share code, notes, and snippets.

@awidegreen
Last active January 20, 2024 15:10
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save awidegreen/6003640 to your computer and use it in GitHub Desktop.
Save awidegreen/6003640 to your computer and use it in GitHub Desktop.
systemd definition for pulseaudio in system-mode (example for archlinux). The pulseaudio developers explicitly recommend to NOT run pulseaudo system-mode!
# systemd service spec for pulseaudio running in system mode -- not recommended though!
# on arch, put it under /etc/systemd/system/pulseaudio.service
# start with: systemctl start pulseaudio.service
# enable on boot: systemctl enable pulseaudio.service
[Unit]
Description=Pulseaudio sound server
After=avahi-daemon.service network.target
[Service]
ExecStart=/usr/bin/pulseaudio --system --disallow-exit --disallow-module-loading
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
@koenschepens
Copy link

I can't get it working on my intel-nuc running CentOs 8.
strange thing is that I don't have a problem when running the command directly from the command line as user root.

I have the exact same problem. Did you ever manage to fix this?

@janvda
Copy link

janvda commented Apr 26, 2021

@koenschepens
I managed to get it working. See my Audio setup here below:

Audio setup

Setup of pulseaudio in user mode

  1. Install latest version of pulseaudio
# command to install pulseaudio is:
#           dnf install pulseaudio
# below command returns version of installed pulseaudio
[root@nuc-jan ~]# pulseaudio --version
pulseaudio 13.99.1-rebootstrapped
  1. create a specific user for pulseaudio server
useradd -m pulseaudio
  1. assure that systemd is running for user pulseaudio at all times (also after reboot)
loginctl enable-linger pulseaudio

# you can check this by below command which should return a line like:
#  pulseau+ 1187308       1  0 10:32 ?        00:00:00 /usr/lib/systemd/systemd --user
ps -ef | grep pulse | grep systemd
  1. activate pulseaudio in user mode for user pulseaudio (has uid = 1041 in my case)
[root@nuc-jan ~]# su - pulseaudio
[pulseaudio@nuc-jan ~]$ export XDG_RUNTIME_DIR=/run/user/1041
[pulseaudio@nuc-jan ~]$ systemctl --user enable pulseaudio
Created symlink /home/pulseaudio/.config/systemd/user/default.target.wants/pulseaudio.service → /usr/lib/systemd/user/pulseaudio.service.
Created symlink /home/pulseaudio/.config/systemd/user/sockets.target.wants/pulseaudio.socket → /usr/lib/systemd/user/pulseaudio.socket.
[pulseaudio@nuc-jan ~]$ 
  1. Check if pulseaudio socket (=/run/user/1041/pulse/native) is active for user pulseaudio
# assure you are user `pulseaudio` and then enter following command
#     su - pulseaudio
[pulseaudio@nuc-jan ~]$ systemctl --user status pulseaudio.socket
● pulseaudio.socket - Sound System
   Loaded: loaded (/usr/lib/systemd/user/pulseaudio.socket; enabled; vendor preset: enabled)
   Active: active (listening) since Fri 2020-12-18 10:32:23 CET; 3min 53s ago
   Listen: /run/user/1041/pulse/native (Stream)
   CGroup: /user.slice/user-1041.slice/user@1041.service/pulseaudio.socket
[pulseaudio@nuc-jan ~]$
  1. give user pulseaudio access to audio group so it has access to audio hardware (/dev/snd/*)
# following command must be executed as user root:
usermod -a -G audio pulseaudio

This can be checked by:

# logon as user pulseaudio
root@nuc-jan ~]# su - pulseaudio
[pulseaudio@nuc-jan ~]$ groups
pulseaudio audio

# check if audio hardware is recognized (it might be needed to install alsa utilities)
[pulseaudio@nuc-jan ~]$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 0: ALC283 Analog [ALC283 Analog]
  Subdevices: 0/1
  Subdevice #0: subdevice #0
[pulseaudio@nuc-jan ~]$ 
  1. reboot the system

  2. Following command can be used to check if pulseaudio can access audio card
    (assure that no other program is accessing the audio card)

 runuser -l pulseaudio -c 'export XDG_RUNTIME_DIR=/run/user/1041 ; pactl info'
  1. NOT NEEDED: Assure that pulseaudio socket is accessible by any user
chmod a+rwx -R /run/user/1041/pulse
chmod a+x /run/pulse/1041
  1. Changes to /etc/pulse/daemon.conf
# JVA 2020-12-18 : next line assures that the daemon keeps on running 
exit-idle-time = -1
  1. Changes to /etc/pulse/default.pa
# JVA 2020-12-18: added auth-anonymous=true to below line
load-module module-native-protocol-unix auth-anonymous=true
  1. reboot the system to check if everything is properly setup

Test pulseaudio installation (including pavucontrol)

  1. Check if pulseaudio is running by entering following command as root user on host machine (=nuc-jan)
[root@nuc-jan ~]# export PULSE_SERVER=unix:/run/user/1041/pulse/native; pactl info
Server String: unix:/run/user/1041/pulse/native
Library Protocol Version: 33
Server Protocol Version: 33
Is Local: yes
Client Index: 16
Tile Size: 65472
User Name: pulseaudio
Host Name: nuc-jan.borsbeek
Server Name: pulseaudio
Server Version: 13.99.1-rebootstrapped
Default Sample Specification: s16le 2ch 44100Hz
Default Channel Map: front-left,front-right
Default Sink: alsa_output.pci-0000_00_0e.0.analog-stereo
Default Source: alsa_input.pci-0000_00_0e.0.analog-stereo
Cookie: ded1:9276
[root@nuc-jan ~]# 
  1. install pavucontrol on host (pavucontrol cannot be installed in current node-red alpine image !)
dnf install pavucontrol

# assure that XQuartz is running on macbook and 
# that you have enabled remote access (type "xhost +" in xterm window)

# you can then launch pavocontrol by following command
export PULSE_SERVER=unix:/run/user/1041/pulse/native; pavucontrol --display=Jans-MBP.lan:0.0

Accessing pulseaudio server (on host) in docker container

Assure that pulseaudio server (on host) is setup as in previous sections.

  1. Add following line to alpine docker image to install pulseaudio and also alsa, sox and pulseaudio utilities.
# https://wiki.alpinelinux.org/wiki/PulseAudio
#  notes:
#     - pavucontrol is not yet available in alpine image used in node-red:1.2.6-12
#     - alsa-plugins-pulse allows to use alsa applications via default pulseaudio
RUN set -ex && apk --no-cache add sox pulseaudio alsa-utils pulseaudio-utils pulseaudio-alsa alsa-plugins-pulse
  1. In docker compose file add following lines for the container:
    volumes:
      ...
       # map the location of pulseaudio unix socket to /run/pulseaudio/socket
       - '/run/user/1041/pulse/native:/run/pulseaudio/socket'
    environment:
      ...
      - PULSE_SERVER=unix:/run/pulseaudio/socket
  1. You can validate this by opening a console on the container and running below command
bash-5.0$ pactl info
Server String: unix:/run/pulseaudio/socket
Library Protocol Version: 33
Server Protocol Version: 33
Is Local: yes
Client Index: 4
Tile Size: 65472
User Name: pulseaudio
Host Name: nuc-jan.borsbeek
Server Name: pulseaudio
Server Version: 13.99.1-rebootstrapped
Default Sample Specification: s16le 2ch 44100Hz
Default Channel Map: front-left,front-right
Default Sink: alsa_output.pci-0000_00_0e.0.analog-stereo
Default Source: alsa_input.pci-0000_00_0e.0.analog-stereo
Cookie: ded1:9276
bash-5.0$ 

Change pulseaudio profile (using hdmi / analog / digital output)

Here below the commands to change the audio profile of the card.
Note that it is also possible to do this using pavucontrol.

# set profile to use digital output port (S/PDIF)
pactl set-card-profile alsa_card.pci-0000_00_0e.0 output:iec958-stereo+input:analog-stereo

# set profile to use analog output port (headphone)
pactl set-card-profile alsa_card.pci-0000_00_0e.0 output:analog-stereo+input:analog-stereo

# list audio card details (including available profiles)
pactl list cards

# check default sink/source
pactl info

@koenschepens
Copy link

koenschepens commented Apr 27, 2021

@janvda

Wow thank you so much for the extensive description! I will try it out :)

@dbinfl
Copy link

dbinfl commented Aug 26, 2021

@janvda
Hello! I have a server with 3 VMs that I want to have sound-enabled. In particular I wanted the VM running my music server to start on boot, and with sound enabled. The audio part of that solution evaded me until I found your post. I i implemented it as described, except for using a different account, and it works beautifully.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment