个人GIT服务器搭建Windows/Linux

===Windows===

自:https://www.cnblogs.com/sumuncle/p/6362697.html

Git在版本控制方面,相比与SVN有更多的灵活性,对于开源的项目,我们可以托管到Github上面,非常方便,但是闭源的项目就会收取昂贵的费用。

 

那么私有项目,如何用Git进行代码版本控制呢?我们可以自己构建Git服务器。一般来说,在Linux上搭建Git的教程比较多,但是如何在Windows Server平台下搭建Git服务器呢?

 

对于很多.NET用户来说,代码编写的工具是Visual Studio,该工具是不支持SSH协议的,通过搜索和对比,发现Gitblit可以很方便的搭建服务,他是用Java编写的,所有安装的前提是安装Java运行时环境。

 

第1步:下载Java并安装Java.我这里下载的是jdk1.7.0_79

 

第2步:配置Java环境变量

 

右键” 计算机” => ”属性” => ”高级系统设置” => ”高级” => “环境变量” => “系统变量”。

 

  1. 新建:变量名:JAVA_HOME;变量值:C:\Program Files\Java\jdk1.7.0_79【具体要根据你自己的安装路径,我的是安装在D盘的】

     

  2. 新建:变量名:CLASSPATH;变量值:%JAVA_HOME%/lib/dt.jar;%JAVA_HOME%/lib/tools.jar

     

     

  3. 添加:找到PATH变量,选择编辑。把%JAVA_HOME%/bin;%JAVA_HOME%/jre/bin添加到”变量值”的结尾处。

 

第3步:验证Java是否安装成功。

 

在命令窗口中,输入javac, 回车,如果出现如下信息,说明安装成功。

 

 

 

第4步:下载Gitblit.下载地址:http://www.gitblit.com/

 

 

 

第5步:解压缩下载的压缩包即可,无需安装。路径为 C:\gitblit-1.8.0

 

 

 

第6步:创建用于存储项目代码的文件夹。这里为G:\GitProject\webui

 

第7步:配置gitblit,文件在C:\gitblit-1.8.0\data下,打开gitblit.properties ,里面有 include = defaults.properties,说明配置信息在defaults.properties中

 

 

 

2.找到git.repositoriesFolder(资料库路径),赋值为G:\GitProject\webui。

 

 

 

3.找到server.httpPort,设定http协议的端口号

 

 

 

4.找到server.httpBindInterface,设定服务器的IP地址。这里就设定你的服务器IP。

 

 

 

5.找到server.httpsBindInterface,设定为localhost

 

 

 

另外注意看看server.shutdownPort ,其默认值为 8081,是否被占用,如果占用请修改。

 

 

6.保存,关闭文件。

 

第8步:运行gitblit.cmd 批处理文件。运行结果如下,运行成功。

 

 

 

在浏览器中输入:http://192.168.180.159:10010/ ,默认可以用admin和admin进行登录,然后改密即可。

 

 

 

当然,如果在windows中将gitblit的启动注册到windows服务中,可以省的每次都要运行gitblit.cmd。

 

设置 


SET ARCH=amd64(64位,32位机器为 x86)

 

设置 


Set CD=C:\gitblit-1.8.0,CD的值为gitblit的路径,将启动参数设为空值,采用默认的参数即可 ,--StartParams="" ^

 

 

 

然后,以管理员方式打开CMD,  运行批处理文件installService.cmd即可。

 
 如果没有提示异常, 则到 “服务“ 组件里就可以找到 ”gitblit“ 这个服务了。

 

 

 

在浏览器中输入:http://192.168.180.159:10010/,用管理员登录后,可以创建团队和用户,并配置权限

 

 

 

 

 

 

 

然后可以创建一个webui的版本库,单击Git下拉列表,可以查看ssh,http等网络地址:

 

 

 

由于visual studio 2015 Git不支持ssh,这里用http协议,打开vs2015的团队资源管理器

 

 

 

单击[克隆]按钮,然后再解决方案下单击[新建]

 

 

 

创建一个web网站,如下图:

 

 

 

 

 

 

切换到 解决方案资源管理器中,可以看到项目文件结构:

 

 

 

我们可以添加和编辑此项目文件:

 

 

 

下面需要将该项目提交到服务器上

 

 

 

 

 

 

 

 

 

 

 

此时,可以刷新http://192.168.180.159:10010/tree/webui.git 查看服务器目录结构:

 


===Linux===

自:https://www.cnblogs.com/dwj97/p/6559056.html

环境:

服务器 CentOS6.6 + git(version 1.7.1)

客户端 Windows10 + git(version 2.8.4.windows.1)

 ① 安装 Git

Linux 做为服务器端系统,Windows 作为客户端系统,分别安装 Git

服务器端:

1
#yum install -y git

安装完后,查看 Git 版本

1
2
[root@localhost ~] # git --version
git version 1.7.1

客户端:

下载 Git for Windows,地址:https://git-for-windows.github.io/

安装完之后,可以使用 Git Bash 作为命令行客户端。

安装完之后,查看 Git 版本

1
2
$ git --version
git version 2.8.4.windows.1

② 服务器端创建 git 用户,用来管理 Git 服务,并为 git 用户设置密码

1
2
3
4
[root@localhost home] # id git
id : git:无此用户
[root@localhost home] # useradd git
[root@localhost home] # passwd git

③ 服务器端创建 Git 仓库

设置 /home/data/git/gittest.git 为 Git 仓库

