Wednesday, September 13, 2006

Svn over ssh (svn+ssh://) on Windows via cygwin and PuTTy

One of the more obvious things lacking from Windows is an ssh (secure shell) client, and this has caused me no end of grief trying to check out code from Subversion repositories on servers that require ssh access - such as our own.

I'd previously managed to get access by forcing my username in the URL of the repository along with a saved PuTTY session name:

svn+ssh://alistair@(saved PuTTY session name)/etc...

...where the saved PuTTY session used my imported private key from the live host.

(Note: you *must* use PuTTYGen to convert your OpenSSH keys to PuTTY-compatible keys)

This caused a certain amount of sneering from the Debian users in the office, who didn't have to jump through any of these hoops, but I could live with that...

The latest problem to have me gesticulating at the screen like a south-american-footballer-in-front-of-a-referee-who's-reaching-for-his-pocket was the use of externals.

An external in a Subversion repository is a URL reference to another resource - for instance, in a Java app that requires a certain library to build, you might add an external pointing to the download url of the particular jar file that you need.

All well and good, but you can also add externals pointing to a different location in the same repository, for instance, to share config files between back end and front end modules.

Now here was the problem - you can't have relative urls in externals, they have to be absolute. This particular external was pointing to a different lcoation in the same repository in the standard URL way, which works fine on UNIX (Debian) clients because they have a native ssh client :


- but there was no way this was going to work on Windows via PuTTY - the only way I'd got svn+ssh:// urls to work previously was via the username@savedsessionname method. Every time I tried to update the working copy, it would get as far as that external, then give me this error message:

svn: Connection closed unexpectedly

So how do you get round this? Enter the cavalry - Cygwin.

Cygwin is a command shell for Windows that gives you a Bash-style environment, including....(drum roll)..... an ssh client!

With this, you can configure SVN (and the rather wonderful TortoiseSVN) to use this ssh client and - crucially - pass it a command line parameter that tells it to use your private key file for authentication.

If you open your SVN config file in a text editor, (you'll find this in C:\Documents and Settings\(username)\Application Data\Subversion\config) you'll see instructions on how to configure ssh. You can pretty much ignore it :-) and just use the following line:

ssh = "C:/(cygwin root path)/bin/ssh.exe" -i "C:/(path to your private key)"

...and that's it! It's one of those really simple solutions that took me a couple of minutes to implement, but hours of desk-headbanging frustration to find. You can also use the same command line in TortoiseSVN -> settings -> network -> ssh client.

One other note: make sure that you point ssh.exe at your openssh-compatible private key. If you've set up your key with PuTTYGen, you can load it in and export an OpenSSH-compatible key from there.


Anonymous said...

That's one way. I think though if you're going to go through all the trouble of installing Cygwin, you might as well use Cygwin's openssh package. Do your keygen right in cygwin using /usr/bin/ssh-keygen. That will create a .ssh directory under your home dir. Then you can take your new public key from there, add it to the authorized_keys file on the remote host, and off you go. No need to involve putty whatsoever.

Alistair Davidson said...

Yes, that's true - the only reason I have for using PuTTY over Cygwin's ssh client is that PuTTY runs in the background. Cygwin's ssh client doesn't, which means that everytime you perform an SVN operation with TortoiseSVN, you get a succession of empty command-prompt-style windows popping up and closing again, which can get quite irritating. There's probably an option you can specify to stop that happening, but I haven't found it yet.

Anonymous said...

I'm using svnant in an automated build environment, using cygwin openssh is the only way I can get svn+ssh to work on windows.

ax said...

You can [...] suppress the console windows by simply using the following as the SSH Client in TortoiseSVN Settings:

C:/cygwin/bin/run C:/cygwin/bin/ssh

acks to Alex

Anonymous said...

One thing I found is that the private key file will be ignored if you use backslashes in its path, so make sure you use forwardslashes.

spazmosity said...

I have trouble with plink on my computer. Therefore I want to use cygwin ssh to connect to my svn-repro.

I got my hopes up reading your text, but I can’t get it right. If I enter "D:/Program/cygwin/bin/ssh.exe -i D:/Program/cygwin/home/Lotta/rsa.txt -l mikael" in the tortoiseSetting->network->ssh client input field, what URL shall I enter when checking out a project with tortosieSVN?

I've tried with "svn+ssh://" but i only get "connection closed unexpectedly"

I also tried to use the "ssh" command directly from cygwin but the ssh answers with "connection to host *** port **: no Error" and it doesn’t matter if my -i parameter includes a key file or a random file.

Please help me, I would really appreciate it. If I don’t get this one i must re-install my Os.


Alistair Davidson said...

OK, well the first thing you need to do is get the ssh connection working in cygwin. If you can't get it working from the cygwin shell, then it's not going to work via TortoiseSVN.

So can you connect from there?

i.e. does:
ssh username@host


spazmosity said...

Hallo Alistair and thanks for the answer.

I think ssh works in cygwin but im not sure. As I wrote the ssh command tells me "no error" if i enter "ssh username@servier_ip"

But i really dont know if it works because if i change my username to a dummy username or changes the server_ip to a dummy ip i got the same answer, "no error" from ssh.

Alistair Davidson said...

OK, that sounds like ssh isn't working, to me. Even though it's saying "no error", I think i'ts lying :-)

You should get a welcome message and a remote command prompt like this:


greatdiamondsforless said...
This comment has been removed by a blog administrator.
hjkl said...

welcome to the wow power leveling cheap Wow gold service site, buy cheap wow gold,wow gold,world of warcraft power leveling buy wow gold

Anonymous said...

Cygwin was the only way I could get around TortoiseSVN's svn+ssh problem. Thanks for the tip!

josebrwn said...

At 11:58 AM BST, Anonymous ax said...

You can [...] suppress the console windows by simply using the following as the SSH Client in TortoiseSVN Settings:

C:/cygwin/bin/run C:/cygwin/bin/ssh

THIS is how you use cygwin's ssh with tortoise!!! You don't even have to edit config. Thank YOU!

Anonymous said...

I love you man!