• 利用阿里云搭建私有Git服务器


    服务器系统:Centos 6 (查看centos版本命令:lsb_release -a

    客户端系统:Windows 7

    一、服务器端安装Git

    ==通常centos上使用yum源安装的git版本过低==

    1. 检查系统上是否已经安装git,若已有则卸载

    // 查看当前git版本
    # git --version
    git version 1.7.1
    
    // 卸载旧版本
    # yum remove -y git

    2. 安装依赖包,下载最新版本git源码

    # yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
    # wget https://github.com/git/git/archive/v2.13.2.tar.gz
    # tar zxf v2.13.2.tar.gz

    3. 安装git,配置环境变量

    # cd git-2.13.2
    # make prefix=/usr/local/git all
    # make prefix=/usr/local/git install
    # echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/bashrc
    # source /etc/bashrc    // 实时生效 

    4. 查看git版本号,正确显示则安装成功

    # git --version
    git version 2.13.2

    5. 若编译时报错如下

    libgit.a(utf8.o): In function `reencode_string_iconv':
    /usr/local/src/git-2.13.2/utf8.c:463: undefined reference to `libiconv'
    libgit.a(utf8.o): In function `reencode_string_len':
    /usr/local/src/git-2.13.2/utf8.c:524: undefined reference to `libiconv_open'
    /usr/local/src/git-2.13.2/utf8.c:535: undefined reference to `libiconv_close'
    /usr/local/src/git-2.13.2/utf8.c:529: undefined reference to `libiconv_open'
    collect2: ld returned 1 exit status
    make: *** [git-credential-store] Error 1 

    可以按照如下方式解决

    // 对之前git的make 操作进行 make clean
    # make clean
    # wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
    # tar zxf libiconv-1.14.tar.gz
    # cd libiconv-1.14
    # ./configure --prefix=/usr/local/libiconv
    # make && make install
    // 创建一个软链接到/usr/lib
    # ln -s /usr/local/lib/libiconv.so /usr/lib
    # ln -s /usr/local/lib/libiconv.so.2 /usr/lib

    然后

    # make configure
    # ./configure --prefix=/usr/local/git --with-iconv=/usr/local/libiconv/
    # make && make install
    # echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/bashrc
    # source /etc/bashrc

    6. 配置git 用户名及邮箱(客户机安装后也要配置,在这里略过windows的git安装过程)

    # git config --global user.name 'your name'
    # git config --global user.email 'your email address'

    二、服务器设置

    1、添加一个git用户

    # useradd git

    2、初始化一个项目目录为一个仓库

    su git
    //git的~的实际地址为/home/git
    cd ~
    //yourName为自定义的仓库名称
    mkdir yourName.git
    cd yourName.git
    git init --bare

     至此,我们的仓库完整地址为/home/git/yourName.git

    【问题】:建立远程仓库使用 git init 命令,也可以增加 --bare 参数。写不写 --bare 参数有什么区别呢?

    【答案】:

      我们知道,一般从远程 clone 下来的仓库会生成一个独立的目录,在这个目录下有当前分支最新版本的文件,同时还有一个 .git 文件夹,与 .git 同级的文件夹称为我们的“工作目录”,我们的修改都在这个目录中进行。而 .git 就是我们 Git 本地仓库的工作目录,我们 add 和 commit 的东西都会被提交到这个目录中。对 git init 命令添加 --bare 参数就表示初始化 Git 仓库的时候不要创建本地工作目录,所以相当于 .git 文件夹下的所有内容直接创建到当前目录下,而不是被放到 .git 目录下。在 Git 服务器上建立好仓库以后,用户就可以克隆这个仓库了。等等。。还没配置用户 SSH 公钥呢,这么就让用户去下载,肯定还是要输入密码才行的。

    3、在 Git 服务器上为用户配置 SSH 公钥

     还是先在 Git 服务器上使用 authorized_keys 文件来管理所有用户的 SSH 公钥。(密钥登录的方式比密码登录更安全、更便捷,注意保管好自己的私钥,下面会讲到如何生成秘钥对)

    git@Linux:~$ mkdir .ssh
    git@Linux:~$ touch .ssh/authorized_keys
    git@Linux:~$ chmod 600 .ssh/authorized_keys 
    git@Linux:~$

    注意:这里的authorized_keys跟配置好的centos的证书方式ssh登录不同(如已配置),我们git的证书文件路径为/home/git/.ssh/authorized_keys(ssh终端登录所用证书文件路径为/etc/ssh/authorized_keys,一般使用xshell或者putty等工具用的证书登录ssh所用的pub密钥信息都在里面)

    4、打开服务器的RSA认证

    # vim /etc/ssh/sshd_config
    // 找到下面3行并去掉注释
     1. RSAAuthentication yes     
     2. PubkeyAuthentication yes     
     3. AuthorizedKeysFile  .ssh/authorized_keys

     重启sshd

    service sshd restart

    5、为安全起见禁用git用户shell登录

    // 为安全起见,禁用 git 用户的 shell 登录
    # vim /etc/passwd
    // 修改 git 用户的 shell 为 git-shell,路径使用 which git-shell 查看
    // 找到如下一行
    git:x:1001:1001::/home/git:/bin/bash
    // 修改成如下
    git:x:1001:1001::/home/git:/usr/local/git/bin/git-shell

     重启sshd服务

    service sshd restart

    三、客户端开始使用

    1、打开git bash

    2、生成秘钥对

      2.1 客户机执行以下命令将在windows的“用户目录/.ssh”下得到秘钥对

    cd ~/.ssh
    ssh-keygen -t rsa -C “youremail@example.com” 

      2.2上传公共秘钥到git服务器有以下2种方式:

      ①复制到git服务器的/home/git/.ssh/authorized_keys文件末尾中;

      ②通过ftp等方式上传后,执行以下命令:

    cat 源秘钥文件路径 >> /home/git/.ssh/authorized_keys


    2、任意新建一个工作区文件夹

    3、执行clone命令(输入自己的IP地址,端口默认为22,如有不同就加上去)

    git clone git@ip:/home/git/yourname.git

    4、随便新建个文件

    5、提交

    cd 项目文件下下
    git add .
    git commit -m "本次提交的备注"
    git push

    6、服务器端验证是否上传成功

    cd /home/git/yourName.git/branches
    git log

    成功信息:

    commit 087966c9f3f73f4aee153213213212132132ac191a7 (HEAD -> master)
    Author: upLoadUserName <yourEmailAddress>
    Date:   Tue Oct 9 08:59:21 2018 +0800

    【遇到的坑】

    1、回到家git clone一下发觉这玩意儿 Permission denied我了!

    解决办法:再次百度鼓捣一趟,原来是的ssh-keygen的时候自定义了密钥的文件名,保持默认回车下去生成的id_rsa能够正常使用,ssh -v 服务器IP 一下发现可能客户端的git默认使用了id_rsa的私钥,待深入学习git考究(原谅我懒人不看git官网原著,专门看百度出来的博客被坑)

    2、git push报错1

    $ git push
    Enter passphrase for key '/c/Users/PC-name/.ssh/id_rsa':
    Enumerating objects: 145, done.
    Counting objects: 100% (145/145), done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (134/134), done.
    fatal: sha1 file '<stdout>' write error: Broken pipe
    error: remote unpack failed: unable to create temporary object directory
    error: failed to push some refs to 'git@youripaddr:survey.git'

    缓存区不够大,搞大它

    $ git config http.postBuffer 52428800

    3、git push报错2

    $ git push
    Enter passphrase for key '/c/Users/hp206/.ssh/id_rsa':
    Enumerating objects: 145, done.
    Counting objects: 100% (145/145), done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (134/134), done.
    Writing objects: 100% (145/145), 103.16 KiB | 685.00 KiB/s, done.
    Total 145 (delta 64), reused 0 (delta 0)
    error: remote unpack failed: unable to create temporary object directory
    To youripaddr:yourprojectname.git
     ! [remote rejected] master -> master (unpacker error)
    error: failed to push some refs to 'git@youripaddr:survey.git'

    权限的问题,因为不是服务器git用户创建的目录
    到服务器git目录下

    chown -vR git *

    完美!

    参考文章:

    1.搭建私有git服务器进行版本控制
    2.向git服务器添加shh公钥
    3.搭建Git服务器遇到的那些坑

  • 相关阅读:
    iOS技术框架构和更新版本的技术特性
    iOS第三方常用类库
    查看MySQL系统变量的命令
    解决WIN7下VMWARE虚拟机无法上网问题
    MySQL性能优化的最佳20+条经验
    PHP高效率写法(详解原因)
    google官方建议使用的网站性能测试工具
    ETL工具-KETTLE教程 实例实战4----转换(值映射、列转行,增加常量、增加序列等)
    ETL工具-KETTLE教程实例实战2----环境介绍
    ETL工具-KETTLE教程实例实战1----术语和定义
  • 原文地址:https://www.cnblogs.com/yominhi/p/9759246.html
Copyright © 2020-2023  润新知