Using tramp with EmacsW32 and cygwin, possible?

Take note of the cygwin-related information on the emacs wiki:
http://www.emacswiki.org/emacs/TrampMode

I don’t use EmacsW32, but I do successfully use TRAMP over ssh with Cygwin and NT Emacs.

I never got TRAMP working without an ssh agent (i.e. prompting for credentials) — as you noticed, it just hangs — but it works fine with one, so I didn’t spend time trying to resolve that. Assuming you’re also happy to use an agent (and you have already generated your keys and added authorized_keys files as necessary), the approach that works for me is:

  1. Run ssh-agent from cygwin.
  2. Launch NT Emacs via cygwin (so that it inherits the ssh-agent environment variables).
  3. Use ‘sshx’ as the TRAMP method (you can specify it manually in each file path, but I recommend making it the default, with (setq tramp-default-method "sshx")).

Those points are all covered at the Wiki, but you can also automate things somewhat:

For step 1, my bash profile automatically starts an ssh agent if one isn’t already running, or prompts me for my passphrase if my identity has expired. (See code below.)

For step 2, the target of my Windows shortcut for launching emacs looks like this:

C:\cygwin\bin\bash.exe --login -c "env HOME=\"`cygpath '%APPDATA%'`\" /cygdrive/c/emacs/emacs-23.1/bin/runemacs.exe"

The --login argument means my bash profile is executed, which ensures that step 1 has always been taken care of before emacs is started.

(Setting HOME isn’t necessary for TRAMP, but %APPDATA% is the default under NT Emacs, and this prevents the Cygwin home directory from taking precedence, therefore keeping your emacs config consistent regardless of whether you use this shortcut to run it.)

Finally, here’s the code from my cygwin .bash_profile that manages my ssh-agent. It’s a bit hacky, but it works for me.

Do note that I expire my identity after 4 hours (14400 seconds). TRAMP can hang after that happens (as mentioned before, I never got it to prompt me for credentials), so be aware of this issue. Type C-g to stop it from trying to connect. To resume connectivity, you can generally just start up another cygwin shell, re-enter your passphrase, and then TRAMP will be happy again. Occasionally it has remained unresponsive, but killing the *tramp* buffer will sort that out. Not expiring your identity should circumvent this issue, of course, should that be acceptable.

SSH_ENV="${HOME}/.ssh/environment"

# Run ssh-agent, if one is not already running
function start_agent {
    echo "Initialising new SSH agent..."
    /usr/bin/ssh-agent -t 14400 | sed 's/^echo/#echo/' > "${SSH_ENV}"
    echo succeeded
    chmod 600 "${SSH_ENV}"
    . "${SSH_ENV}" >/dev/null
    /usr/bin/ssh-add;
}

# Source SSH settings, if applicable
if [ -f "${SSH_ENV}" ]; then
    . "${SSH_ENV}" >/dev/null
    #ps ${SSH_AGENT_PID} doesn't work under cywgin
    ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ >/dev/null || {
        start_agent;
    }
    #if our ssh-added identity has expired (see -t option to ssh-agent)
    #then we need to re-add it
    if ! /usr/bin/ssh-add -l >/dev/null; then
        /usr/bin/ssh-add;
    fi
else
    #no ssh-agent running at the moment
    start_agent;
fi

Leave a Comment