Linux 学习笔记(二):搭建个人Git服务器

Git的安装

详细学习可以到:Git 官方教程

依赖安装

[root@centos7 ~] yum install git
[root@centos7 ~] git --version

Git 版本:git version 1.8.3.1

编译安装

Git Hub 选择 release版本

Git 的依赖 curl-devel expat-devel gettext-devel openssl-devel zlib-devel

进行下载、解压、配置、编译、安装

[root@centos7 ~] yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
[root@centos7 ~] wget https://codeload.github.com/git/git/tar.gz/v2.20.0-rc0
[root@centos7 ~] tar -xvf v2.20.0-rc0
[root@centos7 ~] make configure
[root@centos7 ~] ./configure
[root@centos7 ~] make && make install

此处使用 ./configure 直接使用默认配置,实际上和 yum 没什么区别了

[root@centos7 ~] git --version

Git 版本:git version 2.20.0-rc0

  • 创建 Git 用户
为了访问的便捷,我们使用 git 用户的身份来创建代码仓库,实际上使用任何用户都是可以的,区别在于在 git clone 的时候,需将 git@server 改成别的用户名
[root@centos7 ~] adduser git
[root@centos7 ~] passwd git
  • Git 的 SSH 协议使用 SSH key 免密鉴权
The authorized_keys file in SSH specifies the SSH keys that can be used for logging into the user account for which the file is configured.
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile      .ssh/authorized_keys

所以我们明白了 authorized_keys 文件是用以保存已授权的客户端公钥。具体参见: SSH原理
如果在创建「ssh-key」的时候使用了「passphrase」 ,那么使用「SSH」的「method: publickey」方式进行连接时会报出一个提示:Enter passphrase for key '/home/git/.ssh/id_rsa',与免密登录违背了

[root@centos7 ~] su git
[root@centos7 ~] ssh-keygen -t rsa
[root@centos7 ~] cat /home/git/.ssh/id_rsa.pub >> /home/git/.ssh/authorized_keys
[root@centos7 ~] chmod 700 /home/git/.ssh

+---[RSA 2048]----+    
|o. +XE=o.        |    # -t:指定生成密钥类型(rsa、dsa、ecdsa等)
|o.=+=*=          |    # -P:指定passphrase,用于确保私钥的安全    
| . . o B o       |    # -f:指定存放密钥的文件  
|  .   = .        |
|o+.oo* o o       |    
|o o ..+ + .      |    # authorized_keys    #保存已授权的客户端公钥
|.  o +.oS. .     |    # known_hosts        #保存已认证的远程主机ID
|.   o B.+ .      |    # id_rsa.pub         #保存公钥
|       .         |    # id_rsa             #保存私钥
+----[SHA256]-----+

authorized_keys添加需要授权 shell 登录用户的公钥,首先「server」向「client」发送一个随机数值,用户在「client」返回使用密钥加密随机数后的密文在「server」进过公钥解密与原随机数进行比对从而完成一次认证过程

  • 测试 publickey 方式登录
[root@centos7 ~] ssh -vv git@104.199.134.0  #追溯两层错误

如果出现错误,根据 debug 的关键字搜索。其中大部分原因可以分为两种:

1. SSH 配置问题
2. 文件权限问题
  • 基本配置
StrictModes                     no                           #关闭严格校验
    
    RSAAuthentication               yes                          #允许RSA认证
        
    PubkeyAuthentication            yes                          #允许公钥认证
    
    AuthorizedKeysFile              .ssh/authorized_keys         #ssh文件位置
    
    PasswordAuthentication          yes                          #允许密码认证
  • 正常使用的:sshd_config
Protocol                         2

    SyslogFacility                   AUTH

    LogLevel                         debug

    PermitRootLogin                  yes

    RSAAuthentication                yes

    PubkeyAuthentication             yes

    AuthorizedKeysFile               .ssh/authorized_keys

    PasswordAuthentication           yes

    ChallengeResponseAuthentication  no

    GSSAPIAuthentication             yes

    GSSAPICleanupCredentials         yes

    UsePAM                           yes

    AcceptEnv                        LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES

    AcceptEnv                        LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT

    AcceptEnv                        LC_IDENTIFICATION LC_ALL LANGUAGE

    AcceptEnv                        XMODIFIERS

    X11Forwarding                    yes

    Subsystem                        sftp    /usr/libexec/openssh/sftp-server

更改文件权限

[root@centos7 ~] chmod 700 /home/git/.ssh
[root@centos7 ~] chmod 600 /home/git/.ssh/authorized_keys
  • 正常登录效果
[root@centos7 ~]$ ssh git@104.199.134.0

The authenticity of host '104.199.134.0 (104.199.134.0)' can't be established.
ECDSA key fingerprint is SHA256:49g0X6kyudjfjCa/QBoZwf0mbPZnFphYjMRV/LrQPpQ.
ECDSA key fingerprint is MD5:cc:ef:83:ab:e4:33:00:9e:0f:62:87:df:62:01:73:62.
Are you sure you want to continue connecting (yes/no)?

登录后 .ssh 下就生成了 known_hosts

[root@centos7 ~]$ vim /home/git/.ssh/known_hosts

104.199.134.0 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABEBPA/O+a8s92uinv3NKnVzGqaohbX6vqDVGMoE5vs+PuT7NXivy5aSkRutROcN/H8AhnBLaK5HWGsqwBRw8FXgSY=
  • 禁用 shell 登录
因为git用户是专门用来上传代码的,所以禁用git用户的登录权限

将 /sbin/nologin 作为 git 用户的登录 shell,即禁止 git用户 shell 登录

[root@centos7 ~] usermod -s /sbin/nologin git   #禁止shell登录
[root@centos7 ~] usermod -s /bin/bash git       #恢复默认shell

本地仓库

[root@centos7 ~] su - git
[root@centos7 ~] git init sea.git

git init 用来搭建本地仓库,在工程中使用的 commit 保存到本地仓库

远程仓库

[root@centos7 ~] su - git
[root@centos7 ~] git init --bare sea.git

git init --bare 用来搭建远程仓库,在工程中使用 push 推送到远程仓库

  • 初始化 Git 仓库

前面我们创建了 git 用户,那么 git 用户的 home 目录变可以用来当仓库路径

这里的 git 仓库便是远程仓库了,用户们使用 push 命令将更新推送到远程仓库,使用 --bare 选项运行 git init 来建立一个裸仓库

仓库后缀都是 .git

创建远程仓库目录并初始化了空的仓库

[root@centos7 ~] cd /home/git/
[root@centos7 ~] mkdir sea.git
[root@centos7 ~] git init --bare sea.git
  • 普通仓库与裸仓库的区别

git 不仅可以做本地开发的版本控制,更多还用与团队写作的迭代开发。普通仓库保存着工程代码、版本历史,远程的裸仓库即版本库仅包含记录着版本历史的文件

使用 git init –bare 方法创建一个所谓的裸仓库,之所以叫裸仓库是因为这个仓库只保存git历史提交的版本信息,而不允许用户在上面进行各种git操作,如果你硬要操作的话,只会得到下面的错误: This operation must be run in a work tree

项目拉取

协议介绍

  • 本地协议

所谓的远程仓库在该协议(Local protocol)中的表示,就是硬盘上的另一个目录。这常见于团队每一个成员都对一个共享的文件系统(例如 NFS)拥有访问权,或者比较少见的多人共用同一台电脑的情况。

  • Git 协议

用 Git 协议作为访问项目的唯一方法通常是不可取的。一般的做法是,同时提供 SSH 接口,让几个开发者拥有推送(写)权限,其他人通过 git:// 拥有只读权限。

这是一个包含在 Git 软件包中的特殊守护进程; 它会监听一个提供类似于 SSH 服务的特定端口(9418),而无需任何授权。打算支持 Git 协议的仓库,需要先创建 git-daemon-export-ok 文件 — 它是协议进程提供仓库服务的必要条件 — 但除此之外该服务没有什么安全措施。要么所有人都能克隆 Git 仓库,要么谁也不能。这也意味着该协议通常不能用来进行推送。你可以允许推送操作;然而由于没有授权机制,一旦允许该操作,网络上任何一个知道项目 URL 的人将都有推送权限。
  • SSH 协议

SSH 也是唯一一个同时支持读写操作的网络协议。也是默认协议

[root@centos7 ~] git clone ssh://user@server/project.git
[root@centos7 ~] git clone user@server:project.git
  • HTTP/S 协议

