您的位置:首页 > 其它

在mac os x上安装git server

2013-04-02 14:48 218 查看
So I’m a pretty big fan of GitHub but sometimes it just isn’t what I’m after. I have some projects that I want to keep hidden away (at least for the time being) and don’t really want to pay for the private hosting features.
Sound familiar? Well then you probably want to set up your own Git repository on a machine at home and push to that. If that does sound like something you’d want to do continue reading below.
This post is mainly a OSX port of
this article which is a great article, and while a lot of it is the same I hope to cover some of the OSX specifics that I personally found a little hidden when following that article.

What You Need

A spare computer running osx (these steps will work with some small tweaking on and unix system but as I couldn’t find a complete osx tutorial I’ve decided to stick to that, also I had a spare mac mini.)
Thats it apart from an internet connection which I can assume you have.

Just as a note I’m currently running OSX Lion but these steps shouldn’t be too different for Snow Leopard (anything else I can’t guarantee)

Step 1: SSH access

In order to push and pull from your repositories you need some way to connect to the computer they are stored on. SSH (or
secure shell) is the way to do just that. The following steps will help you set up your spare computer for ssh access using certificates only (more on that below).

1.1 Settings

Turning on ssh access in osx is pretty simple all you need to do is open up “System Preferences” and click on the “Sharing” icon.





Click the check box for remote log in and give your computer a name (something easy to type is probably not bad idea). Now at this point you have two options, you can allow any user on your computer to sign in or you can create
a new user that will just be used for git (perhaps even call them git). I would advise creating the git user anyway as this will make your repository URL much nicer. To create your new git user go back to “System Preferences” and click the “Users & Groups”
icon.





Click the plus beneath the list of existing users and fill out the details of your new user.
Now if you only want to allow git to ssh into your machine go back and change the “Allow Access For:” settings in “Sharing”

1.2 A quick test

Just to make sure you have set everything up correctly thus far open up terminal and type the following line on another computer on your local network.
>ssh git@yourOtherMachinesName

you should be asked to add this host to allowed addresses make sure you type yes here. Afterwards you should be prompted for a password, enter the password you assigned to the git user and press enter. If everything worked correctly
you should now be connected to your other machine (exciting isn’t it).

1.3 Making things safer

Now as we stand anyone with your ssh address and password can log into your machine. That might seem okay but it allows people to sit all day attempting to log in until they finally crack your password. A better approach is to
use RSA public keys. This method is a little more involved and once set up it means you cant log in from any old machine without adding a new key but it is by far the safest option, it also means on trusted machines you don’t have to type in your password
when you ssh. It’s really up to you whether you do this or not, if it doesn’t sound like your cup of tea then you can just skip this step.
First thing we need to do is open up terminal on our trusted machine (not the one used as a sever just yet). If you have used ssh before for github or anything like that you may already have a public key set up, for those that
dont you’ll need to type the following into the terminal. (you can use -dsa or -rsa)
>ssh-keygen -t rsa

You’ll then be prompted for a password, I cannot stress this enough
PUT IN A PASSWORD. Once that’s done you should be prompted for a save location, just hit enter and it will go to ~/.ssh by default. You should then see your public key printed nicely and it will have been saved to
~/.ssh/id_rsa.pub

(unless you gave it a file name). Now we need to copy this public key onto the other machine. Type the following commands into the terminal to do so. (If you already have an authorized_keys file you should use the cat command instead
to append it to the existing file)
>ssh git@yourOtherMachineName mkdir .ssh
>scp ~/.ssh/id_rsa.pub git@yourOtherMachineName:.ssh/authorized_keys

Now we need to log into the server and tell it to stop accepting passwords. Type in the following commands into the terminal on your non server machine (or if your on the server machine just skip the ssh line).
> ssh git@yourOtherMachineName

type your password.
>cd /etc
>chmod 666 sshd_config
>vim sshd_config

So we are editing the sshd_config file (not ssh_config this is for logging into other machines not having people log into ours). now scroll through and make the following changes to the lines shows below (they’re
shown as the default setting). If you don’t know how to use vim I suggest having a quick read on how to use it before doing the following.
Change:
#PermitRootLogin yes

To (note no hash and changed from yes to no)
PermitRootLogin no

Remove the # from the following
#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile     .ssh/authorized_keys

Also from
#PasswordAuthentication no
#PermitEmptyPasswords no

and finally change
#UsePAM yes

to
UsePAM no

now save the file (:w) and log out of the machine (ctrl-D). Attempt to log back in and if it worked you shouldn’t have to enter your password. If you are able to try it on a different machine and you shouldn’t be allowed in at
all (because you haven’t added that machine’s key).

2.0 Actually setting up git

Now there are a few ways to install git and I’ll let you choose your favourite the main thing is you have git installed on both machines.

2.1 Setting up an empty repository on your server machine

So now that git is set up on both machines we need to set up a repository that you can push to. Ssh into your remote machine and choose/make a directory to store your repositories. In that folder run the following command to create
a new empty folder to store our repository in.
>mkdir newrepo.git
>cd newrepo.git
>git init --bare

Thats all there is to it! By using the –bare flag we are saying this is a git repository but only use it to store pushes, in other words nobody will be using this as a local repository.

2.1 Setting up your local repository

Now head back to your local machine and find a folder to store the local copy of the repo. Run the following commands to setup the repository, add a readme file and push it to your server.
>mkdir newrepo
>cd newrepo
>git init
>touch README
>git add README
>git commit -m "initial commit"
>git remote add origin git@yourOtherMachineName:/path/to/newrepo.git
>git push origin master

And assuming everything was done correctly your repository should have been pushed to the server and you can now use it the same way as you would a github or bitbucket repository.

3.0 Summary

So hopefully this will help someone one day to set things up, if it does feel free to comment below or what not. also if you get stuck anywhere you can leave a comment as well as I may have mistyped a few things here and there.
Another thing to note is this will only allow you to log in on your local network to allow external log in you will need to set up either a fixed IP for your router or configure a service like dyndns. These are both outside the
scope of the article but there is a bunch of info around the web on doing both of these things.
Other than that happy coding.

转自:http://blog.smitec.net/posts/setting-up-a-git-server-on-osx/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: