aneasystone@little-stone:~$ sudo mkdir -p /git/repo
aneasystone@little-stone:~$ sudo git init --bare /git/repo/test.git
已初始化空的 Git 倉庫於 /git/repo/test.git/
aneasystone@little-stone:~$ cd ~/working/
aneasystone@little-stone:~/working$ git clone /git/repo/test.git
正克隆到 'test'...
warning: 您似乎克隆了一個空倉庫。
完成。
aneasystone@little-stone:~/working$ cd test/
aneasystone@little-stone:~/working/test$ touch 1
aneasystone@little-stone:~/working/test$ touch 2
aneasystone@little-stone:~/working/test$ git add .
aneasystone@little-stone:~/working/test$ git commit -m 'first commit'
[master (根提交) 4983f84] first commit
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 1
create mode 100644 2
aneasystone@little-stone:~/working/test$ sudo git push
[sudo] aneasystone 的密碼:
物件計數中: 3, 完成.
Delta compression using up to 8 threads.
壓縮物件中: 100% (2/2), 完成.
寫入物件中: 100% (3/3), 205 bytes | 205.00 KiB/s, 完成.
Total 3 (delta 0), reused 0 (delta 0)
To /git/repo/test.git
* [new branch] master -> master
root@myserver:~# mkdir -p /git/repo
root@myserver:~# git init --bare /git/repo/test.git
已初始化空的 Git 倉庫於 /git/repo/test.git/
aneasystone@little-stone:~/working$ git clone ssh://root@myserver/git/repo/test.git
正克隆到 'test'...
root@myserver's password:
warning: 您似乎克隆了一個空倉庫。
$ git clone root@myserver:/git/repo/test.git
-
最顯而易見的方法是為每個 Git 使用者建立一個獨立的賬號,並分別為他們分配對倉庫的讀寫許可權,這種方法行的通,但是對賬號的管理非常麻煩,在團隊人員不是很多的時候可以嘗試,但是並不推薦;
-
另一種方法是配置 SSH 伺服器使用某個已有的認證系統來管理使用者,比如 LDAP,這在很多企業中是很常見的,這樣可以省去用 adduser 手工管理伺服器賬號的麻煩;
-
還有一種方法是隻建立一個賬號,比如叫做 Git,他對倉庫具有讀寫許可權,大家都使用這個賬號來訪問倉庫。這種方法的好處是使用者管理起來比較簡單,而且可以使用後面介紹的 authorized_keys 檔案對使用者的公鑰進行管理。
root@myserver:~# adduser git
Adding user `git' ...
Adding new group `git' (1000) ...
Adding new user `git' (1000) with group `git' ...
Creating home directory `/home/git' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for git
Enter the new value, or press ENTER for the default
Full Name []: git
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] Y
# chmod a+w -R /git/repo/test.git
$ git clone git@myserver:/git/repo/test.git
aneasystone@little-stone:~/.ssh$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/aneasystone/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/aneasystone/.ssh/id_rsa.
Your public key has been saved in /home/aneasystone/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:4Ulpufuhs/AgDMb0VXnqMUTw6bD/HrAOI2z9c1cod9I aneasystone@little-stone
The key's randomart image is:
+---[RSA 2048]----+
| .oo. |
| oo+. |
| . o.Oo |
| o . . B++ |
| + . ..So o |
| . + . ..+. + E |
| * * + oo + |
| . o Oo+.o. |
| **+. |
+----[SHA256]-----+
# scp id_rsa.pub root@myserver:/home/git
root@myserver:/home/git# cat id_rsa.pub >> /home/git/.ssh/authorized_keys
git:x:1000:1000:git,,,:/home/git:/bin/bash
root@myserver:~# cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/dash
/bin/bash
/bin/rbash
root@myserver:~# chsh git
Changing the login shell for git
Enter the new value, or press ENTER for the default
Login Shell [/bin/bash]: /usr/bin/git-shell
-
使用 g+w 設定 Git 倉庫的許可權,讓倉庫建立者所在的使用者組具有寫許可權,而不是所有人都有寫許可權(這一步通常也可以在 git init 的時候加上 –shared 引數);
-
然後將 Git 賬號加到倉庫建立者的使用者組;
-
再建立一個 git_ro 賬戶,這個賬戶對倉庫只有隻讀許可權;
-
最後為 gitro 賬戶建立一個金鑰對,把 gitro 的私鑰公開出來供所有人使用。
root@myserver:~# git daemon --reuseaddr --base-path=/git/repo/ /git/repo/
root@myserver:~# cd /git/repo/test.git/
root@myserver:/git/repo/test.git/# touch git-daemon-export-ok
aneasystone@little-stone:~/working$ git clone git://myserver/test.git
# apt-get install -y git-core nginx fcgiwrap apache2-utils
# service nginx start
# service fcgiwrap start
location / {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend;
fastcgi_param GIT_HTTP_EXPORT_ALL "";
fastcgi_param GIT_PROJECT_ROOT /git/repo;
fastcgi_param PATH_INFO $uri;
fastcgi_param REMOTE_USER $remote_user;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
}
-
SCRIPT_FILENAME:指定 CGI 指令碼 git-http-backend 的位置,表示每次 HTTP 請求會被轉發到該 CGI 指令碼;
-
GITHTTPEXPORTALL:git-http-backend 預設只能訪問目錄下有 git-daemon-export-ok 檔案的 Git 倉庫,和上面介紹的 Git 協議是一樣的,如果指定了 GITHTTPEXPORTALL,表示允許訪問所有倉庫;
-
GITPROJECTROOT:Git 倉庫的根目錄;
-
REMOTE_USER:如果有認證,將認證的使用者資訊傳到 CGI 指令碼;
aneasystone@little-stone:~/working$ git clone http://myserver/test.git
aneasystone@little-stone:~/working/test$ git push origin master
fatal: unable to access 'http://myserver/test.git/': The requested URL returned error: 403
root@myserver:/# cd /git/repo/test.git/
root@myserver:/git/repo/test.git# git config http.receivepack true
$HTTP["querystring"] =~ "service=git-receive-pack" {
include "git-auth.conf"
}
$HTTP["url"] =~ "^/git/.*/git-receive-pack$" {
include "git-auth.conf"
}
[27/Nov/2018:22:18:00] "GET /test.git/info/refs?service=git-upload-pack HTTP/1.1" 200 363 "-" "git/1.9.1"
[27/Nov/2018:22:18:00] "POST /test.git/git-upload-pack HTTP/1.1" 200 306 "-" "git/1.9.1"
[27/Nov/2018:22:20:25] "GET /test.git/info/refs?service=git-upload-pack HTTP/1.1" 200 363 "-" "git/1.9.1"
[27/Nov/2018:22:20:25] "POST /test.git/git-upload-pack HTTP/1.1" 200 551 "-" "git/1.9.1"
[27/Nov/2018:22:19:33] "GET /test.git/info/refs?service=git-receive-pack HTTP/1.1" 401 204 "-" "git/1.9.1"
admin [27/Nov/2018:22:19:33] "GET /test.git/info/refs?service=git-receive-pack HTTP/1.1" 200 193 "-" "git/1.9.1"
admin [27/Nov/2018:22:19:33] "POST /test.git/git-receive-pack HTTP/1.1" 200 63 "-" "git/1.9.1"
location @auth {
auth_basic "Git Server";
auth_basic_user_file /etc/nginx/passwd;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend;
fastcgi_param GIT_HTTP_EXPORT_ALL "";
fastcgi_param GIT_PROJECT_ROOT /git/repo;
fastcgi_param PATH_INFO $uri;
fastcgi_param REMOTE_USER $remote_user;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
}
location / {
error_page 418 = @auth;
if ( $query_string = "service=git-receive-pack" ) { return 418; }
if ( $uri ~ "git-receive-pack$" ) { return 418; }
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend;
fastcgi_param GIT_HTTP_EXPORT_ALL "";
fastcgi_param GIT_PROJECT_ROOT /git/repo;
fastcgi_param PATH_INFO $uri;
fastcgi_param REMOTE_USER $remote_user;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
}
root@myserver:/# htpasswd -cb /etc/nginx/passwd admin 123456
aneasystone@little-stone:~/working/test$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 193 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
error: unpack failed: unable to create temporary object directory
To http://myserver/test.git
! [remote rejected] master -> master (unpacker error)
error: failed to push some refs to 'http://myserver/test.git'
root@myserver:/# chown -R www-data:www-data /git/repo
$ git config --global credential.helper cache
$ git config --global credential.helper store
aneasystone@little-stone:~/working$ git clone http://admin:123456@myserver/test.git
-
優點:架設簡單,不依賴外部服務,直接使用現有檔案和網路許可權,常用於共享檔案系統
-
缺點:共享檔案系統的配置和使用不方便,且無法保護倉庫被意外損壞,傳輸效能較低
-
優點:架設簡單,所有資料經過授權加密,資料傳輸很安全,傳輸效能很高
-
缺點:不支援匿名訪問,配置 SSH 的金鑰對小白使用者有一定的門檻
-
優點:對開放的專案很適用,無需授權,傳輸效能最高
-
缺點:缺乏授權機制,架設較麻煩,企業一般不會預設開放 9418 埠需要另行新增
-
優點:同時支援授權訪問和無授權訪問,傳輸效能較高,配合 HTTPS 也可以實現資料安全
-
缺點:架設 HTTP 服務較麻煩,認證憑證不好管理
-
https://rhodecode.com/
-
https://rhodecode.com/insights/version-control-systems-2016
-
http://www.aneasystone.com/archives/2016/04/java-and-https.html
-
https://help.github.com/articles/connecting-to-github-with-ssh/
-
https://git-scm.com/docs/git-daemon
-
https://git-scm.com/book/zh/v2