个人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