Play sound *on* remote machine via ssh on Ubuntu / Linux

Solution 1:

Setting the DISPLAY variable to :0 solves the problem for me. Try running the command like this:

$ DISPLAY=:0 mpg123 test.mp3

Solution 2:

With recent Ubuntu systems (17.10 in this case), it is necessary to run pulseaudio at system start or manually to get audio via ssh. pulseaudio is started on a per-user basis as default, so if nobody is logged in, you only get the null sink for your SSH login, where all audio vanishes quietly.

For a permanent solution, a systemd service could be created:

cat <<EOF >/etc/systemd/system/pulseaudio.service
[Unit]
Description=Pulse Audio

[Service]
Type=simple
ExecStart=/usr/bin/pulseaudio --system --disallow-exit --disable-shm --exit-idle-time=-1

[Install]
WantedBy=multi-user.target
EOF

which makes sure that the pulseaudio daemon is started as the user pulse at startup. Since this is highly discouraged by the developers, the preferred solution for temporary use should be to issue sudo pulseaudio --system -D via SSH.

If a pulseaudio instance is already running, it should be killed to allow the new instance to discover the hardware. This impacts the audio for all logged-in users and should be handled on a case-by-case basis.

In any case, the logged-in user needs to be part of the pulse-access group:

sudo usermod -a -G pulse-access userName

Now it is possible to play sounds via SSH on the remote system (mpg123, mpv or others). Success can be checked by pactl list short sinks for proper audio sink detection.