linux搭建git远程仓库

linux搭建git远程仓库,第1张

1. linux和windows端分别安装git,其中linux中可以用yum安装

[root@node0~]#yum install git

git的默认安装路径在/usr/libexec/git-core

[root@node0 git-core]#cd /usr/libexec/git-core

[root@node0 git-core]#git --version

git version 1.7.1

2.设置linux端git的用户名和密码

[root@node0 git-core]# groupadd git

[root@node0 git-core]# useradd wang -g git

[root@node0 git-core]# passwd wang

New password:

3.在服务器端创建远程仓库

[root@node0 ~]# mkdir -p /mnt/gitrep/wjf

[root@node0 ~]# cd /mnt/gitrep/wjf/

[root@node0 wjf]# git init

Initialized empty Git repository in /mnt/gitrep/wjf/.git/

把仓库所属用户改为wang(git的用户名)

[root@node0 wjf]# chown -R wang:git .git/

注:chown将指定文件的拥有者改为指定的用户或组 -R处理指定目录以及其子目录下的所有文件

4.在windows客户端克隆仓库 

$ git clone [email protected]:/mnt/gitrep/wjf/.git

Cloning into 'wjf'...

The authenticity of host '192.168.111.60 (192.168.111.60)' can't be established.

RSA key fingerprint is SHA256:MgWCWF************************1m2tI.

Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

Warning: Permanently added '192.168.111.60' (RSA) to the list of known hosts.

[email protected]'s password:

第一次连接远程仓库,出现黑体部分,这是因为Git使用SSH连接,而SSH连接在第一次验证GitHub服务器的Key时,需要你确认GitHub的Key的指纹信息是否真的来自GitHub的服务器,键入yes,然后输入远程仓库的密码就可以了。

5.实际中也通常通过设置公钥的方式来连接远程仓库,这样就不用每次连接都需要密码了。

设置公钥:

1.在windows客户端的gitbash中生成用户私钥和公钥

$ ssh-keygen -t rsa -C "[email protected]"

在c盘用户路径下的/.ssh文件夹下会生成私钥id_rsa和公钥id_rsa.pub

2.linux端

首先 Git服务器打开RSA认证,即,修改/etc/ssh/sshd_config,将其中的以下三项打开

RSAAuthentication yes 

 PubkeyAuthentication yes 

 AuthorizedKeysFile .ssh/authorized_keys

然后,将客户端生成的公钥给到服务器端

即,将公钥给到 home/wang(git的用户名)/.ssh/authorized_keys

[root@node0 ~]# cd /home/wang

[root@node0 wang]# mkdir .ssh

[root@node0 wang]# chmod 777 .ssh

[root@node0 wang]# touch .ssh/authorized_keys

在windows客户端的gitbash中 执行:

$ ssh [email protected] 'cat >>.ssh/authorized_keys' <~/.ssh/id_rsa.pub

然后在linux端:

[root@node0 wang]# chmod 600 .ssh/authorized_keys

[root@node0 wang]# chmod 700 .ssh

[root@node0 wang]# chown wang:git .ssh

[root@node0 wang]# chown wang:git .ssh/authorized_keys 

至此,以后再连接远程仓库就不需要密码了。

若仍需要密码,可以查看ssh连接日志/var/log/secure:

常见连接失败原因:Authentication refused: bad ownership or modes for directory /home/wang/.ssh 

这时需要检查该目录的所属用户和读写权限等级是否符合要求。公钥以及.ssh文件的权限应该属于git的用户和用户组,读写权限等级.ssh 700,authorized_keys 600.

使用yum安装确实简单方便,但 yum 存在一个问题就是安装的版本不好控制。

以下安装的版本为1.8.3.1,这个版本太老了。

截至发布文章的时候最新版本为:v2.30.0

Git 最新版本: https://github.com/git/git/releases

源码编译安装看这里 https://www.jianshu.com/p/d06538116a2e

直接使用以下命令

安装完成之后使用以下命令查看安装的 Git 的版本,验证是否安装成功

出现以下信息则安装成功

此方法安装 Git 的默认路径为(/usr/libexec/git-core)可以使用命令查看安装目录信息

安装目录信息如下图所示


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/6054767.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-13
下一篇 2023-03-13

发表评论

登录后才能评论

评论列表(0条)

保存