Linux 使用 Gitolite 架設 Git Server

單純使用 SSH 架設可見此篇: Linux 架設使用 SSH 共享存取的 Git Server

想要控管 User / Project 權限, 而且還想要控管 branch / tag 等讀寫權限, 則需要靠 Gitolite 等套件來協助.

  • gitolite - SSH-based gatekeeper for git repositories

此篇主要寫的是 Gitolite 架設, 若之前已經有依照上述文章架設共享存取的 Git Server, 而 Gitolite 也想用 git 的帳號來管理, 則需做下述動作先改回原始設定.

註: 此文會用 gitolite 的帳號來管理, 不會用 git 帳號, 所以不需要做下述更改的動作

  1. vim /etc/passwd

    git:x:1000:1000::/home/git:/usr/bin/git-shell
    改回
    git:x:1000:1000::/home/git:/bin/sh

  2. mv /home/git/.ssh/authorized_keys /home/git/.ssh/authorized_keys.bak

相關資料準備

  • 系統: Debian / Ubuntu Linux
  • Server: example.com
  • Project name: project_name
  • Gitosis (Git) Repository 位置: /var/lib/gitolite/repositories # Debian / Ubuntu Linux 套件預設位置
  • Group name: myteam

系統套件安裝

  • apt-get install gitolite git-core
  • 安裝完成會出現此訊息: No adminkey given - not initializing gitolite in /var/lib/gitolite.
  • 相關設定參考可見 /etc/gitolite/example.conf, /etc/gitolite/example.gitolite.rc

產生 SSH 公鑰

  1. ssh-keygen -t rsa # 產生 id_rsa, id_rsa.pub
  2. mv id_rsa ~/.ssh/ # 將 id_rsa 放在 ~/.ssh/ 內.
  3. scp id_rsa.pub example.com:/tmp/user1.pub # 將 id_rsa.pub 丟到 Server 上, 大家的 public key 都需要傳到 Server 上.
  4. scp id_rsa.pub example.com:/tmp/admin.pub # 管理者的 key 同 user key, 在此設為 admin.pub, 避免下述內容造成混淆.

Gitolite Server 架設

  1. ssh example.com # Git Server
  2. sudo su - gitolite
  3. gl-setup /tmp/admin.pub # 匯入管理者的 Public key. 注意: 此檔名即是帳號名稱, 不要使用 id_rsa.pub 當檔名, 建議用 "帳號.pub" 當檔名
  4. exit

Gitolite Server 設定專案、新增帳號

  1. Gitolite 的專案權限 / 帳號管理 是使用 Git 來管理, 專案名稱: gitolite-admin.git
  2. git clone [email protected]:gitolite-admin # 因為 Gitolite 是用 gitolite-admin.git 來管理, 所以需要抓下來修改、設定(未來所有管理也是如此)
  3. cd gitolite-admin # 會看到下述
    • conf/gitolite.conf # 設定檔, 設定誰可以讀寫哪個專案的 Repository
    • keydir # 目錄, 放每個帳號的 public key. 放置的檔案命名: user1.pub, user2.pub (user1, user2.. 為帳號名稱(檔名 = 帳號), 建議使用 "帳號.pub" 當檔名)

設定專案權限

  1. cd gitolite-admin
  2. vim conf/gitolite.conf # 會看到下述, 不要動他, 於最下方設定自己的 Group / 專案名稱即可.

    repo    gitolite-admin
    RW+     =   admin
    repo    testing
    RW+     =   @all

  3. 由此檔案 新增 / 修改後, commit + push 即可.

建立專案

  1. git clone [email protected]:testing # 對應 gitolite.conf 的 repo testing, 會出現下述訊息

    Cloning into testing...
    warning: You appear to have cloned an empty repository.

  2. cd testing
  3. touch readme
  4. git add .
  5. git commit -m 'add readme'
  6. git push origin master

新增帳號

  1. cd gitolite-admin
  2. cp /tmp/user1.pub keydir/user1.pub # 請依照實際帳號命名, 不要取 user1, user2
  3. cp /tmp/user1.pub keydir/[email protected] # 若相同帳號, 則使用 [email protected]
  4. cp /tmp/user2.pub keydir/user2.pub
  5. git add keydir/user1.pub keydir/[email protected] keydir/user2.pub
  6. git commit -m 'add user1, user1@machine, user2 public key'
  7. git push

gitolite.conf 更多設定條件

下述摘錄自: Gitolite 構建 Git 服務器 - 授權使用者建立屬於自己的空間 (User 下面可以建 N 個 Repository), 在此就不記載, 請自行詳見: 此文的 章節 2.4.3

# 取自 2.3.1 授權文件基本語法
@admin = jiangxin wangsheng

repo gitolite-admin
RW+    = jiangxin

