CentOS下Git的服务器版安装和使用

7 篇文章 0 订阅

使用了一段时间的git,使用过①:TortoiseGit和②:eclipse的git插件和③Git-2.6.2-64-bit的命令行,前两者是可视化的,我选择的最后的命令行。

Git的思想,遵循GNU的思想:GNU是“GNU is Not Unix”的递归缩写。Git继承了开源社区的精神,不直接支持权限控制。但也有脚本接口和第三方管理权限。不过视代码为生命,视员工为窃贼的公司,就感觉没必要用。但可以自己安装gitlab,自己的git服务器。
git和github和gitlab的区别:
GitLab - 基于Git的项目管理软件。
Github - 一个免费/收费网站,提供给用户空间创建git仓储,保存用户的一些数据文档或者代码等。
GitLab - 是一个用于仓库管理系统的开源项目。使用Git作为代码管理工具,并在此基础上搭建起来的web服务。
Git – 分布式版本控制系统通常也有一台充当“中央服务器”的电脑,但这个服务器的作用仅仅是用来方便“交换”大家的修改,没有它大家也一样干活,只是交换修改不方便而已。
详细请学习,这儿的讲解nice:廖雪峰的官方网站

闲话不说,开始搭建自己的git服务器:
系统:#cat /etc/issue –> CentOS release 6.5 (Final)和 #git –version –> git version 1.7.1

1、安装git

安装git  
yum install git  
版本git  
git --version  
卸载git  
yum remove git

2、搭建服务器

创建git运行用户并设置密码

useradd git
passwd git

创建git目录和一个空git仓库

cd /
mkdir data
cd data
mkdir git
cd git
git init --bare test.git
chown -R git:git test.git

客户运行git bash

git clone git@192.168.31.157:/data/git/test.git
cd test
git remote -v

详细讲解

①:创建git服务器工作空间:  
#mkdir -p /home/repository/git  
注:-p参数:如果一个目录的父目录不存在,就创建它  
②:新建组和用户  
#groupadd git  
#useradd git -g git -d /home/repository/git/ -s /usr/bin/git-shell  
注:若不能执行则,先-s /bin/sh 后手动修改 /etc/passwd文件的/usr/bin/git-shell  
#grep git /etc/passwd  
git:x:501:501::/home/repository/git/:/usr/bin/git-shell  
③:更改权限git文件和目录权限:  
#chown -R git.git /home/repository/git/  
#ls -la  
drwxr-xr-x  3 root root 4096 Jan 19 13:25 .  
drwxr-xr-x. 6 root root 4096 Jan 17 22:51 ..  
drwxr-xr-x  4 git  git  4096 Jan 19 13:25 git  
④:初始化Git服务器仓库:  
服务器上的Git仓库纯粹是为了共享,并且用户无权限直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git结尾。  
git init --bare kekeai.git  
注:a,不使用--bare选项时,就会生成.git目录以及其下的版本历史记录文件,这些版本历史记录文件就存放在.git目录下;  
b,使用--bare选项时,不再生成.git目录,而是只生成.git目录下面的版本历史记录文件,这些版本历史记录文件也不再存放在.git目录下面,而是直接存放在版本库的根目录下面。  
⑤:本地用户生成自己的公钥,并收集所有用户的id_rsa.pub文件的信息,把所有公钥导入到/home/repository/git/.ssh/authorized_keys文件里:目录写错将出现错误2[文章末尾备注]文件里,一行一个!  

⑥:本地clone代码,自己已经装好了git命令行[ip自己服务器]:  
$ git clone git@123.12.188.057:/home/repository/git/kekeai.git  
Cloning into 'kekeai'...  
warning: You appear to have cloned an empty repository.  
Checking connectivity... done.  
则ok! 

注:生成自己的公钥:git使用步骤

3、遇到错误

错误1:

$ git clone git@123.12.188.057:/home/repository/git/kekeai.git
Cloning into 'kekeai'...
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
SHA256:OHPAU2Z3PkMFJYco3F/YB3slBeVz8IlP7ig4CEcm624.
Please contact your system administrator.
Add correct host key in /c/Users/Tony_tian/.ssh/known_hosts to get rid of this message.
Offending RSA key in /c/Users/Tony_tian/.ssh/known_hosts:2
RSA host key for 123.12.188.057 has changed and you have requested strict checking.
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

解决 1:
删除自己本地[windows]的C:\Users\Sony.ssh下known_hosts文件下,删除,对应当前”192.168.31.157“这个服务器的:
192.168.31.157 ssh-rsa ……这一行!

问题2:
git在clone时候权限要输入密码:

$ git clone git@123.12.188.057:/home/repository/git/kekeai.git
Cloning into 'kekeai'...
The authenticity of host '123.12.188.057 (123.12.188.057)' can't be established.
RSA key fingerprint is SHA256:OHPAU2Z3PkMFJYco3F/YB3slBeVz8IlP7ig4CEcm624.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '123.12.188.057' (RSA) to the list of known hosts.
git@123.12.188.057's password:
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

解决2:
1:查看用户的公钥位置是否在,git或其他用户的那个指定的目录下,”公钥文件路径错误”,请查看!
2:如果不是root,则查看,文件的权限问题!
3:查看本地的id_rsa.pub的内容,是否告诉服务器的,authorized_keys。
4:删除本地known_hosts中,当前服务器的配置,重新开启一个git命令行,clone就好!

备注:ubuntu下安装的,gitweb和git的安装包:

5、创建SSH Key
首先在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步。如果没有,打开Shell(Windows下打开Git Bash),创建SSH Key:

$ ssh-keygen -t rsa -C "youremail@example.com"  

你需要把邮件地址换成你自己的邮件地址,然后一路回车,使用默认值即可,由于这个Key也不是用于军事目的,所以也无需设置密码。

6、Git服务器打开RSA认证
然后就可以去Git服务器上添加你的公钥用来验证你的信息了。在Git服务器上首先需要将/etc/ssh/sshd_config中将RSA认证打开,即:

StrictModes  no #在用户名和其公钥文件名不匹配时将通过验证
RSAAuthentication yes     
PubkeyAuthentication yes     
AuthorizedKeysFile  .ssh/authorized_keys

重启ssh服务

service sshd restart   或
/etc/init.d/sshd restart

这里我们可以看到公钥存放在.ssh/authorized_keys文件中。所以我们在/home/git下创建.ssh目录,然后创建authorized_keys文件,并将刚生成的公钥导入进去。
然后再次clone的时候,或者是之后push的时候,就不需要再输入密码了:

Zhu@XXX/E/testgit/8.34
$ git clone git@192.168.8.34:/data/git/learngit.git
Cloning into 'learngit'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.

7、禁用git用户的shell登陆
出于安全考虑,第二步创建的git用户不允许登录shell,这可以通过编辑/etc/passwd文件完成。找到类似下面的一行:

git:x:1001:1001:,,,:/home/git:/bin/bash  

最后一个冒号后改为:

git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell 

这样,git用户可以正常通过ssh使用git,但无法登录shell,因为我们为git用户指定的git-shell每次一登录就自动退出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值