• Centos6.8搭建Git服务(git版本可选)


    搭建Git服务器需要准备一台运行Linux的机器,本文以Centos6.8纯净版系统为例搭建自己的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档案内存在才行,否则就会显示错误。

    • chgrp [-R] dirname/filename....
      
        改变档案拥有者:chown 这个指令就是change owner的缩写。使用者必须是已经存在的系统中的账号,也就是在/etc/passwd 这个档案中有记录的使用者名称才能改变。
      
      chown 的用途其实还是蛮多的,他还可以顺带直接修改群组的名称
      
      chown [-R] 账号名称 档案或者目录
      
      chown [-R] 账号名称:群组名称 档案或者目录
      
      选项或者参数
      
        -R :进行递归(recursive)的持续变更,亦即连同次目录下的所有档案都变更
      
        改变权限:chmod,但是权限的设定方法有两种,分别可以使用数字或者符号来进行权限变更。--摘录自鸟哥的linux私房菜
      
      数字类型改变档案权限
      
      Linux档案的基本权限就有九个,分别是owner/group/others三种身份各有自己的read/write/execute权限, 先复习一下刚刚上面提到的资料:档案的权限字元为:‘-rwxrwxrwx’, 这九个权限是三个三个一组的!其中,我们可以使用数字来代表各个权限,各权限的分数对照表如下:
      r:4
      w:2
      x:1
      每种身份(owner/group/others)各自的三个权限(r/w/x)分数是需要累加的,例如当权限为: [-rwxrwx---] 分数则是:
      owner = rwx = 4+2+1 = 7
      group = rwx = 4+2+1 = 7
      others= --- = 0+0+0 = 0
      所以等一下我们设定权限的变更时,该档案的权限数字就是770啦!变更权限的指令chmod的语法是这样的:
      [root@www ~]# chmod [-R] xyz 档案或目录
      选项与参数:
      xyz : 就是刚刚提到的数字类型的权限属性,为 rwx 属性数值的相加。
      -R : 进行递回(recursive)的持续变更,亦即连同次目录下的所有档案都会变更
      举例来说,如果要将.bashrc这个档案所有的权限都设定启用,那么就下达: 
      [root@www ~]# ls -al .bashrc
      
      -rw-r--r--  1 root root 395 Jul  4 11:45 .bashrc
      [root@www ~]# chmod 777 .bashrc
      [root@www ~]# ls -al .bashrc
      -rwxrwxrwx  1 root root 395 Jul  4 11:45 .bashrc
      那如果要将权限变成‘ -rwxr-xr-- ’呢?那么权限的分数就成为 [4+2+1][4+0+1][4+0+0]=754 啰!所以你需要下达‘ chmod 754 filename’。 另外,在实际的系统运作中最常发生的一个问题就是,常常我们以vim编辑一个shell的文字批次档后,他的权限通常是 -rw-rw-r-- 也就是664, 如果要将该档案变成可执行档,并且不要让其他人修改此一档案的话, 那么就需要-rwxr-xr-x这样的权限,此时就得要下达:‘ chmod 755 test.sh ’的指令啰!
      
      另外,如果有些档案你不希望被其他人看到,那么应该将档案的权限设定为例如:‘-rwxr-----’,那就下达‘ chmod 740 filename ’吧! 
      
      例题:
      将刚刚你的.bashrc这个档案的权限修改回-rw-r--r--的情况吧!
      答:
      -rw-r--r--的分数是644,所以指令为:
      chmod 644 .bashrc
      
      符号类型改变档案权限
      
      还有一个改变权限的方法呦!从之前的介绍中我们可以发现,基本上就九个权限分别是(1)user (2)group (3)others三种身份啦!那么我们就可以藉由u, g, o来代表三种身份的权限!此外, a 则代表 all 亦即全部的身份!那么读写的权限就可以写成r, w, x啰!也就是可以使用底下的方式来看:
      
      chmod	u
      g
      o
      a	+(加入)
      -(除去)
      =(设定)	r
      w
      x	档案或目录
      来实作一下吧!假如我们要‘设定’一个档案的权限成为‘-rwxr-xr-x’时,基本上就是:
      
      user (u):具有可读、可写、可执行的权限;
      group 与 others (g/o):具有可读与执行的权限。
      
      所以就是:
      [root@www ~]# chmod  u=rwx,go=rx  .bashrc
      # 注意喔!那个 u=rwx,go=rx 是连在一起的,中间并没有任何空白字元!
      [root@www ~]# ls -al .bashrc
      -rwxr-xr-x  1 root root 395 Jul  4 11:45 .bashrc
      那么假如是‘ -rwxr-xr-- ’这样的权限呢?可以使用‘ chmod u=rwx,g=rx,o=r filename ’来设定。此外,如果我不知道原先的档案属性,而我只想要增加.bashrc这个档案的每个人均可写入的权限, 那么我就可以使用:
      [root@www ~]# ls -al .bashrc
      -rwxr-xr-x  1 root root 395 Jul  4 11:45 .bashrc
      [root@www ~]# chmod  a+w  .bashrc
      [root@www ~]# ls -al .bashrc
      -rwxrwxrwx  1 root root 395 Jul  4 11:45 .bashrc
      
      而如果是要将权限去掉而不更动其他已存在的权限呢?例如要拿掉全部人的可执行权限,则:
      [root@www ~]# chmod  a-x  .bashrc
      [root@www ~]# ls -al .bashrc
      -rw-rw-rw-  1 root root 395 Jul  4 11:45 .bashrc
      知道 +, -, = 的不同点了吗?对啦! + 与 – 的状态下,只要是没有指定到的项目,则该权限‘不会被变动’, 例如上面的例子中,由于仅以 – 拿掉 x 则其他两个保持当时的值不变!多多实作一下,你就会知道如何改变权限啰! 这在某些情况底下很好用的~举例来说,你想要教一个朋友如何让一个程式可以拥有执行的权限, 但你又不知道该档案原本的权限为何,此时,利用‘chmod a+x filename’ ,就可以让该程式拥有执行的权限了。是否很方便?
      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 ""

    ---------------------------------------------------------2018.01.02------------------------------------------

    mkdir m

    cd m

    git init

    git remote add origin /home/repos/m.git
    git fetch
    git checkout master

    -------------------------------------------------------------end-----------------------------------------------

    移动项目代码

    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

    --------------------------------------------2018.01.02----------------------------------------

    chmod +x post-receive

    ------------------------------------------------end----------------------------------------------

     配置免秘钥登录

    远程服务器设置

    修改/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

  • 相关阅读:
    aes加密
    获取当前系统的版本号
    解决eclipse中出现Resource is out of sync with the file system问题
    Mac系统打开命令行终端及查看操作系统版本号的方法
    android短信拦截
    android权限大全
    mac系统下的常用命令
    android 中 系统日期时间的获取
    ubuntu tor浏览器
    Python中的random模块
  • 原文地址:https://www.cnblogs.com/jasonxu19900827/p/7820224.html
Copyright © 2020-2023  润新知