linuxgit服务器
A. 如何 连接 linux git服务器
1、前期准备 服务器上配置好的git git客户端 1.1 在服务器上安装git (本机所使用的linux是ubuntu) 在服务器输入命令:sudo apt-get install git即可 然后创建名字为git的用户组和用户 1.2 下载客户端 在浏览器地址栏输入:https://git-for-windows.github.io/ 回车后 点击Download进行下载 2、具体操作 2.1 在合适的位置创建一个目录充当git远程仓库(本机位置为/usr/testgit),然后使用init命令初始化仓库 在命令终端输入: sudo git init –bare 2.2 将git init生成的目录所属者改为git 输入命令:sudo chown -R git:git * 至此服务器端的操作完成。 在客户端合适位置使用git 客户端从服务器资源 2.3 首先打开git客户端 点击Git Bash Here 后出现 在git客户端命名终端输入: git clone git@xxxxxx:/rrrrr 其中xxxxxx是远程服务器的地址 rrrrr为git仓库所在位置 如果配置正确你选中的目录下会出现名字为testgit的文件夹 testgit文件夹下随意创建若干个文件 2.3 在git客户端上使用命令 git add 111.txt 222.txt 333.txt 或者使用git add .(将本文件夹下所有文件都add) 该命令的作用是告诉git把文件添加到git仓库 2.4 然后使用git commit命令将文件提交到git仓库 -m 后面的内容为本次提交文件的一些注释内容 此时文件还没有从本地仓库上传到远程服务器仓库 2.5 使用push命令将本地仓库中的内容提交到远程仓库 在git客户端命令终端输入:git push origin master 至此本地仓库中的文件上传已经上传到远程服务器仓库。 在其他文件夹下再次使用 git clone 命令 从远程服务器同步仓库
B. linux如何搭建git
1、环境准备
服务器:CentOS 7.3 + git (1.8.3.1)
客户端:win10 + git (2.17.0.windows.1)
2、服务器安装git
yum install -y git
3、创建git用户,管理 git服务
[root@localhost home]# useradd git
[root@localhost home]# passwd git
4、服务器创建git 仓库
设置/home/git/repository-git 为git 服务器仓库,然后把 git 仓库的 owner 修改为 git 用户。
复制代码
[root@localhost git]# mkdir repository-git
[root@localhost git]# git init --bare repository-git/
Initialized empty Git repository in /home/git/repository-gt/
[root@localhost git]# chown -R git:git repository-git/
5、客户端安装git
下载 Git for Windows,地址:https://git-for-windows.github.io/
安装完之后,可以使用 Git Bash 作为命令行客户端。
5.1、选择一个目录 F:\project\sell 作为本地仓库,右键进入Git Bash 命令行模式
初始化本地仓库:git init
5.2、尝试克隆一个服务器的空仓库到本地仓库
git clone [email protected]:/home/git/repository-gt
第一次连接到目标 Git 服务器时会得到一个提示:
The authenticity of host '192.168.116.129(192.168.116.129)' can't be established.
RSA key fingerprint is SHA256:Ve6WV/.
Are you sure you want to continue connecting (yes/no)?
选择 yes:
Warning: Permanently added '192.168.116.129' (RSA) to the list of known hosts.
此时 C:\Users\用户名\.ssh 下会多出一个文件 known_hosts,以后在这台电脑上再次连接目标 Git 服务器时不会再提示上面的语句。
C. linux怎么搭建git服务器
GitHub就是一个免费托管开源代码的远程仓库。但是对于某些视源代码如生命的商业公司来说,既不想公开源代码,又舍不得给GitHub交保护费,那就只能自己搭建一台Git服务器作为私有仓库使用。
搭建Git服务器需要准备一台运行Linux的机器,强烈推荐用Ubuntu或Debian,这样,通过几条简单的apt命令就可以完成安装。
假设你已经有sudo权限的用户账号,下面,正式开始安装。
第一步,安装git:
$ sudo apt-get install git
第二步,创建一个git用户,用来运行git服务:
$ sudo adser git
第三步,创建证书登录:
收集所有需要登录的用户的公钥,就是他们自己的id_rsa.pub文件,把所有公钥导入到/home/git/.ssh/authorized_keys文件里,一行一个。
第四步,初始化Git仓库:
先选定一个目录作为Git仓库,假定是/srv/sample.git,在/srv目录下输入命令:
$ sudo git init --bare sample.git
Git就会创建一个裸仓库,裸仓库没有工作区,因为服务器上的Git仓库纯粹是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git结尾。然后,把owner改为git:
$ sudo chown -R git:git sample.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每次一登录就自动退出。
第六步,克隆远程仓库:
现在,可以通过git clone命令克隆远程仓库了,在各自的电脑上运行:
$ git clone git@server:/srv/sample.git
Cloning into 'sample'...
warning: You appear to have cloned an empty repository.
剩下的推送就简单了。
D. linux搭建git远程仓库
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.