Linux中使用Gitolite搭建Git服务器,服务器和客户端可以为同一台
在linux系统中我们可以直接使用Gitolite 搭建 Git 服务器了,下面本文章就给各位介绍详细的配置过程,希望能帮助到大家。
一般来说,配置git服务器的话,需要一台服务器,还需要一个客户端来验证服务器是否搭建成功,而一般开发者基本都只有一台服务器,那就需要这台服务器既作为Git的服务器,又要作为客户端来使用了,下面的教程就是以服务器和客户端都是同一台服务器为例的。如果需要服务器和客户端分开的话,只需要将下面列出的服务器端和客户端的操作到相应的机器上操作就可以了。
第一步,服务器端创建git服务器专用账户,账户名字就叫git吧,创建之后为其设置密码,代码如下:
- #useradd-m-s/bin/bashgit
- #passwdgit
- EnternewUNIXpassword:
- RetypenewUNIXpassword:
- passwd:passwordupdatedsuccessfully
切换到git用户,在家目录/home/git下创建安装目录bin,代码如下:
- root@letuknowit:~#su-git
- git@letuknowit:~$mkdirbin
- git@letuknowit:~$ll
- total24
- drwxr-xr-x3gitgit4096Aug2010:38./
- drwxr-xr-x8rootroot4096Aug2010:36../
- -rw-r--r--1gitgit220Apr32012.bash_logout
- -rw-r--r--1gitgit3486Apr32012.bashrc
- drwxrwxr-x2gitgit4096Aug2010:38bin/
- -rw-r--r--1gitgit675Apr32012.profile
获取 Gitolite 并安装,gitolite要安装在服务器端,安装之前要保证系统上已经安装了git,代码如下:
- git@letuknowit:~$gitclonegit://github.com/sitaramc/gitolite
- Cloninginto'gitolite'...
- remote:Countingobjects:8682,done.
- remote:Total8682(delta0),reused0(delta0)
- Receivingobjects:100%(8682/8682),3.61MiB|33KiB/s,done.
- Resolvingdeltas:100%(4917/4917),done.
- git@letuknowit:~$ll
- total28
- drwxr-xr-x4gitgit4096Aug2010:40./
- drwxr-xr-x8rootroot4096Aug2010:36../
- -rw-r--r--1gitgit220Apr32012.bash_logout
- -rw-r--r--1gitgit3486Apr32012.bashrc
- drwxrwxr-x2gitgit4096Aug2010:38bin/
- drwxr-xr-x6gitgit4096Aug2010:40gitolite/
- -rw-r--r--1gitgit675Apr32012.profile
- git@letuknowit:~$gitolite/install-ln
- git@letuknowit:~$ll
- total28
- drwxr-xr-x4gitgit4096Aug2010:40./
- drwxr-xr-x8rootroot4096Aug2010:36../
- -rw-r--r--1gitgit220Apr32012.bash_logout
- -rw-r--r--1gitgit3486Apr32012.bashrc
- drwxrwxr-x2gitgit4096Aug2010:44bin/
- drwxr-xr-x6gitgit4096Aug2010:40gitolite/
- -rw-r--r--1gitgit675Apr32012.profile
第二步,客户端,创建 Gitolite 服务器管理员用户,并创建管理员用户密钥.
因为我们是在同一台服务器上,直接exit退回到root用户,之后变身为客户端模式,如果客户端和服务器分开的话,不需要exit指令了,直接在客户端执行添加用户的指令就可以了,这里的Gitolite服务器管理员用户名我们叫gitAdmin,代码如下:
- git@letuknowit:~$exit
- logout
- root@letuknowit:~#useradd-m-s/bin/bashgitAdmin
- root@letuknowit:~#passwdgitAdmin
- EnternewUNIXpassword:
- RetypenewUNIXpassword:
- passwd:passwordupdatedsuccessfully
切换到gitAdmin用户,创建用户密钥,代码如下:
- root@letuknowit:~#su-gitAdmin
- gitAdmin@letuknowit:~$mkdir.ssh
- gitAdmin@letuknowit:~$ll
- total24
- drwxr-xr-x3gitAdmingitAdmin4096Aug2010:48./
- drwxr-xr-x9rootroot4096Aug2010:46../
- -rw-r--r--1gitAdmingitAdmin220Apr32012.bash_logout
- -rw-r--r--1gitAdmingitAdmin3486Apr32012.bashrc
- -rw-r--r--1gitAdmingitAdmin675Apr32012.profile
- drwxrwxr-x2gitAdmingitAdmin4096Aug2010:48.ssh/
- gitAdmin@letuknowit:~$ssh-keygen-f.ssh/gitAdmin
- Generatingpublic/privatersakeypair.
- Enterpassphrase(emptyfornopassphrase):
- Entersamepassphraseagain:
- Youridentificationhasbeensavedin.ssh/gitAdmin.
- Yourpublickeyhasbeensavedin.ssh/gitAdmin.pub.
- Thekeyfingerprintis:
- 84:a0:9d:d6:c6:43:57:0e:22:cd:67:97:8e:03:be:09gitAdmin@letuknowit
- Thekey'srandomartimageis:
- +--[RSA2048]----+
- |oooo...|
- |oB++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
- total36
- drwxr-xr-x4gitgit4096Aug2010:53./
- drwxr-xr-x9rootroot4096Aug2010:46../
- -rw-------1gitgit788Aug2010:45.bash_history
- -rw-r--r--1gitgit220Apr32012.bash_logout
- -rw-r--r--1gitgit3486Apr32012.bashrc
- drwxrwxr-x2gitgit4096Aug2010:44bin/
- -rw-r--r--1rootroot412Aug2010:53gitAdmin.pub
- drwxr-xr-x6gitgit4096Aug2010:40gitolite/
- -rw-r--r--1gitgit675Apr32012.profile
第三步,服务器端配置 Gitolite 服务器,再回到服务器端,用从客户端复制过来的 Gitolite 管理员用户公钥,gitAdmin.pub,配置 Gitolite 服务器,代码如下:
- root@letuknowit:~#su-git
- git@letuknowit:~$gitolitesetup-pkgitAdmin.pub
- InitializedemptyGitrepositoryin/home/git/repositories/gitolite-admin.git/
- InitializedemptyGitrepositoryin/home/git/repositories/testing.git/
- WARNING:/home/git/.sshmissing;creatinganewone
- (thisisnormalonabrandnewinstall)
- WARNING:/home/git/.ssh/authorized_keysmissing;creatinganewone
- (thisisnormalonabrandnewinstall)
- git@letuknowit:~$ll
- total60
- drwxr-xr-x7gitgit4096Aug2010:57./
- drwxr-xr-x9rootroot4096Aug2010:46../
- -rw-------1gitgit788Aug2010:45.bash_history
- -rw-r--r--1gitgit220Apr32012.bash_logout
- -rw-r--r--1gitgit3486Apr32012.bashrc
- drwxrwxr-x2gitgit4096Aug2010:44bin/
- -rw-r--r--1rootroot412Aug2010:53gitAdmin.pub
- drwxr-xr-x6gitgit4096Aug2010:40gitolite/
- drwx------6gitgit4096Aug2010:57.gitolite/
- -rw-------1gitgit6662Aug2010:57.gitolite.rc
- -rw-r--r--1gitgit675Apr32012.profile
- -rw-------1gitgit12Aug2010:57projects.list
- drwx------4gitgit4096Aug2010:57repositories/
- drwx------2gitgit4096Aug2010:57.ssh/
你会看到在git用户的家目录下多了很多的文件夹,具体请看上面ll命令的结果.
第四步,客户端验证Gitolite管理员账户访问服务器的别名以及验证别名是否成功,代码如下:
- git@letuknowit:~$exit
- logout
- root@letuknowit:~#su-gitAdmin
- gitAdmin@letuknowit:~$ll.ssh/
- total16
- drwxrwxr-x2gitAdmingitAdmin4096Aug2010:49./
- drwxr-xr-x3gitAdmingitAdmin4096Aug2010:53../
- -rw-------1gitAdmingitAdmin1679Aug2010:49gitAdmin
- -rw-r--r--1gitAdmingitAdmin412Aug2010:49gitAdmin.pub
- gitAdmin@letuknowit:~$vi.ssh/config
切换到gitAdmin账户,在.ssh文件夹下新建config文件,输入下面内容:
- hostgit
- usergit
- hostnamexxx.xxx.xxx.xxx
- port22
- identityfile~/.ssh/gitAdmin
host:随便定义,用于客户端标识所连接服务端的名字;
user:ssh登录服务器的账户,就是服务器端的那个git专用账户的用户名;
hostname:服务器主机名,或者直接填写服务器 IP 地址;
port:服务器的ssh访问端口,默认22;
identityfile:管理员登录服务器所使用的验证密钥;
使用服务器别名从 Gitolite 服务器 clone Gitolite 服务器管理仓库 gitolite-admin,验证别名配置是否成功,代码如下:
- gitAdmin@letuknowit:~$gitclonegit:gitolite-admin
- Cloninginto'gitolite-admin'...
- Theauthenticityofhost'xxx.xxx.xxx.xxx(xxx.xxx.xxx.xxx)'can'tbeestablished.
- ECDSAkeyfingerprintisc1:c2:6a:7a:68:c8:e5:a6:87:f4:9b:95:d5:fd:ff:09.
- Areyousureyouwanttocontinueconnecting(yes/no)?yes
- Warning:Permanentlyadded'xxx.xxx.xxx.xxx'(ECDSA)tothelistofknownhosts.--phpfensi.com
- remote:Countingobjects:6,done.
- remote:Compressingobjects:100%(4/4),done.
- Receivingobjects:100%(6/6),763bytes,done.
- remote:Total6(delta0),reused0(delta0)
第五步,客户端给普通用户创建访问 Gitolite 服务器的密钥.
系统里已经有一个kris的账户了,如果没有的话,可以自己创建一个,名字随便取,先通过ssh-keygen创建密钥,代码如下:
- gitAdmin@letuknowit:~$exit
- logout
- root@letuknowit:~#su-kris
- kris@letuknowit:~$ssh-keygen-f.ssh/kris
- Generatingpublic/privatersakeypair.
- Enterpassphrase(emptyfornopassphrase):
- Entersamepassphraseagain:
- Youridentificationhasbeensavedin.ssh/kris.
- Yourpublickeyhasbeensavedin.ssh/kris.pub.
- Thekeyfingerprintis:
- 18:7c:21:e8:7b:4c:2a:d9:b2:e2:eb:db:5b:94:65:49kris@letuknowit
- Thekey'srandomartimageis:
- +--[RSA2048]----+
- |..E.|
- |......|
- |.o+.|
- |.=+|
- |oB.S|
- |+=o|
- |+o|
- |.o.|
- |o*oo.|
- +-----------------+
- kris@letuknowit:~$ll.ssh/
- total20
- drwx------2kriskris4096Aug2011:22./
- drwxr-xr-x9kriskris4096Aug2009:56../
- -rw-r--r--1kriskris442Nov202013known_hosts
- -rw-------1kriskris1679Aug2011:22kris
- -rw-r--r--1kriskris410Aug2011:22kris.pub
复制用户公钥(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/
- total16
- drwxrwxr-x2gitAdmingitAdmin4096Aug2012:09./
- drwxr-xr-x5gitAdmingitAdmin4096Aug2011:19../
- -rw-rw-r--1gitAdmingitAdmin412Aug2011:19gitAdmin.pub
- -rw-r--r--1rootroot410Aug2012:09kris.pub
使用公钥(kris.pub)为用户 kris 创建一个他能访问的 Git 仓库,代码如下:
- gitAdmin@letuknowit:~/gitolite-admin$gitadd.
- gitAdmin@letuknowit:~/gitolite-admin$gitstatus-s
- Mconf/gitolite.conf
- Akeydir/kris.pub
- gitAdmin@letuknowit:~/gitolite-admin$gitcommit-m"adduserkrisbykris.gitandcreatearepoletuforit"
- ***Pleasetellmewhoyouare.
- Run
- gitconfig--globaluser.email"you@example.com"
- gitconfig--globaluser.name"YourName"
- tosetyouraccount'sdefaultidentity.
- Omit--globaltosettheidentityonlyinthisrepository.
- fatal:emptyident<gitAdmin@letuknowit.(none)>notallowed
这里需要先配置下用户名和email,否则会给出错误提示,代码如下:
- gitAdmin@letuknowit:~/gitolite-admin$gitconfig--globaluser.email"xxx@163.com"
- gitAdmin@letuknowit:~/gitolite-admin$gitconfig--globaluser.name"kris"
- gitAdmin@letuknowit:~/gitolite-admin$gitcommit-m"adduserkrisbykris.gitandcreatearepoletuforit"
- [masteref0bde9]adduserkrisbykris.gitandcreatearepoletuforit
- 2fileschanged,4insertions(+)
- createmode100644keydir/kris.pub
- gitAdmin@letuknowit:~/gitolite-admin$gitstatus
- #Onbranchmaster
- #Yourbranchisaheadof'origin/master'by1commit.
- #
- nothingtocommit(workingdirectoryclean)
- gitAdmin@letuknowit:~/gitolite-admin$gitpushoriginmaster
- Countingobjects:10,done.
- Deltacompressionusingupto2threads.
- Compressingobjects:100%(5/5),done.
- Writingobjects:100%(6/6),840bytes,done.
- Total6(delta0),reused0(delta0)
- remote:InitializedemptyGitrepositoryin/home/git/repositories/letu.git/
- Togit:gitolite-admin
- b19c84d..ef0bde9master->master
切换到拥有私钥(~/.ssh/kris)的用户 kris,尝试使用私钥 clone “letu”这个git库,代码如下:
- gitAdmin@letuknowit:~/gitolite-admin$exit
- logout
- root@letuknowit:~#su-kris
- kris@letuknowit:~$ll.ssh/
- total20
- drwx------2kriskris4096Aug2011:22./
- drwxr-xr-x9kriskris4096Aug2009:56../
- -rw-r--r--1kriskris442Nov202013known_hosts
- -rw-------1kriskris1679Aug2011:22kris
- -rw-r--r--1kriskris410Aug2011:22kris.pub
- kris@letuknowit:~$vi.ssh/config
为了方便,我们这里也给kris用户配置一个访问别名,输入以下内容后保存,代码如下:
- hostkris
- usergit
- hostnamexxx.xxx.xxx.xxx
- port22
- identityfile~/.ssh/kris
下面就可以clone 之前添加的letu这个git库了,代码如下:
- kris@letuknowit:~/$gitclonekris:letu
- Cloninginto'letu'...
- warning:Youappeartohaveclonedanemptyrepository.
- kris@letuknowit:~$cdletu/
- kris@letuknowit:~/letu$gitstatus
- #Onbranchmaster
- #
- #Initialcommit
- #
- nothingtocommit(create/copyfilesanduse"gitadd"totrack)
- kris@letuknowit:~/letu$echo"Hellogit">init.txt
- kris@letuknowit:~/letu$gitadd.
- kris@letuknowit:~/letu$gitstatus.
- #Onbranchmaster
- #
- #Initialcommit
- #
- #Changestobecommitted:
- #(use"gitrm--cached..."tounstage)
- #
- #newfile:init.txt
- #
- kris@letuknowit:~/letu$gitcommit-m"Initrepo"
- kris@letuknowit:~/letu$gitpushoriginmaster
热门评论