Skip to content

Instantly share code, notes, and snippets.

@kkoomen
Last active December 16, 2019 14:43
Show Gist options
  • Save kkoomen/05303e469e514504f6af63620cbc3f7e to your computer and use it in GitHub Desktop.
Save kkoomen/05303e469e514504f6af63620cbc3f7e to your computer and use it in GitHub Desktop.
Setup VNC on a Raspberry Pi 3 to enable screen sharing via OSX.
#!/usr/bin/env bash
#
# Thanks to:
# https://stackoverflow.com/questions/32361132/screen-sharing-between-raspberry-pi-and-mac-osx#32361133
#
# The NVC server will be available by default on: <raspberry ip>:5900.
# Connect from your Mac by opening Screen Sharing and then enter the Raspberry's ip.
# Install x11vnc.
sudo apt-get install x11vnc
# Setup a password.
echo "Setup a new x11vnc password"
x11vnc -storepasswd
# Autostart VNC server on boot.
[[ ! -e ~/.config/autostart ]] && mkdir -p ~/.config/autostart
cat <<EOF > ~/.config/autostart/x11vnc.desktop && echo "Successfully enabled x11vnc on startup by setting configuration in ~/.config/autostart/x11vnc.desktop"
[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=X11VNC
Comment=
Exec=x11vnc -forever -usepw -display :0 -ultrafilexfer
StartupNotify=false
Terminal=false
Hidden=false
EOF
echo "Set correct file permissions for ~/.config/autostart/x11vnc.desktop"
sudo chmod a+r ~/.config/autostart/x11vnc.desktop
echo "Make the Raspberry to be visible in the sharing network of the Mac."
sudo apt-get install -y netatalk avahi-daemon
sudo update-rc.d avahi-daemon defaults
echo "Setup Avahi config files."
[[ ! -e /etc/avahi/services ]] && sudo mkdir -p /etc/avahi/services
cat <<EOF | sudo tee /etc/avahi/services/afpd.service 1>&2 > /dev/null && echo "Successfully enabled /etc/avahi/services/afpd.service"
<?xml version="1.0" standalone='no'?><!--*-nxml-*-->
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
<name replace-wildcards="yes">%h</name>
<service>
<type>_afpovertcp._tcp</type>
<port>548</port>
</service>
</service-group>
EOF
cat <<EOF | sudo tee /etc/avahi/services/rfb.service 1>&2 > /dev/null && echo "Successfully enabled /etc/avahi/services/rfb.service"
<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
<name replace-wildcards="yes">%h</name>
<service>
<type>_rfb._tcp</type>
<port>5900</port>
</service>
</service-group>
EOF
echo "Done. Reboot your system to apply the changes."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment