20200619 前一段時間學習 git 時,嘗試過搭建公司自用的 git 服務器,並且安裝了 gitlab 社區版
後來,因爲大家都不太熟悉,而且英文也不好,只好直接採用了 碼雲 gitee 企業版作爲入門
然後,以上搭建過程也都忘記得差不多了!
疫情期間閒下來,再次重走一遍,並記錄下來,以備日後 gitee 過期時再次遺忘
1、 安裝git
$ sudo apt install git
重新找了一個 visualBox 虛擬機, ubuntu 20.04,不記得安裝過 git ,但是卻發現 git 已經存在!
2、創建一個git用戶,用來運行git服務
sudo adduser git
Passed = 123
如果嘗試過多次,可能會已經存在 git 用戶,可以先完全刪除 git 用戶及其目錄
$ sudo userdel -r git
userdel: git mail spool (/var/mail/git) not found
這個錯誤好像沒有發現什麼影響!
3、創建一個 git 倉庫目錄
cd /home
$ sudo mkdir gitrepo
$ cd gitrepo
前幾次操作時,直接用有 root 權限的用戶名登錄,並且直接在該用戶的 home 下建立的倉庫目錄(沒有 cd /home),後續 clone 時卻沒有加上該用戶的 home 路徑,造成過多次錯誤!
4、初始化 Git 倉庫
$ sudo git init --bare --shared sample.git
返回結果:
Initialized empty shared Git repository in /home/gitrepo/sample.git/
注意返回提示的倉庫路徑: /home/gitrepo/sample.git/
在客戶端 clone 時一定要寫正確了!
如果如上一步所提到的:沒有 cd /home , 這裏的路徑會帶上實際登錄的用戶名
例如:/home/dhbm/srv/sample.git/
5、 修改倉庫目錄 owner 爲 git
sudo chown -R git:git /home/gitrepo
確認一下
$ ll
...
drwxrwsr-x 7 git git 4096 Jun 19 09:58 sample.git/
6、 客戶端測試一下
$ git clone [email protected]:/gitrepo/sample.git
報錯了!
Cloning into 'sample'...
[email protected]'s password:
fatal: '/gitrepo/sample.git' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
倉庫路徑沒有書寫正確!請對照前面 init 時返回的路徑信息
再來!
$ git clone [email protected]:/home/gitrepo/sample.git/
Cloning into ‘sample’…
[email protected]’s password:
warning: You appear to have cloned an empty repository.
確認一下:
$ ls -al
total 0
drwxr-xr-x 3 dhbm staff 96 6 19 13:43 .
drwx------+ 24 dhbm staff 768 6 19 14:10 ..
drwxr-xr-x 3 dhbm staff 96 6 19 13:43 sample
這就 ok 了!
如果只是個人使用,到這裏就算完成了!
如果公司多人使用,建議給每個人單獨分配一個賬號,否則,倉庫無法管理了!
7、 再創建幾個用戶,並統一到 gitgroup 組
-
創建git用戶組
$ sudo addgroup gitgroup
Adding group `gitgroup’ (GID 1004) …
Done. -
新建幾個賬戶
sudo adduser gituser1
sudo adduser gituser2 -
將用戶添加到 gitgroup 用戶組
sudo usermod -G gitgroup git
sudo usermod -G gitgroup gituser1
sudo usermod -G gitgroup gituser2 -
確認一下用戶組
$ cat /etc/group |grep git...... gitgroup:x:1004:gituser1,gituser2,git
8、 修改git倉庫的用戶組
sudo chgrp -R gitgroup /home/gitrepo/sample.git/
或者
sudo chown -R :gitgroup /home/gitrepo/sample.git/
9、 客戶端分別測試一下
$ git clone [email protected]:/home/gitrepo/sample.git/
$ git clone [email protected]:/home/gitrepo/sample.git/
自行確認一下即可!
10、善後
剛纔建立了幾個用戶,只作爲 git 使用,所以,必須禁止他們實際登錄到服務器
學習 git 的時候,注意力不在這裏,所以,放在最後單獨處理!
$ sudo usermod -s /sbin/nologin gituser1
就地測試一下
$ su gituser1
Password:
This account is currently not available.
$ sudo usermod -s /sbin/nologin git
$ sudo usermod -s /sbin/nologin gituser2
** 後續測試結果證實:不能按照這個辦法禁止登錄!
摘錄如下:
sudo vim /etc/passwd
找到
git❌1001:1001:,:/home/git:/bin/bash
改爲:
git❌1001:1001:,:/home/git:/usr/bin/git-shell
11、 後續
-
客戶端測試提交一點兒內容
cd /Users/dhbm/Desktop/git-test/sample
-
查看一下遠程倉庫
$ git remote -v
origin [email protected]:/home/gitrepo/sample.git/ (fetch)
origin [email protected]:/home/gitrepo/sample.git/ (push) -
vim test.md
隨意輸入一些東西! -
$ git add *
-
$ git commit -m ‘20200619,just test’
[master (root-commit) e4b701f] 20200619,just test 1 file changed, 7 insertions(+) create mode 100644 test.md
-
$ git push
怎麼報錯了![email protected]'s password: Could not chdir to home directory /home/gituser1: No such file or directory fatal: protocol error: bad line length character: This
-
查看一下用戶
$ cat /etc/passwd |grep gitgit❌1001:1001:,:/home/git:/sbin/nologin
gituser1❌1002:1002::/home/gituser1:/sbin/nologin
gituser2❌1003:1003:,:/home/gituser2:/bin/bash -
上服務端查看一下倉庫目錄
$ ll...... drwxr-xr-x 3 git git 4096 Jun 19 10:00 git/ drwxr-xr-x 3 git git 4096 Jun 19 09:58 gitrepo/ drwxr-xr-x 2 gituser2 gituser2 4096 Jun 19 13:34 gituser2/
怎麼 gitrepo 還是屬於 git:git ,前面修改組沒有成功?
sudo chown -R :gitgroup /home/gitrepo/sample.git/
-
換成 git 試試
$ git clone [email protected]:/home/gitrepo/sample.git/
見鬼了!這次連 clone 也錯了?
Cloning into 'sample'...
[email protected]'s password:
fatal: protocol error: bad line length character: This
怎麼回事?難道是我們善後工作沒有做好,不應該這樣子禁止 git 用戶登錄?
-
改用 gituser2 試試
好在剛纔只是學習,禁止登陸並沒有操作到 gituser2$ git clone [email protected]:/home/gitrepo/sample.git/
這個是正確的!Cloning into 'sample'... [email protected]'s password: warning: You appear to have cloned an empty repository.
哈哈哈哈哈!😄😄😄😄😄
-
修改禁止 git 用戶登錄的方法
sudo vim /etc/passwd
如下,看到 git、gituser 和沒有修改的 gituser2 的差別!
-
/bin/bash 修改爲 /usr/bin/git-shell
找到
git:x:1001:1001:,,,:/home/git:/bin/bash
改爲:
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell
gituser1 同樣處理
最終結果如下:
-
再次試試
$ git clone [email protected]:/home/gitrepo/sample.git/Cloning into 'sample'... [email protected]'s password: Could not chdir to home directory /home/gituser1: No such file or directory warning: You appear to have cloned an empty repository.
我靠!什麼時候把 gituser1 的 home 目錄刪除了
$ sudo mkdir gituser1
$ sudo chown -R gituser1:gituser1 gituser1加上 gituser1 的 home 之後,再來試試?終於 ok 了 !
-
後來還遇到以下錯誤
錯誤一:
$ git push
[email protected]'s password:
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 210 bytes | 210.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
error: remote unpack failed: unable to create temporary object directory
To 192.168.1.203:/home/gitrepo/sample.git/
! [remote rejected] master -> master (unpacker error)
error: failed to push some refs to '[email protected]:/home/gitrepo/sample.git/'
錯誤二:
Total 3 (delta 0), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: is denied, because it will make the index and work tree inconsistent
remote: with what you pushed, and will require 'git reset --hard' to match
remote: the work tree to HEAD.
remote:
remote: You can set the 'receive.denyCurrentBranch' configuration variable
remote: to 'ignore' or 'warn' in the remote repository to allow pushing into
remote: its current branch; however, this is not recommended unless you
remote: arranged to update its work tree to match what you pushed in some
remote: other way.
remote:
remote: To squelch this message and still keep the default behaviour, set
remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To 192.168.1.203:/home/gitrepo/sample.git/
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '[email protected]:/home/gitrepo/sample.git/'
主要是權限設置問題
最後,實在沒有辦法,服務端重新初始化倉庫目錄
其實差不多就是從頭再來一次