HTTP 或 HTTPS 协议的优美之处在于架设的简便性。基本上,只需要把 Git 的裸仓库文件放在 HTTP 的根目录下,配置一个特定的 post-update 挂钩(hook)就可以搞定(Git 挂钩的细节见第 7 章)。此后,每个能访问 Git 仓库所在服务器上 web 服务的人都可以进行克隆操作。

代码拉取

  • 使用密码

操作环境:windows clone 虚拟机的远程仓库

[root@centos7 ~] git clone git@192.168.108.128:/home/git/sea.git

# Cloning into 'sea'...
# git@192.168.108.128's password:
# warning: You appear to have cloned an empty repository.
  • SSH 拉取
[root@centos7 ~] git clone git@164.109.134.117:/home/git/sea.git

# Cloning into 'test1.git'...
# done.
# warning: You appear to have cloned an empty repository.
  • 非默认端口的SSH协议拉取代码
[root@centos7 ~] git clone ssh://git@193.29.97.24:14726/home/git/star.git
  • 设置配置信息
[root@centos7 ~] git config --global user.email "sea.star.com"
[root@centos7 ~] git config --global user.name "sea"
[root@centos7 ~] git config --global -- list

# user.email=sea@star.com
# user.name=sea

项目推送

  • 一个项目push到多个远程Git仓库

查看远程仓库

[root@centos7 ~] git remote -v

# origin    git@193.29.97.24:/home/git/sea.git (fetch)
# origin    git@193.29.97.24:/home/git/sea.git (push)
  • 在远程仓库建立对应的裸仓库
[root@centos7 ~] git init --bare star.git

添加远程仓库

[root@centos7 ~] git remote add star ssh://git@193.29.97.24:14726/home/git/star.git

再次查看远程仓库

[root@centos7 ~] git remote -v

# origin    git@193.29.97.24:/home/git/sea.git (fetch)
# origin    git@193.29.97.24:/home/git/sea.git (push)
# upstream    ssh://git@193.29.97.24:14726/home/git/star.git (fetch)
# upstream  ssh://git@193.29.97.24:14726/home/git/star.git (push)

删除远程仓库

[root@centos7 ~] git remote rm upstream
[root@centos7 ~] git remote -v

# origin    git@193.29.97.24:/home/git/sea.git (fetch)
# origin    git@193.29.97.24:/home/git/sea.git (push)
这种方式的多个远程仓库需要分别推送

绑定多个仓库

[root@centos7 ~] git remote set-url --add origin ssh://193.29.97.25:/home/git/taskv2.git
[root@centos7 ~] git remote -v

# origin    git@193.29.97.24:/home/git/sea.git (fetch)
# origin    git@193.29.97.24:/home/git/sea.git (push)
# origin    git@193.29.97.25:/home/git/sea.git (push)

提交修改,推送到了两个远程仓库去了

[root@centos7 ~]$ git push origin master

#     Enumerating objects: 4, done.
#     Counting objects: 100% (4/4), done.
#     Compressing objects: 100% (2/2), done.
#     Writing objects: 100% (3/3), 271 bytes | 271.00 KiB/s, done.
#     Total 3 (delta 1), reused 0 (delta 0)
#     To ssh://104.199.134.0:/home/git/star.git
#        b0785b2..5db23a4  master -> master

#     Enumerating objects: 4, done.
#     Counting objects: 100% (4/4), done.
#     Compressing objects: 100% (2/2), done.
#     Writing objects: 100% (3/3), 271 bytes | 271.00 KiB/s, done.
#     Total 3 (delta 1), reused 0 (delta 0)
#     To ssh://93.179.97.24:27038/home/git/star.git
#        b0785b2..5db23a4  master -> master
  • 查看所有分支
git branch -a

  bug
  doc
* master
  remotes/origin/bug
  remotes/origin/doc
  remotes/origin/master
  • 删除远程分支
git push branch :branchname

To ssh://127.0.0.1/home/git/repository.git
 - [deleted]           branchname
  • 关于 Git 版本的选择
Git的升级策略大多是安全更新,少有重大新特性更新,升级可能会引入系统失效陷阱,由此浪费的时间精力完全不必要。

相关文章

Linux 学习笔记(一):内网穿透
Linux 学习笔记(二):搭建个人Git服务器
Linux 学习笔记(三):Ubuntu 操作系统
Linux 学习笔记(四):Docker
Linux 学习笔记(五):Redis
Linux 学习笔记(六):Linux

相关推荐