I am running emacs in daemon mode and I got dissconnected from the server on which it was running. After re-connect, when I run

emacsclient -nc

I get the error

connect localhost port 6012: Connection refused

ERROR: Display localhost:12.0 can't be opened

The daemon still seems to be running, but I can't figure out how to connect to it, any suggestions?

Other fun facts, which may or may not be relevant:

I am connected through a screen session to a head node and then to another side node of a server.

I have been running this setup for a few days and usually I am able to re-connect without problem.

I'm sure I could just restart the daemon but I'd like to recover the working session as it is connected to a running matlab job, which I am interfacing with through matlab-emacs.

Edit: I tried Gilles suggestion and am still running into problems




I went go into the screen session and ran

export DISPLAY=localhost:18.0

and even ran echo $DISPLAY again inside of the screen session


Now emacsclient -nc returns

ERROR: Display localhost: 18.0 can't be opened

which is now the same display being used in the machine running the screen session, but still there is no connection.

Note that I tried also with

 export display=localhost:18

edit2: A note about my system architecture My laptop, in my office is connected via ssh to a server 'host1'. host1 has a number of node computers. I first create, or log into a screen session

 screen -S ohnoplus-five

and then connect to node005 with

ssh node005

I then detach from screen without logging out of node005 and re attach with

screen -r ohnoplus-five

from host1

In response to Gills suggestions, I have run export DISPLAY=localhost18.0 which is the the display returned by echo $DISPLAY on host1, not my local laptop.

  • Does emacsclient -c work outside the screen session? Is the environment variable XAUTHORITY set in or outside the screen session? Commented Sep 3, 2015 at 1:37
  • if I log into node005 (see above) without screen and run emacsclient -c I get a window with nothing in it that I can't seem to kill and which 'C-x #' does nothing to kill. emacsclient -nc gets me my old emacsclient session, except my matlab job terminates when I try this.
    – ohnoplus
    Commented Sep 3, 2015 at 1:58
  • Somehow running emacs only withing the screen session seems to prevent the disconnect to matlab (maybe only some of the time though). I get the message "M-Shell exited abnormally with code 1"
    – ohnoplus
    Commented Sep 3, 2015 at 1:59
  • echo $XAUTHORITY returns nothing in either the screen or outside of it.
    – ohnoplus
    Commented Sep 3, 2015 at 2:00

1 Answer 1


Remote GUI (X11) connections go through TCP port 6000+n where n is the display number¹. So the two messages refer to the same problem: some program tried to connect to display 12 and failed.

Emacsclient doesn't make X11 connections, Emacs does. So if you see this message, it means Emacsclient managed to contact Emacs and tell it to open a new frame. Emacsclient requests a GUI frame if it thinks that an X11 display is available, and a terminal frame otherwise. When the DISPLAY environment variable is set, Emacsclient thinks an X11 display is available.

The DISPLAY environment variable is set automatically by SSH when it's forwarding an X11 connection back to your local machine. But when you attach to an existing Screen session, you get the environment that was set inside the Screen session, including the DISPLAY variable. If you disconnect and reconnect, there's no guarantee that the display number is the same².

To update the DISPLAY variable, detach from the Screen session, run echo $DISPLAY to see the value set by SSH (e.g. localhost:13), then reattach to the Screen session and run export DISPLAY=localhost:13 (or whatever the correct number is). If you have multiple windows in the Screen session, you'll need to do it in each of them. If you create new windows, type Ctrl+A : setenv DISPLAY localhost:13` Enter to set the environment variable in Screen itself, for the sake of the new windows.

If you prefer, you can open a terminal frame by unsetting DISPLAY or by running emacsclient -nw.

¹ SSH uses display numbers starting from 10, leaving numbers 0–9 alone for local displays. In a typical situation, display 12 means that this is the third GUI connection that was opened over SSH.
² In fact, if you got disconnected due to a network problem but reconnected soon after, it's quite possible that the remote machine hasn't noticed the network problem yet — all it knows is that it's been a while since the client sent anything, but that's just normal inactivity — in which case display 12 is still in use as far as the remote machine is concerned.

If you have multiple SSH hops, and the connection from the local machine to the intermediate machine is interrupted and resumed, then upon resuming, the display number on the intermediate machine has changed, but the SSH connection from the intermediate machine to the final machine still forwards the original display number.

In this scenario, it would be simplest if you ran Screen only on the final machine, and used the intermediate machine as a proxy. Add

Host node[0-9]*
ProxyCommand ssh -W %h:%p host1

to your ~/.ssh/config and run ssh node005. Then you don't have to worry about the intermediate machine.

If you have to run Screen on the intermediate machine, then you'll have to restart the SSH connection from host1 to node005, after updating the DISPLAY variable. (You could also forward the TCP connection from the old display number to the new one, but that would only work if the old display number hasn't been reused in the meantime.)

  • Thanks for this very informative answer. I tried Gilles' suggestion and still can't connect. Please see my edit for more information.
    – ohnoplus
    Commented Sep 3, 2015 at 1:30
  • @ohnoplus The information you just added about the intermediate machine was important. I edited my answer to discuss this scenario. Commented Sep 3, 2015 at 2:16

You must log in to answer this question.

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