您的位置:首页 > 其它

ubuntu14.04.1虚拟机搭建git服务器

2016-08-20 15:16 375 查看
http://blog.csdn.net/tommy_wxie/article/details/38779667

http://www.cnblogs.com/yshyee/p/4288465.html

https://segmentfault.com/a/1190000002645623

更新系统软件库的索引文件

ubuntu:sudo apt-get update

安装软件

ubuntu:sudo apt-get install git-core openssh-server openssh-client

安装Python的setuptools

ubuntu:sudo apt-get install python-setuptools

安装gitosis

ubuntu:git config –global user.name “myname”

ubuntu: git config –global user.email “**@gmail.com”

ubuntu:git clone https://github.com/res0nat0r/gitosis.git

ubuntu:cd ~/gitosis/

ubuntu:~/gitosis$ sudo python setup.py install

创建一个账户(git)作为git服务器的管理员,可以管理其他用户的项目权限

ubuntu:~/gitosis$ sudo useradd -m git

ubuntu:~/gitosis$ sudo passwd git

在/home目录下创建一个项目仓库存储点,并设置只有git用户拥有所有权限,其他用户没有任何权限

ubuntu:~/gitosis$ sudo mkdir /home/gitrepository

ubuntu:~/gitosis$ sudo chown git:git /home/gitrepository/

ubuntu:~/gitosis$ sudo chmod 700 /home/gitrepository/

由于gitosis默认状态下会将仓库放在用户的repositories目录下,例如git用户的仓库地址默认在/home/git/repositories/目录下,这里我们需要创建一个链接映射。让他指向我们前面创建的专门用于存放项目的仓库目录/home/gitrepository。

ubuntu:~/gitosis$ sudo ln -s /home/gitrepository /home/git/repositories

这里我将在服务器端生成ssh公钥,如果想在其他机器上管理也可以在其他机器上生成一个ssh的公钥

ubuntu:/home/git$ ssh-keygen -t rsa

初始化 gtosis ,需要通过自己的公钥来运行gitosis-init

ubuntu:/home/git$ cp /home/yuanhcn/.ssh/id_rsa.pub /tmp/

ubuntu:/home/git$ sudo -H -u git gitosis-init < /tmp/id_rsa.pub

结果:

Initialized empty Git repository in /home/gitrepository/gitosis-admin.git/

Reinitialized existing Git repository in /home/gitrepository/gitosis-admin.git/

gitosis主要是通过gitosis-admin.git仓库来管理一些配置文件的,如用户权限的管理。这里我们需要对其中的一个post-update文件添加可执行的权限

ubuntu:/home/git$ sudo chmod 755 /home/gitrepository/gitosis-admin.git/hooks/post-update

使用git账户在服务器上创建一个目录(mytestproject.git)并初始化成git项目仓库

ubuntu:/home/git$ su git

git@ubuntu:~$ cd /home/gitrepository

git@ubuntu:/home/gitrepository$ mkdir mytestproject.git

git@ubuntu:/home/gitrepository$ git init –bare

git@ubuntu:/home/gitrepository$ exit

首先需要在前面生成ssh公钥(用来初始化gitosis)的机器上将gitosis-admin.git的仓库clone下来。在客户端机器上新建一个目录用于存放gitosis-admin.git仓库

ubuntu:~$ mkdir gitadmin

ubuntu:~$ cd gitadmin/

ubuntu:~/gitadmin$ git clone git@192.168.67.130:gitosis-admin.git

clone下来会有一个gitosis.conf的配置文件和一个keydir的目录。gitosis.conf用于配置用户的权限信息,keydir主要用户存放ssh公钥文件(一般以“用户名.pub”命名,gitosis.conf配置文件中需使用相同用户名),用于认证请求的客户端机器。

现在让需要授权的用户使用前面的方式各自在其自己的机器上生成相应的ssh公钥文件,管理员把他们分别按用户名命名好,复制到keydir目录下。

继续编辑gitosis.conf文件

[gitosis]

[group gitosis-admin] ####管理员组

members = charn@ubuntu ####管理员用户名,需要在keydir目录下找到相应的.pub文件,多个可用空格隔开(下同)

