38

While it is possible to use X applications remotely by using -X switch in ssh, the sound is being played in remote machine's speaker only. For example if I view a film in VLC/Totem only video is visible and I can't hear the audio. Is there a way to forward audio too? [without digging through Pulse-audio's setup, I mean; Like how ssh understands X forwarding by itself.]

I have tried this only in Ubuntu (in various Ubuntu versions from 9.10 through 10.10), if that helps.

Akilan
  • 735

1 Answers1

39

First, run paprefs, go to Network Server and check Enable network access to local sound devices. This will load "module-native-protocol-tcp" in PulseAudio.

You can now access the PulseAudio server in several ways:

Manually forward the TCP connection over SSH

  1. Use pax11publish to discover your PulseAudio listener port (usually 4713);
  2. Connect to another computer with ssh -R 24713:localhost:4713 (the remote port '24713' was chosen arbitrarily);
  3. Copy your authentication cookie (~/.config/pulse/cookie) to that computer;
  4. Finally run export PULSE_SERVER="tcp:localhost:24713" and test with pactl info.

Use automatic direct connection with X11-based discovery

Whenever you SSH with X11 forwarding enabled, PulseAudio programs use X11 to discover your sound server (use pax11publish or xprop -root PULSE_SERVER to see for yourself). They will try to establish a direct (non-SSH, unencrypted) connection to your computer for audio streaming.

Use automatic direct connection with DNS-SD discovery

If you have avahi-daemon and pulseaudio-zeroconf installed, you can activate "Allow other machines to discover local devices". This will load "module-zeroconf-publish".

Other computers, with the option "Make discoverable network devices available locally" enabled (module-zeroconf-discover), will automatically list outputs (sinks) that your computer exports. They will use a direct (unencrypted) connection for audio streaming.

This method also requires ~/.config/pulse/cookie to be identical across hosts.

u1686_grawity
  • 452,512
  • 2
    Where does ~/.pulse_cookie exist? On the remote or locally? – HSchmale Dec 29 '15 at 00:29
  • 2
    Both. It's generated by the pulseaudio daemon, then read by connecting clients (like a password), so you'll want to copy it from the server to clients. (Note that the path has since been changed to ~/.config/pulse/cookie; if one doesn't work, try the other.) – u1686_grawity Dec 29 '15 at 15:29
  • 2
    Note that after changing the settings in paprefs, you must manually restart at least pulseaudio. In my case, after spending hours trying to figure out what was wrong, I just rebooted, and then everything worked :). – crazy2be Jun 01 '16 at 23:59
  • This worked for me, thanks. Using a remote raspberry pi with rtl-sdr, hence wanting to get audio over ssh working. – Paul M Apr 17 '19 at 13:18
  • 2
    you may add if test "$SSH_CLIENT" ; then pax11publish -i ; export PULSE_SERVER=tcp:localhost:24713 ; fi to the remote .bashrc : this will set the pulsecookie and the env variable automatically when you ssh – am70 Jan 07 '21 at 11:05
  • 1
    In Ubuntu 21.04 you have to enable load-module module-native-protocol-tcp in /etc/pulse/default.pa , you cannot enable it with paprefs – am70 Dec 22 '21 at 11:40
  • 2
    It's quite important to be clear about what procedures are to be done on which machine. If the words "client" and "server" are used, it's also important to be clear about who those are. With X, programs run on an X client and are displayed on an X server, which is inverse of what may be intuited by the two words. So who is the first computer and who is the "other"? – Daniel Santos Jun 26 '22 at 05:22
  • 1
    @am70 Maybe this helps to enable the preferences in paprefs: https://bugs.launchpad.net/ubuntu/+source/paprefs/+bug/1942949 – jarno Dec 23 '22 at 14:13
  • @am70 I do not need pax11publish -i. because I use another method for access control: https://www.freedesktop.org/wiki/Software/PulseAudio/Documentation/User/Network/#authorization – jarno Dec 23 '22 at 18:29
  • I could not make the "SSH with X11 forwarding enabled" method work. – jarno Dec 25 '22 at 20:59
  • ^Player in the remote device told it could not connect to pulseaudio. – jarno Dec 26 '22 at 10:59
  • @DanielSantos I've just proposed some edits to the answer to address your concerns. Also, note that (for the "Manually forward the TCP connection over SSH" method) both machines have to have Pulseaudio installed - perhaps that's obvious to everyone else, but it wasn't obvious to me until I just tried it in a configuration where the ssh server/X client machine didn't. – Daniel Hatton Jun 05 '23 at 19:07