然后把 Git 仓库的 owner 修改为 git

1
2
3
4
5
[root@localhost home] # mkdir -p data/git/gittest.git
[root@localhost home] # git init --bare data/git/gittest.git
Initialized empty Git repository in  /home/data/git/gittest .git/
[root@localhost home] # cd data/git/
[root@localhost git] # chown -R git:git gittest.git/

④ 客户端 clone 远程仓库

进入 Git Bash 命令行客户端,创建项目地址(设置在 d:/wamp64/www/gittest_gitbash)并进入:

1
2
3
4
5
6
7
8
9
10
11
dee@Lenovo-PC MINGW64 /d
$ cd  wamp64 /www
 
dee@Lenovo-PC MINGW64 /d/wamp64/www
$ mkdir  gittest_gitbash
 
dee@Lenovo-PC MINGW64 /d/wamp64/www
$ cd  gittest_gitbash
 
dee@Lenovo-PC MINGW64 /d/wamp64/www/gittest_gitbash
$

然后从 Linux Git 服务器上 clone 项目:

1
$ git clone [email protected]: /home/data/gittest .git

  

当第一次连接到目标 Git 服务器时会得到一个提示:

The authenticity of host '192.168.56.101 (192.168.56.101)' can't be established.RSA key fingerprint is SHA256:Ve6WV/SCA059EqoUOzbFoZdfmMh3B259nigfmvdadqQ.Are you sure you want to continue connecting (yes/no)?

选择 yes:

Warning: Permanently added '192.168.56.101' (RSA) to the list of known hosts.

此时 C:\Users\用户名\.ssh 下会多出一个文件 known_hosts,以后在这台电脑上再次连接目标 Git 服务器时不会再提示上面的语句。

后面提示要输入密码,可以采用 SSH 公钥来进行验证。 

⑤ 客户端创建 SSH 公钥和私钥

1
$ ssh -keygen -t rsa -C

此时 C:\Users\用户名\.ssh 下会多出两个文件 id_rsa 和 id_rsa.pub

id_rsa 是私钥

id_rsa.pub 是公钥

⑥ 服务器端 Git 打开 RSA 认证

进入 /etc/ssh 目录,编辑 sshd_config,打开以下三个配置的注释:

1
2
3
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile . ssh /authorized_keys

保存并重启 sshd 服务:

1
[root@localhost ssh ] # /etc/rc.d/init.d/sshd restart

由 AuthorizedKeysFile 得知公钥的存放路径是 .ssh/authorized_keys,实际上是 $Home/.ssh/authorized_keys,由于管理 Git 服务的用户是 git,所以实际存放公钥的路径是 /home/git/.ssh/authorized_keys

在 /home/git/ 下创建目录 .ssh

1
2
3
4
5
[root@localhost git] # pwd
/home/git
[root@localhost git] # mkdir .ssh
[root@localhost git] # ls -a
. .. .bash_logout .bash_profile .bashrc .gnome2 .mozilla . ssh

然后把 .ssh 文件夹的 owner 修改为 git

1
2
3
4
5
6
7
8
9
10
11
[root@localhost git] # chown -R git:git .ssh
[root@localhost git] # ll -a
总用量 32
drwx------. 5 git git 4096 8月 28 20:04 .
drwxr-xr-x. 8 root root 4096 8月 28 19:32 ..
-rw-r--r--. 1 git git 18 10月 16 2014 .bash_logout
-rw-r--r--. 1 git git 176 10月 16 2014 .bash_profile
-rw-r--r--. 1 git git 124 10月 16 2014 .bashrc
drwxr-xr-x. 2 git git 4096 11月 12 2010 .gnome2
drwxr-xr-x. 4 git git 4096 5月 8 12:22 .mozilla
drwxr-xr-x. 2 git git 4096 8月 28 20:08 . ssh

⑦ 将客户端公钥导入服务器端 /home/git/.ssh/authorized_keys 文件

回到 Git Bash 下,导入文件:

1
$ ssh  [email protected] 'cat >> .ssh/authorized_keys'  < ~/. ssh /id_rsa .pub

需要输入服务器端 git 用户的密码

回到服务器端,查看 .ssh 下是否存在 authorized_keys 文件:

1
2
3
4
[root@localhost git] # cd .ssh
[root@localhost . ssh ] # ll
总用量 4
-rw-rw-r--. 1 git git 398 8月 28 20:08 authorized_keys

可以查看一下是否是客户端生成的公钥。

重要:

修改 .ssh 目录的权限为 700

修改 .ssh/authorized_keys 文件的权限为 600

1
2
3
[root@localhost git] # chmod 700 .ssh
[root@localhost git] # cd .ssh
[root@localhost . ssh ] # chmod 600 authorized_keys

⑧ 客户端再次 clone 远程仓库

1
$ git clone [email protected]: /home/data/git/gittest .git

查看客户端项目目录:

项目已经 clone 了。 

也可以使用 tortoiseGit 客户端来管理项目:

clone

⑨ 禁止 git 用户 ssh 登录服务器

之前在服务器端创建的 git 用户不允许 ssh 登录服务器

编辑 /etc/passwd

找到:

1
git:x:502:504:: /home/git : /bin/bash

修改为

1
git:x:502:504:: /home/git : /bin/git-shell

此时 git 用户可以正常通过 ssh 使用 git,但无法通过 ssh 登录系统。


猜你喜欢

转载自blog.csdn.net/pansaky/article/details/80913152