﻿{"id":987,"date":"2012-11-02T17:54:20","date_gmt":"2012-11-02T09:54:20","guid":{"rendered":"http:\/\/nick.workao.org\/?p=987"},"modified":"2012-11-02T17:54:29","modified_gmt":"2012-11-02T09:54:29","slug":"setup-a-git-server-on-windows-machine-with-msysgit-and-copssh%e4%bd%bf%e7%94%a8msysgit%e4%b8%8ecopssh%e6%90%ad%e5%bb%ba%e5%9f%ba%e4%ba%8ewindows%e7%9a%84git%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"http:\/\/nick.txtcc.com\/index.php\/other\/987","title":{"rendered":"Setup a Git server on Windows machine, with MSysGit and CopSSH(\u4f7f\u7528msysgit\u4e0ecopssh\u642d\u5efa\u57fa\u4e8ewindows\u7684git\u670d\u52a1\u5668)"},"content":{"rendered":"<p><strong>On Server Box<\/strong>\n<strong>\n<\/strong><\/p>\n<div><strong>1. install CopSSH<\/strong>\n<strong><strong><strong>Be sure to install it to a folder without space in its name. I used C:\\SSH. Then just press next until it finishes.\n<strong><strong>\u00a0<\/strong><\/strong>\n<strong><strong>2. install MSysGit<\/strong><\/strong>\u00a0<strong>\u00a0<\/strong>\n<strong><\/strong><\/strong><\/strong><\/strong>\n<div>Be sure to install it to a folder without space in its name. I used C:\\Git. The default values suits me well.<\/div>\n<div>3. Config CopSSH<\/div>\n<div>a. I would like to user a separate user for git access. So create a user from command line<\/div>\n<div><strong><strong>\u00a0<\/strong><\/strong>\n<strong><strong>net user git userspassword \/add<\/strong><\/strong><\/div>\n<div><\/div>\n<div>b. Then, goto &#8220;Start | Programs | Copssh | 01. Activate a user&#8221; to activate the user.<\/div>\n<div><\/div>\n<div><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/11\/095422Bqw.png\" alt=\"\" width=\"320\" height=\"246\" border=\"0\"><\/div>\n<div><\/div>\n<div>Note, you should clear the 2nd checkbox, we&#8217;ll generate keys later.<\/div>\n<div><\/div>\n<div>c. goto C:\\SSH\\etc folder, open the sshd_config file using wordpad (or<a href=\"http:\/\/www.flos-freeware.ch\/notepad2.html\">notepad2<\/a>), don&#8217;t use notepad, it&#8217;s a UNIX format file. Remove the leading # character for item &#8220;PasswordAuthentication&#8221; and change the &#8220;yes&#8221; to &#8220;no&#8221;. Also you can review the rest of the config file and change if necessary. For me, most of the default settings works fine.<\/div>\n<div><strong><strong>\u00a0<\/strong><\/strong>\n<strong><strong><\/strong><\/strong><\/div>\n<strong><strong><strong><strong><strong><strong><strong>d. Goto C:\\SSH\\home\\git\\.ssh folder, create a file named authorized_keys. Open this file using wordpad.<\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong>\n<div><\/div>\n<div>e. Install\/extract Putty if you have not. Invoke PUTTYGEN.EXE to generate a pair of keys:<\/div>\n<div>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/11\/095424siY.png\" alt=\"\" width=\"320\" height=\"307\" border=\"0\"><\/div>\n<div><\/div>\n<div>I used a 4096 bit SSH2 key. Save the private key to a folder and remember it. We&#8217;ll use it later. Copy the content in the text box labelled with &#8220;Public key for pasting&#8230;.&#8221; to the wordpad window you opened at step d, and save it, close the wordpad.\n\nf. (Re)Start SSH Server. You can either reboot your PC, or use below command line:\nnet stop opensshserver\nnet start opensshserver\n\ng. Now it&#8217;s ready to test SSH connection. Invoke Putty.EXE, put localhost as host name, if you changed port in step c, don&#8217;t forget to change it here. Navigate to Connection\/SSH\/Auth node, press the &#8220;Browse&#8221; button to select the private file you generated and stored in step e, as shown below:<\/div>\n<div>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/11\/0954262Ie.png\" alt=\"\" width=\"320\" height=\"306\" border=\"0\"><\/div>\n<div><\/div>\n<div>You can save the settings to save you some typing next time. Google it if you don&#8217;t know how. Now press &#8220;Open&#8221; button, you will see a warning window on first connection, press &#8220;Yes&#8221; to accept the key. Then a block terminal window pops up, with prompt &#8220;login as:&#8221;, input &#8220;git&#8221; (without quote), and you should be prompted for a key-phrase if you set it when you saved the private key.\u00a0<em>Note, this is not the password of the git user you created during step a!!!<\/em>. And with some luck (which you don&#8217;t need if you know what you are doing), you should see some window similar to this:<\/div>\n<div>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/11\/095427xEI.png\" alt=\"\" width=\"320\" height=\"198\" border=\"0\"><\/div>\n<div>\nIf you&#8217;ve made it, congratulations, the hardest part is behind you. If you don&#8217;t see it, then please review carefully what you have missed.\n\nh. invoke a command window if you have not (where have you executed those &#8220;net xxx&#8221; commands?), and type\ncd \/d %USERPROFILE%\necho export HOME=\/c\/SSH\/home\/git > .bashrc\n\ni. goto C:\\SSH\\home\\git folder, open the .bashrc file (yes, the same name as in the above line). Insert below line to the first line: (AGAIN, THIS IS A UNIX FILE!)\nexport PATH=\/cygdrive\/c\/Git\/bin:\/cygdrive\/c\/Git\/libexec\/git-core:$PATH\n<strong><strong>\u00a0<\/strong><\/strong><\/div>\n<div>NOTE, above &#8220;export PATH=&#8230;&#8221; must be in the same line!<\/div>\n<div>\n<strong><strong><strong><strong>4. setup a Git repository.<\/strong><\/strong><\/strong>\ngoto C:\\SSH\\home\\git and make a folder named test.git. Right click the folder and select &#8220;Git Bash&#8221; from context menu. (What? You did not choose the explorer integration? Goto start menu, find Git|Git Bash, and use command line to goto this folder). Then input below lines:\n$ git init &#8211;bare\nInitialized empty Git repository in C:\/SSH\/home\/git\/test.git\/\n\nNow the server setup is DONE. You might need a cup of coffee of a cake to ease your nerve.\n\n<\/strong><\/div>\n<div>Special notes no 2003 server. It seems the sshd will experience error if the user account used to login is not a member of administrator (thanks for the comment from Raphael). So if you experience problem, try to add the user&#8217;s account to administrator group and try again.<\/div>\n<strong><strong><strong><strong><strong><strong><strong><strong><strong>\n<strong><strong>On Client PC<\/strong><\/strong><\/strong>\n<strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong>\n<div>1. Install MSysGit, as on server. You can choose any folder you like (better a folder without space in it&#8217;s path).\n\n2. Test connection, using Putty. Change the host name and port as in your environment. Don&#8217;t forget the Connection | SSH | Auth node setting. Copy the private key file to client machine and point the file in this node. Then press &#8220;Open&#8221; button, you should find it similar to what you have experienced at step g above. But this time, after you logged in, you are actually logged in to another computer! After you logged in, type &#8220;git&#8221;, and see if you have the familiar git help screen before you. If you see\n\n-bash: git: command not found\nthen please check if you have step i done correctly.\n\n3. Opne a command window, type\n<div>cd \/d %USERPROFILE%<\/div>\n<div>md .ssh\n\n<\/div>\nThen use windows explorer to open this folder. create a files (id_rsa.pub) if they don&#8217;t exist.\n\n4. Fire up PuttyGen.EXE again, load the private key you used at step 2. Paste the content of the text box to file id_ras.pub, and use menu &#8220;Conversions | Export OpenSSH key&#8221; to save to the folder you created at step 3, in name &#8220;id_rsa&#8221;. Now the folder %USERPROFILE&#8221;\\.ssh should have at least 2 files: id_rsa and id_rsa.pub\n<div>\n<div>5. create a empty folder and invoke the Git Bash and navigate to that folder. Type\n<strong><strong><strong><strong><strong><strong><strong><strong><strong><strong>ssh git@your.remote.host &#8220;echo something&#8221;<\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/div>\n<\/div>\n<strong><strong><strong>\n\ninput &#8220;yes&#8221; to accept the remote key. Then you should get an echo &#8220;something&#8221;\n<\/strong><\/strong><\/strong>\n<div><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/11\/095429F8p.png\" alt=\"\" width=\"320\" height=\"72\" border=\"0\"><\/div>\n<strong><strong><strong><strong>\n6. Now we are ready to clone the empty project we created at server\n<strong><strong>$ git clone ssh:\/\/git@tst\/SSH\/home\/git\/test.git<\/strong><\/strong>\nInitialized empty Git repository in D:\/g\/gt\/test\/.git\/\nwarning: You appear to have cloned an empty repository.\n\nNow you can make some change and push it back\n<strong><strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong>\n<div>\n<div>$cd test<\/div>\n<div>$vim readme.txt<\/div>\n<div>$git add readme.txt<\/div>\n<div>$git commit -a -m &#8220;first commit&#8221;<\/div>\n<div><strong><strong><strong><strong>[master (root-commit) f216dfe] first commit<\/strong><\/strong><\/strong><\/strong><\/div>\n<div><strong><strong><strong><strong><strong><strong>\n<\/strong><\/strong><\/strong><\/strong><\/strong><\/strong>\n<div>1 files changed, 1 insertions(+), 0 deletions(-)<\/div>\n<\/div>\n<div><strong><strong><strong><strong><strong><strong>\n<\/strong><\/strong><\/strong><\/strong><\/strong><\/strong>\n<div>create mode 100644 readme.txt<\/div>\n<\/div>\n<div>\n<div>\n<div>$ git push origin master<\/div>\n<\/div>\n<\/div>\nCounting objects: 3, done.\nWriting objects: 100% (3\/3), 236 bytes, done.\nTotal 3 (delta 0), reused 0 (delta 0)\nTo ssh:\/\/git@oti-tst\/SSH\/home\/git\/test.git\n* [new branch] master -> master<\/div>\n<div><\/div>\n<strong><strong><strong><strong><strong><strong>Congratulations, you can clone and push back. It&#8217;s not as hard as it sounds, after all \ud83d\ude00<\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/div>\n<strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong>\n<\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong>\n<div><strong><strong>\u00a0<\/strong><\/strong>Setting up multiple accounts\n<div><\/div>\n<\/div>\n<div>\n<div>I just realized that I forgot the part on how to make your repository accessible to multiple users. It&#8217;s simple once you have gone through above steps: adding another user is just to generate another key-pair, and put the public-key into the authorized-key file. That&#8217;s all. Then the new user can access your repository. (just follow the client setup, no other server settings needs to be changed).<\/div>\n<div><\/div>\n<div>I know a better way is to use Gitosis, but I&#8217;m not able to run it without cygwin yet \ud83d\ude41<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>On Server Box 1. install CopSSH Be sure to install it to a folder without space in its name. I used &#46;&#46;&#46;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[338,14,307,17],"class_list":["post-987","post","type-post","status-publish","format-standard","hentry","category-other","tag-git","tag-server","tag-ssh","tag-windows"],"_links":{"self":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/987","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/comments?post=987"}],"version-history":[{"count":1,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/987\/revisions"}],"predecessor-version":[{"id":993,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/987\/revisions\/993"}],"wp:attachment":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/media?parent=987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/categories?post=987"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/tags?post=987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}