linux下搭建git服务器和客户端
1. 如何在 linux 上安装 git 服务
安装Optware IPKG。这个在QNAP官方的App Center里有提供,直接去安装即可。
安装Git。这里通过ipkg安装的Git比较新。官方App Center里提供的git版本比较老。通过SSH登陆admin账号后运行如下命令。
ipkg update
ipkg install git
初始化git服务器端仓库。你的git仓库务必存放在非系统自带的目录下,否则系统重启之后数据会被抹掉(我尝试了是这样的)。假定服务器端git仓库目录为:/opt/repos,项目目录为hets.git。通过SSH登陆admin账号后运行如下命令。
2. 在Linux下搭建Git服务器
众所周知,版本系统在开发环境中是必不可少的,但是我们可以把代码免费的托管到GitHub上,如果我们不原意公开项目的源代码,公司又不想付费使用,那么我们可以自己搭建一台Git服务器,可以用Gitosis来管理公钥,还是比较方便的。
搭建环境:
服务器 CentOS6.6 + git(version 1.8.3.1)
客户端 Windows10 + git(version 2.11.1.windows.1)
1. 安装Git相关软件
Linux是服务器端系统,Windows作为客户端系统,分别安装Git
安装客户端:
下载 Git for Windows,地址:https://git-for-windows.github.io/
安装完之后,可以使用Git Bash作为命令行客户端。
安装Gitosis
出现下面的信息表示安装成功了
2. 服务器端创建git用户来管理Git服务
3. 配置公钥
在Windows上配置管理者,git服务器需要一些管理者,通过上传开发者机器的公钥到服务器,添加成为git服务器的管理者,打开git命令行
4. 配置gitosis
使用git用户并初始化gitosis
在Windows上机器上clone gitosis-admin到管理者主机
gitosis.conf: git服务器配置文件
keydir: 存放客户端公钥
配置 gitosis.conf 文件
在Windows管理者机器上创建本地test仓库,并上传到git服务端
提交到远程服务器
服务端会自动创建test仓库
5.添加其他git用户开发者
由于公司开发团队人数不断增多,手动添加开发者私钥到/home/git/.ssh/authorized_keys比较麻烦,通过上面的Windows机器的管理者统一收集其他开发者的私钥id_rsa.pub文件,然后传到服务器上,配置好后,用户即获得项目权限,可以从远程仓库拉取和推送项目,达到共同开发项目。
推送完成后,新加进来的开发者就可以进行项目的开发了,后续增加人员可以这样添加进来,开发者直接把仓库clone下来就可以了。
3. 如何搭建linux git服务器
首先我们分别在Git服务器和客户机中安装Git服务程序(刚刚实验安装过就不用安装了):
[root@linuxprobe ~]# yum install git
Loaded plugins: langpacks, proct-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Package git-1.8.3.1-4.el7.x86_64 already installed and latest version
Nothing to do
然后创建Git版本仓库,一般规范的方式要以.git为后缀:
[root@linuxprobe ~]# mkdir linuxprobe.git
修改Git版本仓库的所有者与所有组:
[root@linuxprobe ~]# chown -Rf git:git linuxprobe.git/
初始化Git版本仓库:
[root@linuxprobe ~]# cd linuxprobe.git/
[root@linuxprobe linuxprobe.git]# git --bare init
Initialized empty Git repository in /root/linuxprobe.git/
其实此时你的Git服务器就已经部署好了,但用户还不能向你推送数据,也不能克隆你的Git版本仓库,因为我们要在服务器上开放至少一种支持Git的协议,比如HTTP/HTTPS/SSH等,现在用的最多的就是HTTPS和SSH,我们切换至Git客户机来生成SSH密钥:
[root@linuxprobe ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
65:4a:53:0d:4f:ee:49:4f:94:24:82:16:7a:dd:1f:28 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
| .o+oo.o. |
| .oo *.+. |
| ..+ E * o |
| o = + = . |
| S o o |
| |
| |
| |
| |
+-----------------+
将客户机的公钥传递给Git服务器:
[root@linuxprobe ~]# ssh--id 192.168.10.10
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.10.10'"
and check to make sure that only the key(s) you wanted were added.
此时就已经可以从Git服务器中克隆版本仓库了(此时目录内没有文件是正常的):
[root@linuxprobe ~]# git clone [email protected]:/root/linuxprobe.git
Cloning into 'linuxprobe'...
warning: You appear to have cloned an empty repository.
[root@linuxprobe ~]# cd linuxprobe
[root@linuxprobe linuxprobe]#
初始化下Git工作环境:
[root@linuxprobe ~]# git config --global user.name "Liu Chuan"
[root@linuxprobe ~]# git config --global user.email "[email protected]"
[root@linuxprobe ~]# git config --global core.editor vim
向Git版本仓库中提交一个新文件:
[root@linuxprobe linuxprobe]# echo "I successfully cloned the Git repository" > readme.txt
[root@linuxprobe linuxprobe]# git add readme.txt
[root@linuxprobe linuxprobe]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached ..." to unstage)
#
# new file: readme.txt
#
[root@linuxprobe linuxprobe]# git commit -m "Clone the Git repository"
[master (root-commit) c3961c9] Clone the Git repository
Committer: root
1 file changed, 1 insertion(+)
create mode 100644 readme.txt
[root@linuxprobe linuxprobe]# git status
# On branch master
nothing to commit, working directory clean
但是这次的操作还是只将文件提交到了本地的Git版本仓库,并没有推送到远程Git服务器,所以我们来定义下远程的Git服务器吧:
[root@linuxprobe linuxprobe]# git remote add server [email protected]:/root/linuxprobe.git
将文件提交到远程Git服务器吧:
[root@linuxprobe linuxprobe]# git push -u server master
Counting objects: 3, done.
Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:/root/linuxprobe.git
* [new branch] master -> master
Branch master set up to track remote branch master from server.
为了验证真的是推送到了远程的Git服务,你可以换个目录再克隆一份版本仓库(虽然在工作中毫无意义):
[root@linuxprobe linuxprobe]# cd ../Desktop
[root@linuxprobe Desktop]# git clone [email protected]:/root/linuxprobe.git
Cloning into 'linuxprobe'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
[root@linuxprobe Desktop]# cd linuxprobe/
[root@linuxprobe linuxprobe]# cat readme.txt
I successfully cloned the Git repository
http://www.linuxprobe.com/chapter-21.html#214_Git这篇是详细介绍Git的,中间有一部分是怎么去搭建,你可以看下
4. 如何在Linux环境下建立Git Server并设置用户
我的演示环境是一个Amazon EC2的instance,安装好了Ubuntu 16.04,并且已经安装有git。
我们想达到的目的是这样的:
建立一个名为/srv/git的文件夹,对于我们当前项目,会有一个相应的my_project.git文件夹,我们可以在本地使用git push origin master或者git pull origin master命令来推送或者取回相应的文件。
首先git是存在多种protocol的,我们使用的是ssh。所以在建立用户的时候,我们需要建立相应的public key及private key,并且将public key添加到服务器端.ssh/authorized_keys文件中(稍后会介绍如何操作)。
5. Linux系统中怎么安装Git
一、使用包管理器安装Git
Git已经被所有的主流Linux发行版所支持。所以安装它最简单的方法就是使用各个Linux发行版的包管理器。
1、Debian, Ubuntu, 或 Linux Mint
$sudoapt-getinstallgit
2、Fedora, CentOS 或 RHEL
$sudoyuminstallgit或$sudodnfinstallgit
3、Arch Linux
$sudopacman-Sgit
4、OpenSUSE
$sudozypperinstall闭型git
5、Gentoo
$emerge--ask--verbosedev-vcs/git
二、从源码安装Git
如果由于某些原因,希望从源码安装Git,按照如下介绍操作。
1、安装依赖包
在构建Git之前,先安装它的依赖包。
//Debian,Ubuntu或LinuxMint
$sudoapt-getinstalllibcurl4-gnutls-devlibexpat1-devgettextlibz-devlibssl-devasciidocxmltodocbook2x
//Fedora,CentOS或RHEL
$sudoyuminstallcurl-develexpat-develgettext-developenssl-develzlib-develasciidocxmltodocbook2x
2、从github官网下载最新版本的Git。然后在/usr下构建和安装。
注意,如果打算安装到其大滑他目录下(例如:/opt),那就把“--prefix=/usr”这个配置命令使用其他路径替换掉。
$cdgit-x.x.x
$滚态腊makeconfigure
$。/configure--prefix=/usr
$makealldocinfo
$sudomakeinstallinstall-docinstall-htmlinstall-info
6. Linux系统上怎样安装Git
用git --version命令检查是否已经安装
7. 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.
剩下的推送就简单了。