How to make Xvfb display visible?
Asked Answered
T

4

53

I am running selenium through Xvfb on display number :99 like this:

/usr/bin/Xvfb :99 -ac -screen 0 1024x768x8 & export DISPLAY=":99" && java -jar /usr/lib/selenium/selenium-server-standalone-2.24.1.jar -port 4444

However display with number other than :0 is not visible by default. How do I make it visible to actually see what selenium is doing in the browser?

Telfer answered 21/8, 2012 at 7:19 Comment(0)
L
25

It's virtual. From the man page Xvfb(1):

Xvfb is an X server that can run on machines with no display hardware and no physical input devices. It emulates a dumb framebuffer using virtual memory.

Also in the man page:

Xvfb -pixdepths 3 27 -fbdir /var/tmp The server will listen for connections as server number 0, will have the default screen configuration (one screen, 1280x1024x8), will also support pixmap depths of 3 and 27, and will use memory mapped files in /var/tmp for the framebuffer.

xwud -in /var/tmp/Xvfb_screen0 Displays screen 0 of the server started by the preceding example.

Loyce answered 21/8, 2012 at 7:23 Comment(3)
xwud is like a screenshot utility for virtual screens, right?Telfer
xwud is the undumper, xwd (X Window dump) is a generic screenshot utility for any X screen/disaply. The special thing about Xvfb though is that its contents are already xwd dump files (if you say so, via -fbdir that is).Loyce
@Nav Xvfb is a framebuffer in RAM, and no, there is no latency.Loyce
A
78

Use X11vnc

All you need is to install x11vnc via:

sudo apt-get install x11vnc xvfb fluxbox

Optionally install fluxbox to have simple window manager.

Run x11vnc in shell

Then to setup access to Xvfb for remote control, you can use X11 over SSH or VNC over SSH, e.g.

export DISPLAY=:1
Xvfb $DISPLAY -screen 0 1024x768x16 &
fluxbox &
x11vnc -display $DISPLAY -bg -forever -nopw -quiet -listen localhost -xkb

Run x11vnc using script

Here is script friendly version to run Xvfb, x11vnc and fluxbox:

export DISPLAY=${DISPLAY:-:0} # Select screen 0 by default.
xdpyinfo
if which x11vnc &>/dev/null; then
  ! pgrep -a x11vnc && x11vnc -bg -forever -nopw -quiet -display WAIT$DISPLAY &
fi
! pgrep -a Xvfb && Xvfb $DISPLAY -screen 0 1024x768x16 &
sleep 1
if which fluxbox &>/dev/null; then
  ! pgrep -a fluxbox && fluxbox 2>/dev/null &
fi
echo "IP: $(hostname -I) ($(hostname))"

Note: I'm using it in the following Docker project (check .funcs.cmds.inc.sh).

Run x11vnc using one-liner

Or you can use the following one-liner:

$ x11vnc -create -env FD_PROG=/usr/bin/fluxbox \
    -env X11VNC_FINDDISPLAY_ALWAYS_FAILS=1 \
        -env X11VNC_CREATE_GEOM=${1:-1024x768x16} \
        -gone 'killall Xvfb' \
        -bg -nopw

  • -create makes it start Xvfb
  • X11VNC_FINDDISPLAY_ALWAYS_FAILS=1 makes it goto the created Xvfb session (Display :1 rather than :0 which will be normal desktop)
  • FD_PROG=/usr/bin/fluxbox makes it fire up Fluxbox (Ubuntu's one, should have background Ubuntu logo)
  • X11VNC_CREATE_GEOM=${1:-1024x768x16} sets screen to 16bit colour 1024x768
  • -gone cleans up when it exits as otherwise Xvfb is left behind (killing xvfb also kills fluxbox)

Connect to Xvfb which runs in the Docker container

If you need to connect to Xvfb which runs on Docker container, it's a bit more complicated. You can ssh to your Docker container Here are the steps:

Install and run all dependencies inside the Docker container:

export DEBIAN_FRONTEND=noninteractive
apt update && apt -qqq install x11vnc xvfb fluxbox openssh-server net-tools
export DISPLAY=:0
# Xvfb $DISPLAY -screen 0 1024x768x16 &
x11vnc -display $DISPLAY -bg -forever -nopw -quiet -listen localhost -xkb
fluxbox 2>/dev/null &
mkdir -v /run/sshd ~/.ssh
/usr/sbin/sshd &
echo ssh-TYPE YOUR_PUBKEY_HERE | tee -a ~/.ssh/authorized_keys # Get it from 'ssh-add -L' on your host

Now, on the host, ssh to the container with a local tunnel:

ssh -L 5900:localhost:5900 [email protected]

Where 172.17.0.2 is your container IP. To check the IP of the container, run ip addr or ifconfig

For further details, check below.

Connect to VNC on remote

If your Xvfb listen on localhost only, you can setup tunneling to localhost, so a vncviewer can then connect to localhost to get remote control over the server. E.g.

ssh -N -T -L 5900:localhost:5900 user@remotehost &
vncviewer -encodings 'copyrect tight zrle hextile' localhost:5900

Or to listen on all addresses with password, use:

x11vnc -display :0.0 -usepw

To setup password, run: x11vnc -storepasswd.

Finally, use VNC client to connect to localhost:5900 on your local host.

See: Remote control over SSH at Xvfb Wikipedia page


Check also:

Attaboy answered 18/11, 2016 at 13:42 Comment(1)
This should be the preferred answer as it is well rounded. Covers all aspects of connecting to your frame buffer locally, via a tunnel and remotely. Thank you @AttaboyNonreturnable
A
35

You can get a live view by running a VNC server against the Xvfb display, like this:

x11vnc -display :99 -localhost &
vncviewer :0
Azotic answered 10/2, 2015 at 7:54 Comment(3)
If im running Selenium + x11vnc on a Jenkins node, is there anyway I can remotely view what’s occurring? Would I run vncviewer locally & point it to the Jenkins node?Capitalism
@MikeR in principle, yes, but the -localhost argument means that x11vnc is only accessible from localhost, so you would probably need different arguments to x11vnc, and perhaps an open firewall port. Alternatively, you could set up a proxy for VNC's port, eg ssh tunnel.Azotic
@Azotic I see a black screen instead of the browser. Can you please have a look at this issue? #63936435Grangerize
L
25

It's virtual. From the man page Xvfb(1):

Xvfb is an X server that can run on machines with no display hardware and no physical input devices. It emulates a dumb framebuffer using virtual memory.

Also in the man page:

Xvfb -pixdepths 3 27 -fbdir /var/tmp The server will listen for connections as server number 0, will have the default screen configuration (one screen, 1280x1024x8), will also support pixmap depths of 3 and 27, and will use memory mapped files in /var/tmp for the framebuffer.

xwud -in /var/tmp/Xvfb_screen0 Displays screen 0 of the server started by the preceding example.

Loyce answered 21/8, 2012 at 7:23 Comment(3)
xwud is like a screenshot utility for virtual screens, right?Telfer
xwud is the undumper, xwd (X Window dump) is a generic screenshot utility for any X screen/disaply. The special thing about Xvfb though is that its contents are already xwd dump files (if you say so, via -fbdir that is).Loyce
@Nav Xvfb is a framebuffer in RAM, and no, there is no latency.Loyce
M
2

A simple solution is also to constantly take screenshots of the program like this:

while /bin/true; do
    DISPLAY=:99 import -window root ~/Pictures/xvfb_screenshot.png
    sleep 0.1
done &
PID=$!
feh --reload 0.1 ~/Pictures/xvfb_screenshot.png 2>/dev/null
kill $PID

Note: requires to install imagemagick and feh

Magnific answered 16/2, 2018 at 15:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.