CENTOS 7搭建GIT服务器

Centos 下构建私有git服务器

以下操作都是root账户

安装

第一步,安装git服务

yun install -y git

第二步,新建git用户

useradd git

第三步,禁止git用户,shell登录

修改/etc/passwd

git:x:1010:1010:,,,:/home/git:/bin/bash  改为  git:x:1010:1010:,,,:/home/git:/usr/bin/git/git-shell

第四步,创建证书登录

使用命令ssh-keygen -t rsa -C “[email protected]生成公钥,Windows可以通过git bash执行命令,然后找到id_rsa.pub把文件内容导入 /home/git/.ssh/authorized_keys,如果没有文件

cd /home/git

mkdir .ssh

touch .ssh/authorized_keys

第五步,初始化git仓库

在/home/git 下新建仓库目录

mkdir repository

新建仓库,赋予权限

git init --bare test.git

chown -R git:git test.git

第六步,克隆仓库

git clone git@server:/home/git/repository/test.git  # server 可以是域名也可以是ip看配置
Cloning into 'test'...
warning: You appear to have cloned an empty repository.

遇到的问题

上面的都是理想状态下的流程

端口问题

git默认是22端口,如果服务器有修改端口,执行上面的clone会报错

$ git clone [email protected]:/home/git/repository/test.git
Cloning into 'test'...
ssh: connect to host 111.111.111.111 port 22: Connection timed out
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

正确的方式

git clone ssh://[email protected]:1111/home/git/repository/test.git

权限问题

Cloning into 'xigoubao'...
fatal: '/home/git/repository/test.git' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

如果路径写错了,或者test.git拥有者和组不是git,会报这个错误

示例,路径写错

git clone ssh://[email protected]:1111/home/git/respository/test.git  # repository 拼错

git clone ssh://[email protected]:1111/repository/test.git   # 路径必须为相对于git用户的home目录路径  /home/git/repository/test.git

权限的话,执行命令就可以了

chown -R git.git test.git