How to let Qemu autoselect a listening port for Spice?

I’m switching some VMs from VNC display type to SPICE.

VNC display type offers a very handy to= parameter allowing QEMU to automatically use the first free port from a defined range:

-display vnc=,to=100

Here QEMU will use ports between 5900 and 6000 for each new VNC display server.

What is the cleanest way to achieve such functionality with SPICE display (by the way I’m not using libvirt)?

Currently I’m on my way to script a test greping netstat output to find a yet unused port and pass it to:

-spice addr=,port=${unused_port}

Without mentioning race conditions, I just find this method quite ugly compared to the VNC one. And since QEMU manpage does not seem very exhaustive (VNC to= parameter doesn’t seem mentioned for instance), then I wonder if by any chance there is an equivalent parameter for SPICE?

Asked By: WhiteWinterWolf


It is (currently) not possible to let QEMU pick the next free port for SPICE. This happens because of an implementation detail: QEMU uses the spice_server_set_port which only accepts a single numeric parameter.

The best you can do now is to pick a port number outside QEMU and assign it as you do now.

If you find this too clumsy, consider using DNS to bind to the first free IP address + port available. The SPICE server code tries every address until it succeeds, so if you map local.test to,,, etc., then it will attempt to bind to each of these interfaces until one succeeds. Sounds awful, but it could be useful. Note that this method is only possible if you have no other service listening on the same port on the wildcard address.

Answered By: Lekensteyn

I know this is an old question, but I am sure people are still looking for a better solution. Here it is:

setup your qemu with -spice port=5900,addr=,disable-ticketing=on

then use iptables iptables -t nat -I PREROUTING -p tcp --dport 5900 -j DNAT --to

You can restrict it to certain interfaces or change the source port if you do not want to interfer with vnc etc.

and finally sysctl net.ipv4.conf.all.route_localnet=1

Using this config will allow you to connect to spice, using any of the servers ip addresses.

Answered By: Luke Attard
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.