Skip to content

Instantly share code, notes, and snippets.

@poulou0
Last active March 3, 2023 12:45
Show Gist options
  • Save poulou0/24ed997c6f64ced04bc30e91751c780b to your computer and use it in GitHub Desktop.
Save poulou0/24ed997c6f64ced04bc30e91751c780b to your computer and use it in GitHub Desktop.
Run x server (xorg xinit) on a headless raspberry pi in DISPLAY=:0

Install the minimum

sudo apt update && \
sudo apt install xvfb x11vnc xinit tightvncserver && \

We start Xorg server through Xvfb

printf "[Unit]\nDescription=xvfb custom\nAfter=multi-user.target\nStartLimitIntervalSec=0\n\n[Service]\nType=simple\nRestart=always\nRestartSec=1\nUser=pi\nExecStart=/usr/bin/xinit -- /usr/bin/Xvfb :0 -screen 0 1360x768x24\n\n[Install]\nWantedBy=multi-user.target\n" | sudo tee /etc/systemd/system/xvfb.service && \
sudo systemctl enable xvfb.service && sudo systemctl start xvfb.service \

We need to put something in the .initrc

Example

sudo apt install openbox tint2 firefox && printf "firefox -width 1360 -height 768 &\ntint2 &\nopenbox" > ~/.xinitrc

We setup the VNC

sudo su -l -c 'printf "123456\n123456\n\n" | vncpasswd' pi && \
printf '[Unit]\nDescription=Start x11vnc at startup.\nAfter=multi-user.target\n\n[Service]\nType=simple\nUser=pi\nExecStart=/usr/bin/x11vnc -forever -shared -loop -rfbauth /home/pi/.vnc/passwd -display :0\n\n[Install]\nWantedBy=multi-user.target\n' | sudo tee /etc/systemd/system/x11vnc.service && \
sudo systemctl enable x11vnc.service && sudo systemctl start x11vnc.service \

We need to set to autologin for the services to start (After=multi-user)

sudo raspi-config 

We can access with a VNC client on port :5900

Optional:

we can access from a browser with novnc on port :6080

sudo apt install git && \
git clone https://github.com/novnc/noVNC.git /home/pi/noVNC && \
printf "[Unit]\nDescription=noVNC custom\nAfter=vnc.target\nStartLimitIntervalSec=0\n\n[Service]\nType=simple\nRestart=always\nRestartSec=1\nUser=pi\nExecStart=/bin/bash /home/pi/noVNC/utils/novnc_proxy --vnc localhost:5900\n\n[Install]\nWantedBy=multi-user.target\n" | sudo tee /etc/systemd/system/novnc.service && \
sudo systemctl enable novnc.service && sudo systemctl start novnc.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment