OK – let me start this post by saying this is absolutely NOT for the faint hearted. This was a serious – and I re-emphasize this – serious pain in the arse to the do. Having almost no experience in Linux before – copSSH emulates the Linux environment through cygwin – let me tell you that this process was not easy. If you are an expert and thought – “pfftt, this process was easy” – I will reserve my language and ask you to kindly move along :) For those of you with no clue at all (ala me) – then this post is for you.
Firstly, full credit goes to the fantastic team at msysgit – these guys rock for porting GIT to Windows – GIT is an unreal DVCS and so I cant thank them enough for making this all possible. Secondly, full credit goes out to the guys at StackOverflow – unreal website and lots of fantastic users that have helped answer questions which have allowed the making of this guide possible. Please feel free to ask me questions, but I strongly recommend StackOverflow as your first port-of-call as I may take longer in getting back to you than they will. Also, this instruction guide is how I have set it up – if you start changing things – may the force be with you!
Finally, if this process worked for you – please leave a comment telling me (and everyone else) that it works and share this article around. It took me quite a bit of time to figure all this out on Windows – not to mention the time to type up this guide – so I would appreciate a link back ;)
Update 1: A free solution is called, GitStack.com, which is good but uses Apache and doesn’t use SSH keys – instead only providing password support (which might be fine for some people).
Update 2: You can still use copSSH 3.0.3 as it’s free.
- Download copSSH [SourceForge Link]
- Download msysgit
- Download TortiseGIT
- Download PuTTY Installer
- *Optional Download Git Install Guide (this guide as a Printable PDF)
Step 1 – Installing copSSH
- You’ve got copSSH – now go on and install it.
- Install copSSH into the c:\SSH directory
- Write down the SvcCOPSSH password – you will probably never need it – but just in case.
- It will install and create a bunch of directories – (Aside: I have found that you don’t need to create a password on your Windows User account – it’s not necessary if you use the settings I have later)
- Goto Start –> Programs –> copSSH –> Activate a User
- You can only activate existing users on Windows – so you can choose Your Own Account or Administrator (or indeed another account but I’ve used the primary)
- Uncheck the option – Create keys for public key authentication – we will do this ourselves.
- Finish the Installation Process.
Step 2 – Setup copSSH
- Open Windows Explorer – go into C:\SSH\etc and open BOTH your ssh_config and sshd_config in WordPad (Aside: Note – one has a “d” and one doesn’t – it’s an important difference)
- ssh_config – Delete the # (pound) key next to “Port” and change this to something like 4837 (or whatever Port you want to use)
- sshd_config – Delete the # (pound) key next to “Port” and change this the same as that in ssh_config – i.e. in this case 4837
- sshd_config – Delete the # (pound) key next to MaxAuthTries and make this 2
- sshd_config – Delete the # (pound) key next to RSAAuthentication & also next to the PubkeyAuthentication
- sshd_config – Delete the # (pound) key next to PasswordAuthentication and make this no.
- Save all the changes to ssh_config and sshd_config.
- Open your Router IP Address (or however you access your router) and open the Port you are using – i.e. in this case 4837 (Aside: If you don’t know it – c:\cmd –> ipconfig /all – will tell you)
- Save these Port changes in your router (& restart your router if needed)
- Open Windows Firewall with Advanced Security Settings and create a rule which allows the Port you set in both ssh_config and sshd_config – in this case 4837 (also do this in your Firewall if your firewall requires it – maybe/maybe not)
- Restart Your PC.
Step 3 – Installing PuTTY
- Welcome back after the Restart – now Open the PuTTY Installer you downloaded.
- Install Everything
- Navigate to the Putty Installed Directory (usually c:\Program Files\PuTTY)
- Open PuttyGen.exe
- Enter in Number of bits in a generated key: 4096
- Move your mouse around randomly as instructed until finished – don’t close PuttyGen!
- Open Windows Explorer and Navigate to c:\SSH\Home\<user>\.ssh\ – i.e in our case c:\SSH\Home\Administrator\.ssh\ (Aside: If .ssh doesn’t exist – you can only create it via a cmd.exe prompt – open cmd.exe and enter cd C:\SSH\Home\<user>\ then enter mkdir .ssh)
- Create a new file called authorized_keys (Aside: if it’s not there – Right Click – New Text Document – Delete everything (including the *.txt) – name the file authorized_keys)
- Open PuttyGen and Copy/Paste the Public Key for pasting into OpenSSH authorized_keys files code.
- Still in PuttyGen – save the Private Key as private_key.ppk in the same c:\SSH\Home\<user>\.ssh\ – i.e. i.e in our case c:\SSH\Home\Administrator\.ssh\private_key.ppk
- You should now have 2 files in this directory – authorized_keys and private_key.ppk
- To test our connection – load up putty.exe from the same c:\Program Files\PuTTY
- Enter your IP Address and Port in the Session window (Aside: If you don’t know your IP – c:\cmd –> ipconfig /all – will tell you)
- Click on SSH in the left hand menu and select – Auth – then navigate to your private key c:\SSH\Home\<user>\.ssh\private_key.ppk
- Hit Open and a terminal will open asking you to Enter Login Name: – enter your <user> – i.e. in our case Administrator
- You may get a message about Accept Public Key – type yes & if you get logged in great!
Step 4 – Installing msysgit
- Open the msysgit installer package.
- Ensure its C:\Program Files\Git (x32 bit) or C:\Program Files (x86)\Git (x64 bit)
- Set Use Git Bash Only (Aside: I wanted to only Use Git Bash console but if you want to Run Git from the Windows Command Prompt then you have to select this option – I would recommend only options 1 & 2 unless you really know what you are doing)
- Select Use (Tortoise)Plink available via C:\Program Files\TortoiseSVN\bin\TortoisePlink.exe. If it’s not available in the installer then set the variable in the windows environment to GIT_SSH=/path/to/TortoisePlink.exe. (Aside: I set this as using just PuTTY Plink.exe as opposed to TortoisePlink.exe – would recommend settings this instead to c:\Program Files\PuTTY\plink.exe but it’s up to you)
- Let it install and have a laugh in the meantime.
- Once it’s installed – you now have 2 windows which I will name 1. Git Bash (Right Click Mouse on a file/folder in Explorer) and 2. Start –> Programs –> copSSH –> Start a Unix Bash Shell – Unix Bash.
- Finally, goto your Git-Core folder in the GIT installed directory – C:\Program Files\Git\libexec\git-core (x32 bit) or C:\Program Files (x86)\Git\libexec\git-core (x64 bit) – and copy the files git.exe, git-receive-pack.exe, git-upload-archive.exe and git-upload-pack.exe and paste these into your C:\SSH\Bin.
Step 5 – Modify the User Environment
- The problem with copSSH is that it sets its $HOME environment to the c:\users\<name> or c:\Documents and Settings\<user> variable – and GIT looks for authorized_keys in this folder. Of course, we don’t want this – we setup our Server Environment in the C:\SSH\Home\<user>\.ssh and so we want GIT to look for keys in there.
- Open Windows Explorer – go into C:\SSH\Home\<user>\ and open the .bashrc file using a text editor (Aside: Be sure not to attempt to format this file as it has Unix Encodings NOT Dos encodings – i.e. just open it in a text editor and don’t use another viewer unless you want encoding errors – you don’t need to read the text in this file if you are following these instructions to a tee.)
- Put the Cursor at the beginning of the .bashrc file and use CTRL+F to find the following text – bashrc file.
- After the text .bashrc file# (after the pound with a space) – paste in – export HOME=/c/SSH/home/<user> – i.e. in our case export HOME=/c/SSH/home/Administrator
- Ensure there is a “space” before and after this paste in – i.e. .bashrc file#<space>export HOME=/c/SSH/home/Administrator<space>Shell Options#
- Save this file and close it.
- Copy this file and navigate to your windows assigned home directory (Aside: GIT will still be looking for your path in this directory so we need to set to refer to our C:\SSH\Home\<user>\ directory instead)
- Paste this in your c:\users\<name> or c:\Documents and Settings\<user> – so it now has c:\users\<name>\.bashrc for example.
- Open both a Git Bash (Right Click mouse in Explorer) and a Start –> Programs –> copSSH –> Start a Unix Bash Shell – Unix Bash shell
- Type echo $HOME – into both – they should both spit out – /c/SSH/Home/<user>
- If one does not – you need to set it via – export HOME=/c/SSH/home/<user>
Step 6 – Install TortiseGIT
- Install TortiseGIT per the standard installer using plink.exe (Aside: This should be the same as that setting at Step 4.4)
- Once the install has completed, open Windows Explorer.
- Right Click on anything and goto TortiseGIT –> Settings
- Select General and ensure the MSysGit path is seting to C:\Program Files\Git\bin (x32) or C:\Program Files (x86)\Git\bin (x64)
- Select Network and ensure that this is set to the same plink.exe path that we set in Step 6.1.
- Select Save then OK.
Step 7 – Using GIT and Plink
- Navigate to c:\SSH\Home\<user> and create a new directory called <myapp>.git – i.e. c:\SSH\Home\Administrator\myapp.git
- Right click on this new directory and select Git Bash
- When the window loads enter git –bare init
- You’ll now see a (BARE: master) appear and can close the window.
- Navigate to c:\Program Files\PuTTY and open pageant.exe – a small icon will appear in your system tray at the bottom right hand corner of your screen (a PC with a Hat on it) – open this and add your key from c:\SSH\Home\Administrator\.ssh\private_key.ppk (Aside: I recommend adding pageant.exe to your Start –> Startup folder so this will load each time you start your PC. To do this, goto Start –> PuTTY and right click on pageant.exe. Then change the target path to c:\<path-to-dir>\pageant.exe dir:\<path_to_private_key>\private_key.ppk. Then drag pageant.exe to your StartUp folder – this will then load both pageant.exe and your private key each time the system loads. Refer here if confused.)
- Now it’s time to clone this repository to our local development environment and finally start using GIT
- Navigate to where you want to have your local repository – in my case D:\Git\
- Right click inside this Directory and hit Git Clone
- Enter your URL as ssh://<user>@<ip_address>:<port number>/SSH/Home/<user>/<git directory> – i.e. in our case ssh://firstname.lastname@example.org:4837/SSH/Home/administrator/myapp.git
- The Directory should have the correct path – i.e. in my case D:\Git\myapp
- There is no need to Load Putty Key as we have already done this using pageant in Step 7.5.
- Hit OK and with a bit of luck (a lot of luck) you will get a successful clone.
Step 8 – The Real Test
- Cloning is one thing – the real test is pushing a new commit.
- Go to your newly cloned directory – D:\Git\myapp
- Add a New Text Document.txt
- Go back to just D:\Git\ –> Right Click on the Folder –> Git Commit –> “master” …
- Enter a new Commit message “Test” & tick the checkbox for Not Versioned commit on the file New Text Document.txt.
- Hit OK & then hit Push.
- Ensure Local: master to Remote: master (for this test) & Remote: origin
- Hit OK and you should get below
Counting objects: 1, done.
Compressing objects: 100% (1/1)
Writing objects: 100% (1/1)
Writing objects: 100% (1/1), 244 bytes, done.
Total 1 (delta 0), reused 0 (delta 0)
0526eba..a1bf4a4 master -> master
If yes (get a beer), if no (see Problem Guide and get ready for tears, profane language and keyboard bashing)
- I don’t understand how to install copSSH – can’t you add some pictures? No, but this guy can.
- I want to add another directory instead of installing my GIT repo in the home account ? OK, see here which must be done via Unix Bash.
- When I check services.msc – I can see that the service has stopped :( – what can I do ? You can setup a dependent copSSH service if you are brave. (tip: if you don’t know what you are doing, don’t stop the service ever – when you make changes to c:\SSH\etc\ directory restart your PC instead)
- Can I create keys with less encryption than 4096 from Step 3.5? You can never have enough encryption so no (well at least it’s my opinion!)
- I cant connect via PuTTY? You may have to regenerate your keys via puttygen.exe and put them back into your /.ssh/authorized_keys file per the Step 3 Instructions above.
- I keep getting “fatal: the remote end hung up unexpectedly”? Open a Git Bash window and type echo $HOME – ensure it is set to /c/SSH/Home/<user>/. If it is not – enter export HOME=/c/SSH/home/<user>
- I get “fatal: connection refused”? Check that you have correctly opened the port you have set on the service per Step 1.
- I get “fatal: no authorized methods accepted”? You will have to regenerate your keys with puttygen.exe and follow steps per 3.
- I get “git-upload-pack:command not found”? You must ensure that you have completed Step 4.7 and copied the relevant files into your c:\SSH\Bin directory.
- Sorry cannot read your mind? But these guys can help instantly.