writable = gitosis-admin####可写的项目仓库名,多个可用空格隔开(下同)

[group testwrite] ####可写权限组

members = zhangsan####组用户

writable = mytestproject####可写的项目仓库名

[group testread]

####只读权限组

members =lisi####组用户

readonly= mytestproject####只读项目仓库名

因为这些配置的修改只是在本地修改的,还需要推送到服务器中才能生效。

ubuntu:~/gitadmin/gitosis-admin$ git add .

ubuntu:~/gitadmin/gitosis-admin$ git commit -am “add a user permission”

ubuntu:~/gitadmin/gitosis-admin$ git push origin master

如果新增的用户不能立即生效,这时候需要重新启动一下sshd服务

ubuntu:~/gitadmin/gitosis-admin$ sudo /etc/init.d/ssh restart

现在,服务端的git就已经安装和配置完成了,接下来就需要有权限的组成员在各自的机器上clone服务器上的相应

项目仓库进行相应的工作了。

客户端(windows)使用git

下载安装windows版本的git客户端软件,下载地址:http://msysgit.github.io/

安装完成后右键菜单会出现几个git相关的菜单选项,我们主要使用其中的git bash通过命令行来进行操作。

在本地新建一个目录,使用git初始化这个目录,然后再里面新建一个文本文件用于测试,最后关联到git服务器仓库

中的相关项目,最后上传本地版本到服务器。

[sql] view plaincopyprint?在CODE上查看代码片派生到我的代码片

$ mkdir testgit

$ cd testgit

$ git init

$ echo “this is a test text file,will push to server” > hello.txt

$ git add .

$ git commit -am “init a base version,add a first file for push to server”

$ git remote add origin git@serverip:mytestproject.git

$ git push origin master

这样服务端就创建好了一个mytestproject.git的仓库的基础版本了,现在其他组员只要从服务端进行clone就可以

了。

window下面进入到需要克隆的本地目录下面右键选择git bash选项,输入

[sql] view plaincopyprint?在CODE上查看代码片派生到我的代码片

$ git clone git@serverip:mytestproject.git

就可以把项目clone到本地仓库了。

下面进行简单的修改和提交操作

[sql] view plaincopyprint?在CODE上查看代码片派生到我的代码片

$ cd mytestproject

$ echo “this is another text file created by other” >another.txt

$ git add .

$ git commit -am “add a another file by other”

$ git push origin master

1、设置Git的user name和email:(如果是第一次的话)

$ git config --global user.name "humingx"
$ git config --global user.email "humingx@yeah.net"


2、生成密钥

$ ssh-keygen -t rsa -C "humingx@yeah.net"


连续3个回车。如果不需要密码的话。

最后得到了两个文件:id_rsa和id_rsa.pub。

如果不是第一次,就选择overwrite.

3、添加密钥到ssh-agent

确保 ssh-agent 是可用的。ssh-agent是一种控制用来保存公钥身份验证所使用的私钥的程序,其实ssh-agent就是一个密钥管理器,运行ssh-agent以后,使用ssh-add将私钥交给ssh-agent保管,其他程序需要身份验证的时候可以将验证申请交给ssh-agent来完成整个认证过程。

# start the ssh-agent in the background
eval "$(ssh-agent -s)"
Agent pid 59566


添加生成的 SSH key 到 ssh-agent。

$ ssh-add ~/.ssh/id_rsa


4、登陆Github, 添加 ssh 。

把id_rsa.pub文件里的内容复制到这里

5、测试:

$ ssh -T git@github.com


你将会看到:

The authenticity of host 'github.com (207.97.227.239)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)?


选择 yes

Hi humingx! You've successfully authenticated, but GitHub does not provide shell access.


如果看到Hi后面是你的用户名,就说明成功了。

6、修改.git文件夹下config中的url。

修改前

[remote "origin"]
url = https://github.com/humingx/humingx.github.io.git fetch = +refs/heads/*:refs/remotes/origin/*


修改后

[remote "origin"]
url = git@github.com:humingx/humingx.github.io.git
fetch = +refs/heads/*:refs/remotes/origin/*


7、发布
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  git ubuntu