repo ossxp/.+
C       = @admin
RW     = @all

repo testing
RW+                   =   @admin
RW      master        =   junio
RW+     pu            =   junio
RW      cogito$        =   pasky
RW      bw/           =   linus
-                        =   somebody
RW      tmp/           =   @all
RW      refs/tags/v[0-9] =   junio

# 取自 2.3.3 ACL
repo testing
RW+   = jiangxin @admin
RW    = @dev @test
R      = @all

gitolite.conf 語法說明

repo 語法
  • repo 語法: <權限> [零個或多個正規表示式批配的引用] = <user> [<user> ...]
  • 每條指令必須指定一個權限, 權限可以用下面任何一個權限的關鍵字: C, R, RW, RW+, RWC, RW+C, RWD, RW+D, RWCD, RW+CD
    • C : 建立
    • R : 讀取
    • RW : 讀取 + 寫入
    • RW+ : 讀取 + 寫入 + 對 rewind 的 commit 做強制 Push
    • RWC : 授權指令定義 regex (regex 定義的 branch、tag 等), 才可以使用此授權指令.
    • RW+C : 同上, C 是允許建立 和 regex 配對的引用 (branch、tag 等)
    • RWD : 授權指令中定義 regex (regex 定義的 branch、tag 等), 才可以使用此授權指令.
    • RW+D : 同上, D 是允許刪除 和 regex 配對的引用 (branch、tag 等)
    • RWCD : 授權指令中定義 regex (regex 定義的 branch、tag 等), 才可以使用此授權指令.
    • RW+CD : C 是允許建立 和 regex 配對的引用 (branch、tag 等), D 是允許刪除 和 regex 配對的引用 (branch、tag 等)
    • - : 此設定為不能寫入, 但是可以讀取
    • 註: 若 regex 不是以 refs/ 開頭, 會自動於前面加上 refs/heads/
群組
  • @all 代表所有人的意思
  • @myteam user1 user2 : user1, user2 都是屬於 myteam 這個群組

常用命令

下述全部都在 gitolite-admin.git 內操作

  • 新增帳號
    • cp /tmp/user1.pub keydir/user1.pub # 注意: 檔名要取 "帳號.pub"
  • 新增專案
    1. vim conf/gitolite.conf # 增加 repo, 例如:

      repo testing
      RW @all

    2. git clone [email protected]:testing
  • 設定專案
    • vim conf/gitolite.conf # 增加 repo, 設定讀寫群組、使用者的權限

相關網頁

作者: Tsung

對新奇的事物都很有興趣, 喜歡簡單的東西, 過簡單的生活.

在〈Linux 使用 Gitolite 架設 Git Server〉中有 30 則留言

  1. 你好:
    我在 "Gitolite Server 架設" 的第三步遇到這樣的訊息:

    The default settings in the rc file (/home/gitadmin/.gitolite.rc) are fine for most
    people but if you wish to make any changes, you can do so now.

    hit enter...
    我在這按Enter

    creating gitolite-admin...
    Initialized empty Git repository in /home/gitadmin/repositories/gitolite-admin.git/
    creating testing...
    Initialized empty Git repository in /home/gitadmin/repositories/testing.git/
    WARNING: a pubkey file can only have one line (key); ignoring admin.pub
    Perhaps you're using a key in a different format (like putty/plink)?
    If so, please convert it to openssh format using 'ssh-keygen -i'.
    If you want to add multiple public keys for a single user, use
    "[email protected]" file names. See the "one user, many keys"
    section in doc/3-faq-tips-etc.mkd for details.
    [master (root-commit) ea2940e] start
    2 files changed, 35 insertions(+)
    create mode 100644 conf/gitolite.conf
    create mode 100644 keydir/admin.pub
    fprint failed

    1. 看起來主要訊息好像是這段:
      WARNING: a pubkey file can only have one line (key); ignoring admin.pub
      Perhaps you're using a key in a different format (like putty/plink)?
      If so, please convert it to openssh format using 'ssh-keygen -i'.
      你的 key 好像有問題, 他建議你用 ssh-keygen -i 在建立一把. 🙂

    2. 好像還要改一下~/.ssh/conf
      總之可以work了,在請問一些問題:
      user的名字是要和存入金鑰的名字對應嗎?
      如果不是 在哪邊作設定?
      謝謝你。

  2. 假設有兩個Eric. 一個在 machine_1 另一個在 machine_2,
    keydir 裡面要有:
    Eric.pub Eric@machine_2.pub
    config 裡要用
    Eric 和 Eric@machine_2代表兩個人?

    是這個意思嗎?

    另一個問題是,
    我要怎麼用gitk 看目前整個repo的狀況?
    如果我只有 clone 一個branch 到 local端的話。
    謝謝你

    1. 1. yes, 但是這樣子做的話, 會建議統一都 username@machine 來命名, 分開會很混亂.

      2. 我沒這樣子做, 沒辦法告訴你怎麼做. Orz.

  3. 所以如果 keydir 裡面是
    Eric.pub Eric@machine_1.pub
    那麼gitolite.conf
    就要用 Eric 和 Eric@machine_1 去表示這user?
    謝謝。

  4. 你好,请教个问题,怎样设置在clone一个git代码仓的时候,需要输入用户名和密码,比如说: git clone [email protected]:test.git 输入这个命令之后,不是马上就会开始clone的操作,而是要先输入用户名和密码,然后再继续clone代码仓的操作,请问这该怎么弄?谢谢!

    1. 还有个问题就是,我用ssh-keygen -t rsa 创建key的时候,假设没有加-f参数,默认生成的key是id_rsa和id_rsa.pub 但是我手动把他们改名为user和user.pub 并且已经push到gitolite-admin的远程仓库,一切都配置好了,但是我在客户端执行git clone相应仓库的时候,居然找不到对应的仓库,我只要把客户端的私钥文件由user 改为 id_rsa 就可以git clone 对应的仓代码,请问这是怎么回事?谢谢!

  5. 你好!
    Q1:
    在步驟中 git clone gitolite@localhost:gitolite-admin
    我得到的是一個空的資料, keydir 也沒有
    Q2.
    想請問若User 由我這邊於Server 端建立User1時建立公司鑰,有沒有關係? (指的是說
    非家裡的Linux Clinet端)

    感謝你的回答

    1. 1. 你應該有步驟沒做到, ex: gl-setup /tmp/admin.pub 這類的. (猜想是這樣, 因為我直接做是正常的)

      2. 沒關係, 這是 Server 架設時, 就要匯入的 key.

  6. 果然是我沒做到 , 感謝!
    想再請教你 , 當我執行以下指令卻出現以下訊息, 這是對的嗎 ? (還是沒有加入Mail)
    Q1. 當我進行git commit -m
    gitolite@git:~/gitolite-admin$ git commit -m 'add user1, user2, user3'
    *** Please tell me who you are.

    Run

    git config --global user.email "[email protected]"
    git config --global user.name "Your Name"

    to set your account's default identity.
    Omit --global to set the identity only in this repository.

    fatal: unable to auto-detect email address (got 'gitolite@git.(none)')

    gitolite@git:~/gitolite-admin$

    Q2. 想請教一個問題如下:
    ProjectA.1 – ProjectA.2 –ProjectA.3
    拿A.2分支成: Project B.1 - Project B.2
    但開發人員於B.2 發現Bug 要Teaceback , 系統會如何修正BUG至ProjectA 和 B ,
    Git 如何支援跨Project Tracebak

  7. 請不好意思又來問了!
    當使用Client 端軟體登入Remote 如
    http://192.168.3.100/gitolite/testing/
    卻出現 Excuting a Command has failed
    repository 'http://192.168.3.100/gitolite/testing/' not found
    感覺是找不到testing 這目錄但我在主機看確有此目錄
    請問有哪個步驟操作錯誤嗎 ? 有連到bitbucket 做測試非Client 軟體問題
    感恩

  8. 你好! 重新砍掉後已經完成,不好意思又來請教你了,想請問你當user1 有apk_code1 & apk_code2 .......apk_code10, user2 有code_a & code_b & code_c ..........code_10 , 而遠端假設預設Repository為programrepo 而user1 的apk_code1 ......& user2 的code_a是都要放在同一個programrepo 還是說apk_code1 開一個 Repository 以此類推 , code_a 開一個
    Repository 每個去設定

  9. 你好: 抱歉又來問你了 , 為何用例如: user1 使用者登入會出現以下問題, 所有人的.pub 已經匯入
    Configure Remote: 'testing' does not appear to be a git repository Could not read from remote repository.: Please make sure you have the correct access rights and the repository exists.
    以下gitolite.conf 內容
    repo gitolite-admin
    RW+ = admin

    repo testing
    RW+ = @all
    Repo Testing 卻定是所有人都是RW
    又要麻煩你了, 感謝你的幫忙

    1. 你可以看看他錯誤訊息寫得, 上面寫說沒有 testing 這個 repository.

      你看看是不是打錯名字或是沒有建立 repository~

  10. 你好! 請問在進行以下指令時會何會出現以下錯誤, 感謝解答
    gitolite@git:~/gitolite-admin/testing$ git commit -m 'add readme'

    *** Please tell me who you are.

    Run

    git config --global user.email "[email protected]"
    git config --global user.name "Your Name"

    to set your account's default identity.
    Omit --global to set the identity only in this repository.

    fatal: empty ident not allowed
    gitolite@git:~/gitolite-admin/testing$

git_test 發表迴響取消回覆

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料