(十九)git版本管理软件——搭建git服务器
创建管理员git
以root用户的形式创建一个专门管理git服务器的管理员
创建管理员git
root@ghost-machine:/home/ghost# adduser git 正在添加用户"git"... 正在添加新组"git" (1001)... 正在添加新用户"git" (1001) 到组"git"... 创建主目录"/home/git"... 正在从"/etc/skel"复制文件... 输入新的 UNIX 密码: 重新输入新的 UNIX 密码: passwd:已成功更新密码 正在改变 git 的用户信息 请输入新值,或直接敲回车键以使用默认值 全名 []: 房间号码 []: 工作电话 []: 家庭电话 []: 其它 []: 这些信息是否正确? [Y/n] y root@ghost-machine:/home/ghost#
Ubuntu会在创建的时候设置密码,若其他系统没有提示设置密码,需要手动设置git管理员密码
命令:sudo passwd git
有些网站建议关闭git管理员的ssh访问,但目前不做这一步。
为管理员用户添加sudo权限
编辑文件/etc/sudoers,该文件要更变成可写模式
root@ghost-machine:~# ll /etc/sudoers -r--r----- 1 root root 755 5月 29 2017 /etc/sudoers root@ghost-machine:~# chmod +w /etc/sudoers root@ghost-machine:~# vim /etc/sudoers root@ghost-machine:~# chmod -w /etc/sudoers root@ghost-machine:~# ll /etc/sudoers -r--r----- 1 root root 778 12月 9 10:20 /etc/sudoers root@ghost-machine:~#
编辑内容,在root后面加上git
# User privilege specification root ALL=(ALL:ALL) ALL git ALL=(ALL:ALL) ALL
生成管理员秘钥
切换到git用户并跳转到自己的工作目录下
root@ghost-machine:/home/ghost# su git git@ghost-machine:/home/ghost$ cd git@ghost-machine:~$
生成git管理员秘钥,输入的是用户名和邮箱的组合
git@ghost-machine:~$ ssh-keygen -t rsa -C "git@email.com" Generating public/private rsa key pair. Enter file in which to save the key (/home/git/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/git/.ssh/id_rsa. Your public key has been saved in /home/git/.ssh/id_rsa.pub. The key fingerprint is: SHA256:FxzVolVlTN5iK9XATCVHADyt4mHkN+zR036f+oB91XI git@email.com The key's randomart image is: +---[RSA 2048]----+ | .ooO*BB| | ...= =Bo| | oo+ =+.+| | *.*oooo| | So.=.oooE| | .. +. o+| | . o .+| | o..| | .o. | +----[SHA256]-----+ git@ghost-machine:~$
设置管理员git提交账号和邮箱
git@ghost-machine:~$ git config --global user.name "git" git@ghost-machine:~$ git config --global user.email "git@email.com"
下载安装gitolite
下载地址:下载路径最好放在/home/git/下
命令:git clone git://github.com/sitaramc/gitolite
git@ghost-machine:~$ git clone git://github.com/sitaramc/gitolite 正克隆到 'gitolite'... remote: Counting objects: 9509, done. remote: Compressing objects: 100% (12/12), done. remote: Total 9509 (delta 4), reused 5 (delta 2), pack-reused 9495 接收对象中: 100% (9509/9509), 3.00 MiB | 587.00 KiB/s, 完成. 处理 delta 中: 100% (5881/5881), 完成. 检查连接... 完成。 git@ghost-machine:~$
手动下源码安装保证gitolite的控制权在git管理员手上。
将命令加入系统环境变量中。
sudo vi /etc/profile
export PATH=$PATH:$HOME/bin
启动gitolite
将管理员生成的sskkey加载在软件中
git@ghost-machine:~$ gitolite setup -pk ~/.ssh/id_rsa.pub 初始化空的 Git 仓库于 /home/git/repositories/gitolite-admin.git/ 初始化空的 Git 仓库于 /home/git/repositories/testing.git/ WARNING: /home/git/.ssh/authorized_keys missing; creating a new one (this is normal on a brand new install) git@ghost-machine:~$
gitolite的管理都围绕gitolite-admin.git这个版本库进行。管理服务器的项目git版本库,用户的添加删除,项目git版本库的授权。
为统一管理服务器版本库,后续添加git版本库都统一交给gitolite-admin.git进行统一管理。
gitoltie会用相对路径去下载版本库。
管理员git首先要下载gitolite-admin.git,下载需要输入管理员sshkey的密码。
git@ghost-machine:~$ git clone git@localhost:gitolite-admin.git 正克隆到 'gitolite-admin'... The authenticity of host 'localhost (127.0.0.1)' can't be established. ECDSA key fingerprint is SHA256:ifKZZ/A2lLcOGPCOaQIOTVvvD+fgAgGiB2WJN4R00Xw. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts. Enter passphrase for key '/home/git/.ssh/id_rsa': remote: 对象计数中: 6, 完成. remote: 压缩对象中: 100% (4/4), 完成. remote: Total 6 (delta 0), reused 0 (delta 0) 接收对象中: 100% (6/6), 完成. 检查连接... 完成。 git@ghost-machine:~$
添加项目版本库
进入下载好的gitolite-admin版本库中,编辑文件gitolite.conf
命令:vim conf/gitolite.conf
添加的项目名为:zx_git_repo
git@ghost-machine:~/gitolite-admin$ git diff diff --git a/conf/gitolite.conf b/conf/gitolite.conf index 8eb9fbb..64e882d 100644 --- a/conf/gitolite.conf +++ b/conf/gitolite.conf @@ -3,3 +3,6 @@ repo gitolite-admin repo testing RW+ = @all + +repo zx_git_repo + RW+ = id_rsa git@ghost-machine:~/gitolite-admin$
项目设置为git管理员可以管理,git管理员对应名称id_rsa
在文件keydir会自动创建加载的管理员公钥
git@ghost-machine:~/gitolite-admin$ ls keydir/ id_rsa.pub
将修改push到库中
git@ghost-machine:~/gitolite-admin$ git add . git@ghost-machine:~/gitolite-admin$ git commit -m "add repo zx_git_repo." [master 5d8b89b] add repo zx_git_repo. 1 file changed, 3 insertions(+) git@ghost-machine:~/gitolite-admin$ git push origin master Enter passphrase for key '/home/git/.ssh/id_rsa': 对象计数中: 4, 完成. Delta compression using up to 4 threads. 压缩对象中: 100% (3/3), 完成. 写入对象中: 100% (4/4), 382 bytes | 0 bytes/s, 完成. Total 4 (delta 0), reused 0 (delta 0) remote: 初始化空的 Git 仓库于 /home/git/repositories/zx_git_repo.git/ To git@localhost:gitolite-admin.git 6c9b6a3..5d8b89b master -> master git@ghost-machine:~/gitolite-admin$
随着gitolite-admin的更改,gitolite会在版本库中初始化添加的库,添加的版本库就在/home/git/repositories/zx_git_repo.git/下
添加项目成员
项目需要添加成员,成员通过邮件或其他通信方式发送自己sshkey公钥给git管理员,将id_rsa.pub修改成自己的名字以便识别。
管理员拿到新成员的公钥后将其复制到keydir目录下
git@ghost-machine:~/gitolite-admin$ cp /mnt/hgfs/share/user_zx.pub keydir/ git@ghost-machine:~/gitolite-admin$ ls keydir/ id_rsa.pub user_zx.pub
编辑文件gitolite.conf
git@ghost-machine:~/gitolite-admin$ git diff diff --git a/conf/gitolite.conf b/conf/gitolite.conf index 64e882d..b173b92 100644 --- a/conf/gitolite.conf +++ b/conf/gitolite.conf @@ -1,3 +1,6 @@ +@admin = id_rsa +@user = user_zx + repo gitolite-admin RW+ = id_rsa @@ -5,4 +8,5 @@ repo testing RW+ = @all repo zx_git_repo - RW+ = id_rsa + RW+ = @admin + RW+ = @user git@ghost-machine:~/gitolite-admin$
这里将用户分组,分成admin和user,若有多个用户,用空格隔开
将修改push到库中
git@ghost-machine:~/gitolite-admin$ git add . git@ghost-machine:~/gitolite-admin$ git status 位于分支 master 您的分支与上游分支 'origin/master' 一致。 要提交的变更: (使用 "git reset HEAD <文件>..." 以取消暂存) 修改: conf/gitolite.conf 新文件: keydir/user_zx.pub git@ghost-machine:~/gitolite-admin$ git commit -m "add user user_zx to zx_git_pro." [master 07faacc] add user user_zx to zx_git_pro. 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100755 keydir/user_zx.pub git@ghost-machine:~/gitolite-admin$ git push origin master Enter passphrase for key '/home/git/.ssh/id_rsa': 对象计数中: 6, 完成. Delta compression using up to 4 threads. 压缩对象中: 100% (5/5), 完成. 写入对象中: 100% (6/6), 816 bytes | 0 bytes/s, 完成. Total 6 (delta 1), reused 0 (delta 0) To git@localhost:gitolite-admin.git 5d8b89b..07faacc master -> master git@ghost-machine:~/gitolite-admin$
项目成员下载项目
添加成功成员后,发送git项目的下载地址给对应成员,即可下载项目
项目地址:git@192.168.215.132:zx_git_repo.git
$ git clone git@192.168.215.132:zx_git_repo.git Cloning into 'zx_git_repo'... Enter passphrase for key '/c/Users/zx_work/.ssh/id_rsa': warning: You appear to have cloned an empty repository. Checking connectivity... done.
gitolite将下载路径变成相对地址访问,若不用gitolite管理,需要输入绝对地址并且输入git管理员的密码。
使用了gitolite管理之后,只需要输入授权成员的sskey即可。
推送代码
$ git push origin master Enter passphrase for key '/c/Users/zx_work/.ssh/id_rsa': Counting objects: 4, done. Delta compression using up to 8 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (4/4), 328 bytes | 0 bytes/s, done. Total 4 (delta 0), reused 0 (delta 0) To git@192.168.215.132:zx_git_repo.git * [new branch] master -> master
通过git管理员可以查看到该用户能够正常提交代码
git@ghost-machine:~/repositories/zx_git_repo.git$ git log commit 541c1a53aa884a69d41b9e70b06bdeb7756ed747 Author: zxng <zxngyulin@163.com> Date: Sat Dec 9 12:21:21 2017 +0800 add README.md git@ghost-machine:~/repositories/zx_git_repo.git$