How to prevent sound mute when switching user?

Solution

  1. Add all users that should be able play back to the pulse-access group

    # adduser problemofficer pulse-access

  2. Create /etc/systemd/system/pulseaudio.service with the following content:

    [Service]
    Type=simple
    PIDFile=/var/run/pulse/pid
    ExecStart=/usr/bin/pulseaudio --daemonize=yes --system=yes --disallow-module-loading=yes --disallow-exit=yes
    
    [Install]
    WantedBy=multi-user.target
    
  3. Enable this new systemd service so that it is started on boot: # systemctl enable pulseaudio

  4. Be aware that this configuration is

    • less secure (e.g. other users can listen to your microphone)
    • sound output will not automatically switch to and from headphones anymore and
    • might prevent Bluetooth from working. Also see Caveat section below.
  5. Reboot

Cause

The reason why the sound turns of is that Pulseaudio is started on each login with this users privileges and the system¹ does not allow other users to listen on other users audio.

Solution Background

Pulseaudio

In order to solve this problem Pulseaudio must be started with root privileges so that it runs as a system wide daemon of which there is only for all users. Everyone will connect to this one instance and will be able to playback and listen to everything other users playback or record.

Pulseaudio will not actually run as root the whole time, but will drop those privileges and assume the user pulse.

From man pulseaudio

User pulse, group pulse: if PulseAudio is running as a system daemon (see --system above) and is started as root the daemon will drop privileges and become a normal user process using this user and group. If PulseAudio is running as a user daemon this user and group has no meaning.

Note that "user daemon" is not the same as "system deamon". The former is how Pulseaudio ran before, the latter is how it will run if the changes are applied.

In order to be able to connect to the Pulseaudio system service you need to be a member of the pulse-access group.

Again from man pulseaudio

Group pulse-access: if PulseAudio is running as a system daemon (see --system above) access is granted to members of this group when they connect via AF_UNIX sockets. If PulseAudio is running as a user daemon this group has no meaning.

Systemd Service

As a quick work around it would be possible to simply kill all "user daemon" instances of Pulseaudio and then run /usr/bin/pulseaudio --system=yes. This would start Pulseaudio without it becoming a daemon and in a more insecure way but might be useful for a quick proof-of-concept check.

To make this persistent and for the Pulseaudio daemon to start automatically on startup it needs to added as a systemd service. This is what the file /etc/systemd/system/pulseaudio.service is for.

Pulseaudio will not start a user daemon² when it already finds a system daemon, this is why this solution works.

Caveat

The official Pulseaudio documentation advices against using Pulseaudio as a system daemon. Some of the problems mentioned are:

  • ...one especially problematic thing from security point of view is module loading. Anyone who has access can load and unload modules. Module loading can be disabled, but then bluetooth and alsa hotplug functionality doesn't work...

(That means when you plug-in headphones the sound output does not automatically switch from speakers to headphones. The reverse is also true when you remove the headphones. Both have to be done manually.)

  • ...much higher memory usage and CPU load in system mode...

    (Personally I haven't noticed any change in load though.)

  • ...all users that have access to the server can sniff into each others audio streams, listen to their mikes, and so on...

  • ...you also lose a lot of further functionality, like the bridging to jack...

And possibly other things that I do not understand and therefore did not felt were worthwhile including here.

Note regarding thecarpy's answer: None of the steps described in his answer were necessary for this solution.

Ressources

  • man pulseaudio
  • https://www.freedesktop.org/wiki/Software/PulseAudio/Documentation/User/WhatIsWrongWithSystemWide/
  • https://www.freedesktop.org/wiki/Software/PulseAudio/Documentation/User/SystemWide/

¹ If someone can explain this in detail, I would be very thankful.

² Assumption [citation required]