Linux 學習筆記(二):搭建我的Git服務器

Git的安裝

詳細學習能夠到:Git 官方教程html

依賴安裝

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

Git 版本:git version 1.8.3.1git

編譯安裝

Git Hub 選擇 release版本github

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

進行下載、解壓、配置、編譯、安裝web

[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 沒什麼區別了shell

[root@centos7 ~] git --version

Git 版本:git version 2.20.0-rc0vim

  • 建立 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',與免密登陸違背了segmentfault

[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」進過公鑰解密與原隨機數進行比對從而完成一次認證過程windows

  • 測試 publickey 方式登陸
[root@centos7 ~] ssh -vv git@104.199.134.0  #追溯兩層錯誤

若是出現錯誤,根據 debug 的關鍵字搜索。其中大部分緣由能夠分爲兩種:centos

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

相關文章
相關標籤/搜索