This is not a question about how to forward X through ssh or how to set DISPLAY so that the forwarded X works. That is a different question - if you wound up here looking for an answer to that just go 1 step further because 99% of X DISPLAY questions you'll find are for that.

My question is specifically how to choose the DISPLAY port. That is, if I do ssh -X ... then check DISPLAY with echo $DISPLAY, I might see localhost:10.0 or localhost:12.0 or something similar. It is not always the same each time, as in the previous example where it contains a 10 one time and a 12 some other time. How do I choose what that will be instead of letting it be semi-random each time? I want to force it to always choose 10.0 or always 12.0 or whatever, as long as it is consistent.

A solution to this preferably still works for multiple displays. I often have 2 or 3 X server displays - ie: localhost:10.0, localhost:10.1, localhost:10.2 - which ssh X successfully connects to and sets up. But again, it may use 10.0, 10.1, 10.2, or it may use 12.0, 12.1, 12.2. Whatever it is, I need it to be consistent.

My specific use case: I am using software at work which needs to know where to look for the displays. Each time ssh uses a different value for DISPLAY, I need to go into the config files for the software and manually change all the entries. It is a pain, and it really ought to be a set-and-forget kind of thing - hence my question.

I think this choice is made by ssh on the ssh server (X client) side. I don't think the X server knows or cares that, locally, the X client is considering it "10.0" or whatever it uses. Please correct me if I'm wrong, as I'm not sure.

I do not see any option for ssh to choose this value.

Preferably elevated privileges (root/admin) shouldn't be needed. I don't see why they would be for this, but I'm just stating that up front. If you have an answer which does require root, that is better than no answer and I might be able to use it.


I believe the problem you are running into is that the ssh server can't guarantee that someone else hasn't already used (locally or remotely forwarded) a particular display number (and port), hence it has to pick one that is open. I don't know for sure, but I presume it tries to open them in sequence and stops at the first one that will open. Any open display ports (including by some left unclosed by old process hanging around) could keep the one you want open indefinitely.

While not a guarantee, you could try setting a different value for X11DisplayOffset in your server's sshd_config. That still doesn't stop another user or even a rogue process of yours holding the first available X display/port open.

Alternatively, you could try setting the DISPLAY variable manually (on the server side) to some "unusual" or high-port number in hopes that you can avoid the typically used, lower-numbered ports. e.g., pick DISPLAY=:76, which means the port you need to then forward port 6076 back to your local X server; In case you didn't know, the TCP port X uses is 6000 + DISPLAY number (VNC servers typically use 5900 + DISPLAY, IIRC). You would do that manual forwarding on your ssh invocation using something like this:

ssh -R6076:localhost:6000 server

That will forward port 6076 on the remote (server) end back to your local display (assuming DISPLAY :0). Downside of this approach is that now you have to deal with all the XAuth stuff that ssh does for you automatically by using -X (see this other post for hints), but it should work. YYMV and the fiddly bits left as an exercise to the reader ;-).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.