個人Ubuntu版本是:python
cai@ubuntu02:~$ cat /etc/os-release NAME="Ubuntu" VERSION="16.04.4 LTS (Xenial Xerus)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 16.04.4 LTS" VERSION_ID="16.04" HOME_URL="http://www.ubuntu.com/" SUPPORT_URL="http://help.ubuntu.com/" BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/" VERSION_CODENAME=xenial UBUNTU_CODENAME=xenial
我使用的是普通用戶的賬號cai
,登陸後目錄是:git
cai@ubuntu02:~$ pwd /home/cai
安裝git:github
cai@ubuntu02:~$ sudo apt-get install git ...... Suggested packages: git-daemon-run | git-daemon-sysvinit git-doc git-el git-email git-gui gitk gitweb git-arch git-cvs git-mediawiki git-svn The following NEW packages will be installed: git ...... cai@ubuntu02:~$ git --version git version 2.7.4
也但是使用
sudo apt-get install gitcore
命令來安裝git。廖雪峯老師的Git教程也提到了緣由:之前有個軟件也叫GIT(GNU Interactive Tools),結果Git就只能叫git-core了。因爲Git名氣實在太大,後來就把GNU Interactive Tools改爲gnuit,git-core正式改成git
添加用戶git
,該用戶將做爲全部代碼倉庫和用戶權限的管理者(-m
表示manager
),並設置該用戶的密碼:web
cai@ubuntu02:~$ sudo useradd -m git cai@ubuntu02:~$ sudo passwd git Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
創建一個git倉庫的存儲點,而且設置權限使除了git之外的用戶對此目錄無任何訪問權限:ubuntu
cai@ubuntu02:~$ sudo mkdir /home/git_repo cai@ubuntu02:~$ sudo chown git:git /home/git_repo cai@ubuntu02:~$ sudo chmod 755 /home/git_repo
chmod 755的含義:3個數字分表表示文件全部者
的權限、與文件全部者同屬一個用戶組的其餘用戶
的權限、其它用戶組
的權限。而權限分爲三種:讀(r=4),寫(w=2),執行(x=1) 。 綜合起來還有可讀可執行(rx=5=4+1)、可讀可寫(rw=6=4+2)、可讀可寫可執行(rwx=7=4+2+1)。因此chmod 755
表示文件全部者有rwx權限,同一用戶組的其餘用戶有rx權限,其餘用戶組有rx權限。
Gitosis
是用來管理公鑰的。若是團隊很小,把每一個人的公鑰收集起來放到服務器的/home/git/.ssh/authorized_keys文件裏就好了。可是若是是不少人的團隊,能夠用Gitosis來管理公鑰。bash
cai@ubuntu02:/home/git$ apt-get install python-setuptools cai@ubuntu02:/home/git$ cd ~ cai@ubuntu02:~$ sudo git clone https://github.com/res0nat0r/gitosis.git cai@ubuntu02:~$ cd gitosis cai@ubuntu02:~/gitosis$ sudo python setup.py install
因爲Gitosis默認會將倉庫放在用戶的repositories目錄下,例如git用戶的倉庫地址默認在/home/git/repositories/目錄下,這裏咱們須要建立一個連接映射。讓他指向咱們前面建立的專門用於存放項目的倉庫目錄/home/git_repo:服務器
cai@ubuntu02:/home$ sudo ln -s /home/git_repo /home/git/repositories
在服務器端生成ssh公鑰:app
cai@ubuntu02:~$ cd ~ cai@ubuntu02:~$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/cai/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/cai/.ssh/id_rsa. Your public key has been saved in /home/cai/.ssh/id_rsa.pub. The key fingerprint is: SHA256:1X0QlMjs+OMI2fqWaMxmV0kONg0BWlfe0wMzBV8JF0U cai@ubuntu02 The key's randomart image is: +---[RSA 2048]----+ | o.o=.OBBE| | o ..o+oB+.| | . .=o +oo| | .= + o.| | S+ * . | | o . * | | o + = . | | O = . | | + +. |G +----[SHA256]-----+
用剛生成公鑰id_rsa.pub(/home/cai/.ssh目錄下)來對Gitosis進行初始化:dom
cai@ubuntu02:~/.ssh$ sudo chmod a+r /home/cai/.ssh/id_rsa.pub cai@ubuntu02:~/.ssh$ sudo -H -u git gitosis-init< /home/cai/.ssh/id_rsa.pub Initialized empty Git repository in /home/git_repo/gitosis-admin.git/ Reinitialized existing Git repository in /home/git_repo/gitosis-admin.git/ cai@ubuntu02:~/.ssh$
gitosis主要是經過gitosis-admin.git倉庫來管理一些配置文件的,如用戶權限的管理。這裏咱們須要對其中的一個post-update文件添加可執行的權限:ssh
cai@ubuntu02:/home/git_repo$ sudo chmod 755 /home/git_repo/gitosis-admin.git/hooks/post-update
首先須要在前面生成ssh公鑰(用來初始化gitosis)的機器(Ubuntu)上將gitosis-admin.git的倉庫clone下來。
而後在我本機(Win7,本機也要裝git才能clone哦)上新建一個目錄用於存放gitosis-admin.git倉庫。
clone下來會有一個gitosis.conf的配置文件和一個keydir的目錄。gitosis.conf用於配置用戶的權限信息,keydir主要用戶存放ssh公鑰文件(通常以「用戶名.pub」命名,gitosis.conf配置文件中需使用相同用戶名),用於認證請求的客戶端機器。
git clone git@192.168.86.20:/home/git_repo/gitosis-admin.git
操做以下圖(會提示輸入密碼,就是前面本身設置的git用戶的密碼):
客戶端機器上生成ssh key:
cd /d/gitgitgit/gitadmin/gitosis-admin/keydir ssh-keygen -t rsa -f caibaohong.pub -C "caibaohong@outlook.com"
-t
指定簽名的類型,-f
指定公鑰名稱 -C
表示註釋,操做以下圖:
將客戶機公鑰copy到keydir目錄下,在gitosis.conf裏配置權限,並推送服務器:
$ cd /d/gitgitgit/gitadmin/gitosis-admin $ cp ~/.ssh/id_rsa.pub keydir/caibaohong.pub $ vi gitosis.conf
添加用戶權限,注意這裏的members指定的用戶名,必須與前面生成的公鑰的命名同樣。caibaohong.pub ---> members=caibaohong :
[gitosis] [group gitosis-admin] members = cai@ubuntu02 writable = gitosis-admin [group write] members = caibaohong writable = hello-project
提到到服務器端的gitosis-admin倉庫:
$ git status $ git add . $ git commit -m "add user caibaohong with write privilege" $ git push
在服務器/home/git/repositories下新建一個倉庫hello-project(記得要修改目錄的權限):
cd /home/cat/repositories sudo mkdir hello-project cd hello-project git init cd .. sudo chown -R git:git hello-project
克隆到客戶端機器上,注意,需先刪除~/.ssh
目錄,由於剛纔可能gitosis-admin存了這個工程對應的公鑰,如今下載hello-project,須要在~/.ssh
目錄存caibaohong.pub
這個公鑰,若是不刪除,就會校驗出錯。提示ERROR:gitosis.serve.main:Repository read access denied
,具體操做(在win7上):
$ rm -rf ~/.ssh $ cd /d/gitgitgit/gitadmin $ git clone git@192.168.86.20:/home/git/repositories/hello-project Cloning into 'hello-project'... The authenticity of host '192.168.86.20 (192.168.86.20)' can't be established. ECDSA key fingerprint is SHA256:XbO6oTfugZQ8rGIA2Kz3hCh1sV1+dg9QD+DX++gaE+s. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.86.20' (ECDSA) to the list of known hosts. git@192.168.86.20's password: warning: You appear to have cloned an empty repository. $ ls gitadmin/ hello-project/
本地提交文件來測試一下:
vi test.txt git add . git commit -am "add a test.txt" git push origin master