How To Use Subversion Over SSH


The Problem

I recently had a problem. I am a software developer and as such I use source control. For this purpose I generally prefer Subversion. I have been keeping my source repositories on an external drive with the intention to back them up to another secure location periodically (yeah, that never happens). I looked into online subversion hosting, but it seems a bit pricey for my few meager projects. I have a web host, but they don’t host subversion repositories over HTTPS as I’ve used in other situations. What to do?

The Solution

Subversion over SSH to the rescue! I actually have shell access to my web host (I can login to the server via SSH) and doing some quick research I discovered that I can run Subversion through an SSH tunnel. Cool! So how might one set this up?

Setting Up The Server

Prerequisites for the server are that you can login to your server via ssh. Got that? Good. Now while logged into your (Linux) server make sure that subversion is installed using the following commands:

which svn
which svnadmin
which svnserve

Can’t find them? Well, my host has them and they have great rates. You can check out 1and1 hosting here. Once you are sure that subversion is installed you just need to create your repository. I did it like this:

# Create the directory for the repository
mkdir -p repos/wordpress/themes/franzone
# Create the svn repository
svnadmin create repos/wordpress/themes/franzone
# You'll need to know the full path to your repository so...
cd repos/wordpress/themes/franzone

Now just note (or write down) the full path to your repository from the pwd command (print working directory) and your off to setup the client.

Setting Up The Client

There are two requirements for the client setup. First you should install subversion. If you just install a client (ie TortoiseSVN) you may be missing some required files so go ahead and install the real deal. The second requirement is an SSH client that can handle tunneling. If you’ve performed the server setup over SSH then chances are you already have this. I use cygwin with OpenSSH installed so this how-to will be based on that.

Just so you know, subversion does not support connecting to the svn+ssh protocol “out of the box”. If it did then I wouldn’t be writing this article! The actual steps are mostly taken care of in the installation of the two requirements. Now you just need to edit the subversion config file which can be found at %USERPROFILE%/Application Data/Subversion/config. Mine is actually under %USERPROFILE%/AppData/Roaming/Subversion/config for Windows Vista. Do a search in this file for the [tunnels] section. You will actually see a nice description of the svn+ssh protocol there which I highly suggest reading. After that add the following line:

ssh = C:/cygwin/bin/ssh

If you installed cygwin in another location then use that instead. Make sure that you use forward slashes too as the backslashes act as escape characters and then you have to use double backslashes and it just looks ugly. Save and close the file and you should be good! Yeah, that was easy.

Testing The Connection

Now to see if it actually works. Open up a DOS prompt (if you’re using Windows). Try to query your subversion repository like so:

svn info svn+ssh://USERNAME@DOMAIN.COM/full/path/to/repos/wordpress/themes/franzone

Now obviously use the values in that URL that make sense for your setup. If you don’t know how to do that then press the red box with the X in it at the top right corner of your web browser and then turn off your computer! 🙂 Hopefully all will go well. The client will try to authenticate with your USERNAME and prompt you for the password. Then it will connect and execute the appropriate svn client calls on your remote repository and tunnel the results back to you. Awesome!

Now if you want to kick it up a notch (as Emeril would say) then you can try integrating your new remote repository with a nice graphical client. I highly suggest TortoiseSVN as it integrates right into your Windows shell. You could also try using Subclipse or any others that you like.