Git服务器搭建

来源:互联网 发布:淘宝客是在哪里推广的 编辑:程序博客网 时间:2024/06/04 00:45

安装Git

yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-develyum install git

接下来我们 创建一个git用户组和用户,用来运行git服务:

groupadd git #创建git用户组adduser git -g git  #创建git用户并指定为git在用户组

创建证书登录

收集所有需要登录的用户的公钥,公钥位于id_rsa.pub文件(C:\Users\Administrator.ssh)中,把我们的公钥导入到/home/git/.ssh/authorized_keys文件里,一行一个。
如果没有该文件创建它:

cd /home/git/mkdir .sshchmod 700 .sshtouch .ssh/authorized_keys  #创建好文件后记得把公钥贴上去chmod 644 .ssh/authorized_keys

重要:加上这一步防止出现博主出现的问题

chown -R git:git /home/git

初始化Git仓库

首先我们选定一个目录作为Git仓库,假定是/hotdata/kehu/bak_repo/bak_repo.git,在/hotdata/kehu/bak_repo目录下输入命令

cd /hotdata/kehu/mkdir bak_repochown git:git bak_repo/cd bak_repo/git init --bare bak_repo.git  #将存储库视为裸库。如果git_dir环境                              #未设置,它被设置为当前工作目录

以上命令Git创建一个空仓库,服务器上的Git仓库通常都以.git结尾。然后,把仓库所属用户改为git:

chown -R git:git bak_repo.git

克隆仓库

回到本地操作

git clone  git@120.24.238.xxx:/hotdata/kehu/bak_repo/bak_repo.git

120.24.238.xxx 为 Git 所在服务器 ip ,你需要将其修改为你自己的 Git 服务 ip。
这样我们的 Git 服务器安装就完成了。这里有一个问题,因为博主的服务器SSH端口号不是默认的22,所以就会出现如下错误,因为git默认去找22端口了:

F:\wamp\wamp\www\bak_repo> git clone git@120.24.238.000:4600:/hotdata/kehu/bak_repo/bak_repo.gitCloning into 'bak_repo'...ssh: connect to host 120.24.238.170 port 22: Connection refusedfatal: Could not read from remote repository.Please make sure you have the correct access rightsand the repository exists.

怎么办呢???如何指定clone的端口号???

采用如下格式:

git clone ssh://git@hostname:port/.../xxx.git
git clone  ssh://git@120.24.238.000:4600/hotdata/kehu/bak_repo/bak_repo.git

端口号也加上了,还是报错。错误信息如下:

F:\wamp\wamp\www\bak_repo> git clone  ssh://git@120.24.238.100:4600/hotdata/kehu/bak_repo/bak_repo.gitCloning into 'bak_repo'...git@120.24.238.170's password:Permission denied, please try again.git@120.24.238.170's password:Permission denied, please try again.git@120.24.238.170's password:Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).fatal: Could not read from remote repository.Please make sure you have the correct access rightsand the repository exists.

咋回事。。。。。。

经过goole,参考链接,答案为:
SSH对公钥、私钥的权限和所有权的要求是非常严格的,总结如下:
1、下面两个目录的所有权必须是user,所属组也应该是user,权限必须为700
\home\user
\home\user.ssh
2、下面公钥文件的所有权必须是user,所属组也应该是user,权限必须为644
\home\user.ssh\authorized_keys
3、下面私钥文件的所有权必须是user,所属组也应该是user,权限必须是600
\home\user.ssh\id_rsa

而我的文件夹权限和所属组呢:
这里写图片描述

这里写图片描述

有两个文件权限不对。。

好吧,解决了上面提示的错误,又来了问题:

F:\wamp\wamp\www\bak_repo> git clone  ssh://git@120.24.238.170:4600/hotdata/kehu/bak_repo/bak_repo.gitCloning into 'bak_repo'...fatal: protocol error: bad line length character: This

咋回事呢?既然是要允许登录,那不就和很多git教程矛盾了吗?好吧,先clone下来再说

usermod -s /bin/bash git #允许git 登录,这样是不提示错误了,并且clone成功了

//==以下步骤如果导致不能clone,就用上面的命令恢复吧
接下来我们可以禁用 git 用户通过shell登录,可以通过编辑/etc/passwd文件完成。找到类似下面的一行:

git:x:516:516::/home/git:/bin/bash

改为

git:x:516:516::/home/git:/usr/bin/git-shell

git服务器安装参考文章

git远程仓库中看不到本地push的文件?

git服务器搭建

非22端口的push

 git push   ssh://git@120.24.238.000:4600/hotdata/kehu/bak_repo/icp_pro.git
0 0
原创粉丝点击