首页 > 代码库 > Linux 下搭建git 服务器

Linux 下搭建git 服务器

环境: 服务器 centos7.3 +git1.8.3.1
客户端 centos7.2 +git1.8.3.1
服务器配置;
1.安装git yum install git -y
2.创建git用户
    [root@localhost ~]# id git
    id: git: no such user
    [root@localhost ~]# useradd git
    [root@localhost ~]# id git
    uid=1001(git) gid=1001(git) groups=1001(git)

    为了安全起见,可以只允许git使用git命令

        sed -i ‘/^git/s#/bin/bash#/usr/bin/git-shell#g‘ /etc/passwd
3.在服务端创建git仓库
把/data/git/test.git设置为GIT仓库,并修改属主属组为git
    [root@localhost ~]# mkdir -p /data/git/test.git
    [root@localhost ~]# git init --bare /data/git/test.git #--bare参数创建目录结构
    Initialized empty Git repository in /data/git/test.git/
    [root@localhost ~]# chown -R git.git /data/git/test.git
    [root@localhost ~]# cd /data/git/test.git/
    [root@localhost test.git]# ll
    total 12
    drwxr-xr-x. 2 git git 6 Jun 28 22:57 branches
    -rw-r--r--. 1 root root 66 Jun 28 22:58 config
    -rw-r--r--. 1 git git 73 Jun 28 22:57 description
    -rw-r--r--. 1 git git 23 Jun 28 22:57 HEAD
    drwxr-xr-x. 2 git git 242 Jun 28 22:57 hooks
    drwxr-xr-x. 2 git git 21 Jun 28 22:57 info
    drwxr-xr-x. 4 git git 30 Jun 28 22:57 objects
    drwxr-xr-x. 4 git git 31 Jun 28 22:57 refs

客户端配置:
1.安装git yum install git -y
2.配置免密钥登录
    [root@localhost ~]#ssh-keygen -t rsa
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa):
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /root/.ssh/id_rsa.
    Your public key has been saved in /root/.ssh/id_rsa.pub.
    The key fingerprint is:
    ac:be:d1:12:37:89:1b:ca:52:8b:4c:b7:97:38:18:41 root@localhost.localdomain
    The key‘s randomart image is:
    +--[ RSA 2048]----+
    |   E                      |
    |  .                        |
    |   .                       |
    |    .    o .              |
    |   o o + S            |
    |   o B = O .         |
    |     = B B .          |
    |       . + o            |
    |         o.              |
    +--------------------+
    [root@localhost ~]# ssh-copy-id 192.168.138.133
    /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
    /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
    root@192.168.138.133‘s password:

    Number of key(s) added: 1

    Now try logging into the machine, with: "ssh ‘192.168.138.133‘"
    and check to make sure that only the key(s) you wanted were added.
3.创建本地仓库
    [root@localhost ~]# mkdir /git
    [root@localhost ~]# cd /git/
    [root@localhost git]# ll
    total 0
    [root@localhost git]# git clone 192.168.138.133:/data/git/test.git/
    Cloning into ‘test‘...
    warning: You appear to have cloned an empty repository.
    [root@localhost git]# ll
    total 0
    drwxr-xr-x. 3 root root 17 Jul 3 20:42 test

Linux 下搭建git 服务器