Centos6.4搭建Git服务

搭建Git服务器需要准备一台运行Linux的机器,本文以Centos6.4版系统为例搭建自己的Git服务。

准备工作:以root用户登陆自己的Linux服务器。

Part1:安装依赖库

[root@localhost ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
[root@localhost ~]# yum install  gcc perl-ExtUtils-MakeMaker

Part2:卸载旧版git

加入原先有用yum安装过git,则需要先卸载一下

[root@localhost ~]# yum remove git

Part3:下载源码

下载git-2.10.0.tar.gz 到 /usr/local/src

(查找git版本可以到https://www.kernel.org/pub/software/scm/git/下查看git的版本号自行选择下载)

查看版本方法:

[root@iZbp1ap7v4yegqdgzrh7cuZ ~]# wget -v https://www.kernel.org/pub/software/scm/git/

[root@iZbp1ap7v4yegqdgzrh7cuZ ~]# vi index.html

复制想下载的版本 --> Esc --> :q! --> 回车!

这里我选择下载git-2.10.0.tar.gz

[root@localhost ~]# cd /usr/local/src
[root@localhost ~]# wget https://www.kernel.org/pub/software/scm/git/git-2.10.0.tar.gz

Part4:解压、编译和安装

复制代码

[root@localhost ~]# tar -zvxf git-2.10.0.tar.gz

cd /usr/local/src/ 
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz 
tar -zxvf libiconv-1.14.tar.gz 
cd libiconv-1.14 
./configure --prefix=/usr/local/libiconv && make && sudo make install
cd /usr/local/src/git-2.10.0

make configure 

./configure --prefix=/usr/local/git --with-iconv=/usr/local/libiconv 

make 

make install

复制代码
Part5:将git目录加入PATH

[root@localhost ~]# echo 'export PATH=$PATH:/usr/local/git/bin' >> /etc/bashrc
[root@localhost ~]# source /etc/bashrc

安装成功后就可以查看到git版本了。

[root@localhost ~]# git --version
git version 2.10.0

Part6:创建git账号并设置密码

复制代码

[root@localhost ~]# useradd -m git
[root@localhost ~]# passwd git                 
Changing password for user git.
New password: 
BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.

复制代码
Part7:创建git仓库并初始化

[root@localhost ~]# mkdir -p /home/git/repositories/test.git
[root@localhost ~]# cd /home/git/repositories/test.git
[root@localhost test.git]# git --bare init
Initialized empty Git repository in /home/git/repositories/test.git/

Part8:给git仓库目录设置用户和用户组并设置权限

[root@localhost test.git]# chown -R git:git /home/git/repositories
[root@localhost test.git]# chmod 755 /home/git/repositories

如何改变目录的用户和用户组

改变所属群组:chgrp 这个指令就是change group的缩写。不过要记住,要被改变的群组名称必须要在/etc/group档案内存在才行,否则就会显示错误。

 View Code

Part9:限制git账号的ssh连接

查找git-shell所在目录

[root@localhost ~]# whereis git-shell
git-shell: /usr/src/git-2.10.0/git-shell

编辑passwd文件

[root@localhost ~]# vi /etc/passwd

找到这一行

git:x:500:500::/home/git:/bin/bash 

将最后的/bin/bash改为:git-shell的目录 /usr/src/git-2.10.0/git-shell 如下:

git:x:500:500::/home/git:/usr/src/git-2.10.0/git-shell

Esc --> :wq! --> 回车!
完成搭建,去克隆提交试试吧!

clone地址:

ssh://git@服务器ip地址:端口/home/git/repositories/test.git

附加:以后每次新建仓库时,只需执行上面Part7、8即可!

Part10:后续


配置git 用户和用户邮箱

git config --global user.name ""

git config --global user.email ""

移动项目代码

cp -r 项目备份目录/. 项目目录

设置项目用户和用户组

chown -R git:git 项目目录

提交项目到裸仓库

git add .

git commit -m "init"

git push origin master

使用钩子自动执行shell脚本进行同步项目代码

cd 裸仓库目录/hooks

vi post-receive

#!/bin/sh

cd 项目目录

unset GIT_DIR

git pull origin master

配置免秘钥登录

远程服务器设置

修改/etc/ssh/sshd_config

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile /root/.ssh/authorized_keys 

启用这三行,然后重启service sshd restart
设置.ssh目录权限

chmod 700 -R .ssh

本地设置

本地安装git bash

本地生成ssh秘钥

cd ~/.ssh

ssh-keygen -t rsa -C “739xxxxx@qq.com”

vi ~/.ssh/config

Host XXX
HostName xxx.xxx.xxx.xxx
Port 22
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa

服务器端认证

vi /root/.ssh/authorized_keys

粘贴本地生成的秘钥如id_rsa.pub


获取远程项目

git clone ssh://git@xxx.xxx.xxx.xxx/xxx/xxx/xxx.git

出现

fatal: Could not read from remote repository.错误

解决办法,修改sshd_config

AuthorizedKeysFile /home/git/.ssh/authorized_keys

修改/home/git 700

/home/git/.ssh 700

/home/git/.ssh/authorized_keys 644

/root/.ssh/id_rsa 600

/root/.ssh 700

设置后成功获取远程裸版本库

 git clone ssh://git@xxx.xxx.xxx.xxx/xxx/xxx/xxx.git
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三朝看客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值