Linux中使用Gitolite搭建Git服务器,服务器和客户端可以为同一台
在linux系统中我们可以直接使用Gitolite 搭建 Git 服务器了,下面本文章就给各位介绍详细的配置过程,希望能帮助到大家。
一般来说,配置git服务器的话,需要一台服务器,还需要一个客户端来验证服务器是否搭建成功,而一般开发者基本都只有一台服务器,那就需要这台服务器既作为Git的服务器,又要作为客户端来使用了,下面的教程就是以服务器和客户端都是同一台服务器为例的。如果需要服务器和客户端分开的话,只需要将下面列出的服务器端和客户端的操作到相应的机器上操作就可以了。
第一步,服务器端创建git服务器专用账户,账户名字就叫git吧,创建之后为其设置密码,代码如下:
- # useradd -m -s /bin/bash git
- # passwd git
- Enter new UNIX password:
- Retype new UNIX password:
- passwd: password updated successfully
切换到git用户,在家目录/home/git下创建安装目录bin,代码如下:
- root@letuknowit:~# su - git
- git@letuknowit:~$ mkdir bin
- git@letuknowit:~$ ll
- total 24
- drwxr-xr-x 3 git git 4096 Aug 20 10:38 ./
- drwxr-xr-x 8 root root 4096 Aug 20 10:36 ../
- -rw-r
- -rw-r
- drwxrwxr-x 2 git git 4096 Aug 20 10:38 bin/
- -rw-r
获取 Gitolite 并安装,gitolite要安装在服务器端,安装之前要保证系统上已经安装了git,代码如下:
- git@letuknowit:~$ git clone git://github.com/sitaramc/gitolite
- Cloning into 'gitolite'...
- remote: Counting objects: 8682, done.
- remote: Total 8682 (delta 0), reused 0 (delta 0)
- Receiving objects: 100% (8682/8682), 3.61 MiB | 33 KiB/s, done.
- Resolving deltas: 100% (4917/4917), done.
- git@letuknowit:~$ ll
- total 28
- drwxr-xr-x 4 git git 4096 Aug 20 10:40 ./
- drwxr-xr-x 8 root root 4096 Aug 20 10:36 ../
- -rw-r
- -rw-r
- drwxrwxr-x 2 git git 4096 Aug 20 10:38 bin/
- drwxr-xr-x 6 git git 4096 Aug 20 10:40 gitolite/
- -rw-r
- git@letuknowit:~$ gitolite/install -ln
- git@letuknowit:~$ ll
- total 28
- drwxr-xr-x 4 git git 4096 Aug 20 10:40 ./
- drwxr-xr-x 8 root root 4096 Aug 20 10:36 ../
- -rw-r
- -rw-r
- drwxrwxr-x 2 git git 4096 Aug 20 10:44 bin/
- drwxr-xr-x 6 git git 4096 Aug 20 10:40 gitolite/
- -rw-r
第二步,客户端,创建 Gitolite 服务器管理员用户,并创建管理员用户密钥.
因为我们是在同一台服务器上,直接exit退回到root用户,之后变身为客户端模式,如果客户端和服务器分开的话,不需要exit指令了,直接在客户端执行添加用户的指令就可以了,这里的Gitolite服务器管理员用户名我们叫gitAdmin,代码如下:
- git@letuknowit:~$ exit
- logout
- root@letuknowit:~# useradd -m -s /bin/bash gitAdmin
- root@letuknowit:~# passwd gitAdmin
- Enter new UNIX password:
- Retype new UNIX password:
- passwd: password updated successfully
切换到gitAdmin用户,创建用户密钥,代码如下:
- root@letuknowit:~# su - gitAdmin
- gitAdmin@letuknowit:~$ mkdir .ssh
- gitAdmin@letuknowit:~$ ll
- total 24
- drwxr-xr-x 3 gitAdmin gitAdmin 4096 Aug 20 10:48 ./
- drwxr-xr-x 9 root root 4096 Aug 20 10:46 ../
- -rw-r
- -rw-r
- -rw-r
- drwxrwxr-x 2 gitAdmin gitAdmin 4096 Aug 20 10:48 .ssh/
- gitAdmin@letuknowit:~$ ssh-keygen -f .ssh/gitAdmin
- Generating public/private rsa key pair.
- Enter passphrase (empty for no passphrase):
- Enter same passphrase again:
- Your identification has been saved in .ssh/gitAdmin.
- Your public key has been saved in .ssh/gitAdmin.pub.
- The key fingerprint is:
- 84:a0:9d:d6:c6:43:57:0e:22:cd:67:97:8e:03:be:09 gitAdmin@letuknowit
- The key's randomart image is:
- +
- | ooo o... |
- | o B++ooo |
- | . +.*+.+. |
- | .E..oo . |
- | . oS. |
- | o |
- | |
- | |
- | |
- +
接下来将管理员公钥(gitAdmin.pub)复制 到服务器上的 git 用户的家目录下,因为我们这个服务器和客户端是在一起的,所以就方便了,如果是两台机器的话,你可能要想点办法去拷贝这个文件了,代码如下:
- gitAdmin@letuknowit:~$ exit
- logout
- root@letuknowit:~# cp /home/gitAdmin/.ssh/gitAdmin.pub /home/git/
- root@letuknowit:~# ll /home/git
- total 36
- drwxr-xr-x 4 git git 4096 Aug 20 10:53 ./
- drwxr-xr-x 9 root root 4096 Aug 20 10:46 ../
- -rw
- -rw-r
- -rw-r
- drwxrwxr-x 2 git git 4096 Aug 20 10:44 bin/
- -rw-r
- drwxr-xr-x 6 git git 4096 Aug 20 10:40 gitolite/
- -rw-r
第三步,服务器端配置 Gitolite 服务器,再回到服务器端,用从客户端复制过来的 Gitolite 管理员用户公钥,gitAdmin.pub,配置 Gitolite 服务器,代码如下:
- root@letuknowit:~# su - git
- git@letuknowit:~$ gitolite setup -pk gitAdmin.pub
- Initialized empty Git repository in /home/git/repositories/gitolite-admin.git/
- Initialized empty Git repository in /home/git/repositories/testing.git/
- WARNING: /home/git/.ssh missing; creating a new one
- (this is normal on a brand new install)
- WARNING: /home/git/.ssh/authorized_keys missing; creating a new one
- (this is normal on a brand new install)
- git@letuknowit:~$ ll
- total 60
- drwxr-xr-x 7 git git 4096 Aug 20 10:57 ./
- drwxr-xr-x 9 root root 4096 Aug 20 10:46 ../
- -rw
- -rw-r
- -rw-r
- drwxrwxr-x 2 git git 4096 Aug 20 10:44 bin/
- -rw-r
- drwxr-xr-x 6 git git 4096 Aug 20 10:40 gitolite/
- drwx
- -rw
- -rw-r
- -rw
- drwx
- drwx
你会看到在git用户的家目录下多了很多的文件夹,具体请看上面ll命令的结果.
第四步,客户端验证Gitolite管理员账户访问服务器的别名以及验证别名是否成功,代码如下:
- git@letuknowit:~$ exit
- logout
- root@letuknowit:~# su - gitAdmin
- gitAdmin@letuknowit:~$ ll .ssh/
- total 16
- drwxrwxr-x 2 gitAdmin gitAdmin 4096 Aug 20 10:49 ./
- drwxr-xr-x 3 gitAdmin gitAdmin 4096 Aug 20 10:53 ../
- -rw
- -rw-r
- gitAdmin@letuknowit:~$ vi .ssh/config
切换到gitAdmin账户,在.ssh文件夹下新建config文件,输入下面内容:
- host git
- user git
- hostname xxx.xxx.xxx.xxx
- port 22
- identityfile ~/.ssh/gitAdmin
host:随便定义,用于客户端标识所连接服务端的名字;
user:ssh登录服务器的账户,就是服务器端的那个git专用账户的用户名;
hostname:服务器主机名,或者直接填写服务器 IP 地址;
port:服务器的ssh访问端口,默认22;
identityfile:管理员登录服务器所使用的验证密钥;
使用服务器别名从 Gitolite 服务器 clone Gitolite 服务器管理仓库 gitolite-admin,验证别名配置是否成功,代码如下:
- gitAdmin@letuknowit:~$ git clone git:gitolite-admin
- Cloning into 'gitolite-admin'...
- The authenticity of host 'xxx.xxx.xxx.xxx (xxx.xxx.xxx.xxx)' can't be established.
- ECDSA key fingerprint is c1:c2:6a:7a:68:c8:e5:a6:87:f4:9b:95:d5:fd:ff:09.
- Are you sure you want to continue connecting (yes/no)? yes
- Warning: Permanently added 'xxx.xxx.xxx.xxx' (ECDSA) to the list of known hosts.
- remote: Counting objects: 6, done.
- remote: Compressing objects: 100% (4/4), done.
- Receiving objects: 100% (6/6), 763 bytes, done.
- remote: Total 6 (delta 0), reused 0 (delta 0)
第五步,客户端给普通用户创建访问 Gitolite 服务器的密钥.
系统里已经有一个kris的账户了,如果没有的话,可以自己创建一个,名字随便取,先通过ssh-keygen创建密钥,代码如下:
- gitAdmin@letuknowit:~$ exit
- logout
- root@letuknowit:~# su - kris
- kris@letuknowit:~$ ssh-keygen -f .ssh/kris
- Generating public/private rsa key pair.
- Enter passphrase (empty for no passphrase):
- Enter same passphrase again:
- Your identification has been saved in .ssh/kris.
- Your public key has been saved in .ssh/kris.pub.
- The key fingerprint is:
- 18:7c:21:e8:7b:4c:2a:d9:b2:e2:eb:db:5b:94:65:49 kris@letuknowit
- The key's randomart image is:
- +
- | ..E. |
- | ...... |
- | . o+. |
- | . =+ |
- | o B. S |
- | + = o |
- | + o |
- |. o . |
- |o*oo. |
- +
- kris@letuknowit:~$ ll .ssh/
- total 20
- drwx
- drwxr-xr-x 9 kris kris 4096 Aug 20 09:56 ../
- -rw-r
- -rw
- -rw-r
复制用户公钥(kris.pub)给 Gitolite 管理员(gitAdmin),代码如下:
- kris@letuknowit:~$ exit
- logout
- root@letuknowit:~# cp /home/kris/.ssh/kris.pub /home/gitAdmin/gitolite-admin/keydir/
- root@letuknowit:~# ll !$
- ll /home/gitAdmin/gitolite-admin/keydir/
- total 16
- drwxrwxr-x 2 gitAdmin gitAdmin 4096 Aug 20 12:09 ./
- drwxr-xr-x 5 gitAdmin gitAdmin 4096 Aug 20 11:19 ../
- -rw-rw-r
- -rw-r
使用公钥(kris.pub)为用户 kris 创建一个他能访问的 Git 仓库,代码如下:
- gitAdmin@letuknowit:~/gitolite-admin$ git add .
- gitAdmin@letuknowit:~/gitolite-admin$ git status -s
- M conf/gitolite.conf
- A keydir/kris.pub
- gitAdmin@letuknowit:~/gitolite-admin$ git commit -m "add user kris by kris.git and create a repo letu for it"
- *** Please tell me who you are.
- Run
- git config
- git config
- to set your account's default identity.
- Omit
- fatal: empty ident <gitAdmin@letuknowit.(none)> not allowed
这里需要先配置下用户名和email,否则会给出错误提示,代码如下:
- gitAdmin@letuknowit:~/gitolite-admin$ git config
- gitAdmin@letuknowit:~/gitolite-admin$ git config
- gitAdmin@letuknowit:~/gitolite-admin$ git commit -m "add user kris by kris.git and create a repo letu for it"
- [master ef0bde9] add user kris by kris.git and create a repo letu for it
- 2 files changed, 4 insertions(+)
- create mode 100644 keydir/kris.pub
- gitAdmin@letuknowit:~/gitolite-admin$ git status
- # On branch master
- # Your branch is ahead of 'origin/master' by 1 commit.
- #
- nothing to commit (working directory clean)
- gitAdmin@letuknowit:~/gitolite-admin$ git push origin master
- Counting objects: 10, done.
- Delta compression using up to 2 threads.
- Compressing objects: 100% (5/5), done.
- Writing objects: 100% (6/6), 840 bytes, done.
- Total 6 (delta 0), reused 0 (delta 0)
- remote: Initialized empty Git repository in /home/git/repositories/letu.git/
- To git:gitolite-admin
- b19c84d..ef0bde9 master -> master
切换到拥有私钥(~/.ssh/kris)的用户 kris,尝试使用私钥 clone “letu”这个git库,代码如下:
- gitAdmin@letuknowit:~/gitolite-admin$ exit
- logout
- root@letuknowit:~# su - kris
- kris@letuknowit:~$ ll .ssh/
- total 20
- drwx
- drwxr-xr-x 9 kris kris 4096 Aug 20 09:56 ../
- -rw-r
- -rw
- -rw-r
- kris@letuknowit:~$ vi .ssh/config
为了方便,我们这里也给kris用户配置一个访问别名,输入以下内容后保存,代码如下:
- host kris
- user git
- hostname xxx.xxx.xxx.xxx
- port 22
- identityfile ~/.ssh/kris
下面就可以clone 之前添加的letu这个git库了,代码如下:
- kris@letuknowit:~/$ git clone kris:letu
- Cloning into 'letu'...
- warning: You appear to have cloned an empty repository.
- kris@letuknowit:~$ cd letu/
- kris@letuknowit:~/letu$ git status
- # On branch master
- #
- # Initial commit
- #
- nothing to commit (create/copy files and use "git add" to track)
- kris@letuknowit:~/letu$ echo "Hello git" > init.txt
- kris@letuknowit:~/letu$ git add .
- kris@letuknowit:~/letu$ git status .
- # On branch master
- #
- # Initial commit
- #
- # Changes to be committed:
- # (use "git rm --cached ..." to unstage)
- #
- # new file: init.txt
- #
- kris@letuknowit:~/letu$ git commit -m "Init repo"
- kris@letuknowit:~/letu$ git push origin master