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?

2 Answers 2


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.

  • Yes, that's what I saw in the Qemu source too: SPICE implements its own functions to open a listening port instead of relying on Qemu common ones as VNC does, hence the behaviour difference. Commented Aug 8, 2015 at 14:28
  • 1
    I noticed that libvirt has an "auto" option for SPICE. It turns out that libvirt has a legacy option which treats "-1" as a special value for allocating a free port number using its internal port allocator. This functionality is similar to testing netstat yourself.
    – Lekensteyn
    Commented Aug 8, 2015 at 17:27
  • But it requires using libvirt as a supplementary layer over Qemu/KVM which I prefer to avoid. However I +1 your comment since it may help other people :). Commented Aug 8, 2015 at 17:31

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .