title: Using Emacs server and emacsclient on other machines as other users
body: <p>I know that after I call <code>(start-server)</code> inside an existing Emacs session I can then use <code>emacsclient -c</code> (on the same computer) to create new frames that connect into that server, so that each new frame created by <code>emacsclient</code> has access to the same set of shared state (e.g. buffers).</p>
  
  <p>Most of the documentation I've found focuses on the "give me fast access to my local Emacs" use case, and so there are two things that I haven't seen any details of yet:</p>
  
  <ol>
  <li><p>Can <code>emacsclient -c</code> access Emacs servers started by <em>other</em> users, or is it hard-wired to detect only sessions started by my own user?</p></li>
  <li><p>Does Emacs server (directly or indirectly) support remote connections?  That is, is there some way to set up Emacs (possibly involving SSH) that allows calls to <code>emacsclient -c</code> on <em>remote</em> machines to have access to the <em>local</em> state of my Emacs server?</p></li>
  </ol>
  
  <p>(In case you haven't already guessed, what I'd ultimately like to do is combine the two techniques above to provide rudimentary collaborative editing support.)</p>
  
  <hr>
  
  <p>This is a real-world problem, so here's what I'm working with:</p>
  
  <ul>
  <li>The necessary functionality should be built into Emacs already (23.3.1, 64-bit).  I can stretch to Emacs extensions from the standard Ubuntu repositories, but I'd prefer not to.  (Which I believe rules out <em>Rudel</em>, sadly.)</li>
  <li>No new users or user spoofing.  Solutions should work with the existing set of user accounts, and users must not pretend to be other users (e.g. via <code>su</code> or <code>ssh</code>).</li>
  </ul>
  
  <p>If it makes any difference, the machines are on a private LAN, have OpenSSH clients and servers installed (and running), and all users can connect to (their own account on) all machines, but they have no shared filesystem.</p>
  
  <hr>
  
  <p>So, does anybody know whether Emacs server can</p>
  
  <ul>
  <li>grant access to other users, or</li>
  <li>provide remote access?</li>
  </ul>
  
  <p>EDIT</p>
  
  <p>As commented in rwb's answer, it's clear that the new windows being opened locally by running <code>emacsclient -c</code> are actually being created by the <em>remote</em> Emacs server process.  That is, <code>emacsclient</code> is simply triggering the relevant behaviour in the server.  This causes some issues with incorrect display settings, since the server does not normally have access to the local desktop (see below).  However, I can now connect in to a remote Emacs session if I use the following sequence of commands:</p>
  
  <p>In one terminal, where <code>1.22.333.44</code> is the IP address of <code>remotehost</code>:</p>
  
  <pre><code>ssh -t -X remotehost \
    "emacs -nw --eval
     '(progn (setq server-host \"1.22.333.44\" server-use-tcp t) (server-start))'"
  </code></pre>
  
  <p>Then in another (on the same machine):</p>
  
  <pre><code>scp remotehost:.emacs.d/server/server /tmp/server-file
  DISPLAY=localhost:10 emacsclient -c -f /tmp/server-file
  </code></pre>
  
  <p>The <code>emacsclient</code> command causes the remote Emacs server (which it finds details of in <code>/tmp/server-file</code>) to open up a graphical Emacs window (on the local display) that shares state with the Emacs session on the remote host.  </p>
  
  <p>Since the remote Emacs server was started via <code>ssh -X</code>, SSH provides it with access to my local display via a "fake" <code>:10</code> display.  The <code>DISPLAY=:10</code> passed to it (via <code>emacsclient</code>) thus causes a window to be opened on my local desktop.</p>
  
  <hr>
  
  <p>Although the approach above does tick the "Run Emacs server on remote machine, connect to it using <code>emacsclient</code> locally" box, it's very limited.  In fact, it's not much different to running the server and clients all locally as a single user: the only difference is that the server is now remote, so has access to different system resources.</p>
  
  <p>Unfortunately, launching via <code>ssh -X</code> is the only way I've been able to successfully open a window on a different machine's X server:</p>
  
  <ul>
  <li><p>Specifying a basic <code>DISPLAY=remote:0</code> gets nowhere (since Ubuntu X servers are started with the <code>-nolisten tcp</code> option).</p></li>
  <li><p>Connecting via SSH and then using <code>DISPLAY=:0</code> also fails, but this time only due to lack of suitable authentication credentials.  (I believe that's the case, anyway: the error message cryptically says <code>No protocol specified</code> / <code>Can't open display</code>.)</p></li>
  </ul>
  
  <p>I think that finding a way around the second problem would probably get me a good deal closer to a solution.</p>
  
  <hr>
  
  <p>Having read the posts at <a href="http://comments.gmane.org/gmane.emacs.devel/103350" rel="noreferrer">http://comments.gmane.org/gmane.emacs.devel/103350</a> (starting at the '25 Oct 14:50' post, about half way down) I'm starting to wonder if this might be one of the rare things that Emacs cannot do (i.e. is impossible ;-) ).</p>
  
  <p>However, if anyone does have a way to provide access to remote X displays without the permissions error above, I'm still open to persuasion....</p>
  
  <p><strong>TL;DR</strong></p>
  
  <p>As pointed out by rwb's answer, my questions above about whether Emacs can grant remote access have got things backwards.  There's no real problem with Emacs granting access to other users (<code>server-use-tcp</code> and a suitable <code>server-file</code> take care of this): rather the problem is <strong>how to allow a process on one machine to open new X windows on other users' X displays</strong> (specifically, the Emacs running <code>(start-server)</code> needs to open windows for users who ask it to via <code>emacsclient -c</code>).  That answer's beyond the scope of this question.</p>
  
  <p><strong>Alternative solution</strong></p>
  
  <p>As a workaround, we use the following:</p>
  
  <ul>
  <li>machine0: <code>tmux -S /tmp/shared-tmux-socket new-session</code></li>
  <li>machine1..machineN: <code>ssh -t machine0 tmux -S /tmp/shared-tmux-socket attach</code></li>
  </ul>
  
  <p>with suitable file permissions on <code>/tmp/shared-tmux-socket</code>.</p>
  
  <p>Then we run a text-mode Emacs in the shared terminal. :-)  This does raise some user-spoofing questions, but at least the host can see everything that the guests are doing.</p>