Git 服務器搭建與客戶端安裝

大綱:html

1、前言git

2、搭建Git服務器github

  • yum安裝Git服務器shell

  • 建立一個git用戶,用來運行git服務vim

  • 建立客戶端登陸證書安全

  • 初始化Git倉庫bash

  • 禁用shell登陸 服務器

  • 克隆遠程倉庫ssh

3、安裝客戶端ide

  • Windows 客戶端

  • Linux 客戶端

4、總結

注,測試機 CentOS 5.5 x86_64,Git 服務器版本:git version 1.8.2.1,客戶端版本:git version 1.9.2.msysgit.0。全部軟件請到這裏下載:http://msysgit.github.io/


1、前言

在上一篇博客中咱們主要講解Git是什麼,Git的基本原理,講解一些有關Git的基礎知識,讓咱們簡單的瞭解一下Git,固然咱們還有不少問題沒有搞清楚,嘿嘿!無論理怎麼樣,在咱們弄清楚這些問題以前,咱們得有臺Git服務器是吧,嘿嘿!好了,下面咱們就來一塊兒搭建一臺Git服務器。


2、搭建Git服務器

1.yum安裝Git服務器

[root@git ~]# cd src/
[root@git src]# wget http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
[root@git src]# rpm -ivh epel-release-5-4.noarch.rpm 
Preparing...                ########################################### [100%] 
    package epel-release-5-4.noarch is already installed
[root@git ~]# yum list
[root@git ~]# yum install -y git

2.建立一個git用戶,用來運行git服務

[root@git ~]# adduser git

3.建立客戶端登陸證書

注,收集全部須要登陸的用戶的公鑰,就是他們本身生成的id_rsa.pub文件,把全部公鑰複製到/home/git/.ssh/authorized_keys文件裏,一行一個。嘿嘿!

1).客戶端生成id_rsa.pub文件的命令

$ ssh-keygen -t rsa
$ cat  .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NwUHeNNi+PC6KlrcJrXXDmKxRMmgHIPp79sgX6zqfdSlmNj7rBPQeyEKS9Wg8yI6jd8aG2jsUx99Vjti2VK2vEXKkRHxwID7ri69gE71RfDtv6ekafnzLo14J8hAp0spMk+N3wEAQRYDmcYo1wmnm/jMBedGrHj4NJQ1vYy1hVtJasGMSzjcMrlz9qvaluWnQ5tQjKFQVVwKsRRRzs8qTvzVhLJt4NQ+CAN45tqfsRuf58Uba9QNK7/6xSUiIKXQiILz8PMGJ3MnlV+eN3wx2aeztdevxu9plggtG05SMmd8GNVzXrN1IaxXSvz0UwjQ2kygu7aCqO8AZWH49rouw== leo@LEO-PC

注,一路回車便可,將生成的id_rsa.pub,複製給管理員,幫你在服務器上增長一下,下次你用git時就不須要輸入用戶名和密碼了。

2).查看服務器上authorized_keys文件

[root@git ~]# cat /home/git/.ssh/authorized_keys 
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAwMU4FKB6NRc3XSoIunWsdvVVuSoncbVb5Al6lB3ciswBVd++YmJFhqwkITNGccrO5sycROs9+Fbjgd6oBSzNuaBtCIbwNNsEyM/henTl2euI3XsnJQ/ITr6c/q0P3WoGl4E2QFQ2kZqs+1eDC0CgHcBrqvFv1Jr414sVYK9lfZwIF+jDdtaBOrSJuq1Agx9pGUFUEB4tQfkXxsWm/MvOmKAVvduKDE1eenUEL9zzyeELPcSXLe3NOoTjZhkX6EEXxQR1ZiZRFywLpfM4qopZ10to2KIUyVtzw6hx6V3cg7kn40lYVW0EAMATw9dVldwcRUI+kJzJSKUTKDVSwY3/+Q== root@CHENMINGQIAN 
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAsmmJuR+KhRSpdSirCiL30RA8WbfgicY1z7itWVnKHJW6hTuJFhzruY59FilVjJR1hbQBluP9JnK3XPSK9PSg+bwiJ2iQRa39rXck35r+trVOLyNbPyfKVRfOemD8YuykMlyr5JeW8gZjsHEuLnJ8//RiCiYzd3RT/SSUQ4yawDoIIWkz3eUSL09xoCRZFBsAp+S/LD3vx2MN+FNOHwvqcE+yK3oRNIqjWwLoKE0e5TRnqNgrPQ95PJYB3XPUulzaeMwsWPZs7jIUMl/5yEqSgAkioa8SVMOsikYJG/erv99NNVgFmpCBIiWqKEGkNrIpYzLLHDSYQ4g5Gbci/RZ54Q== Administrator@WIN2003X323 
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NwUHeNNi+PC6KlrcJrXXDmKxRMmgHIPp79sgX6zqfdSlmNj7rBPQeyEKS9Wg8yI6jd8aG2jsUx99Vjti2VK2vEXKkRHxwID7ri69gE71RfDtv6ekafnzLo14J8hAp0spMk+N3wEAQRYDmcYo1wmnm/jMBedGrHj4NJQ1vYy1hVtJasGMSzjcMrlz9qvaluWnQ5tQjKFQVVwKsRRRzs8qTvzVhLJt4NQ+CAN45tqfsRuf58Uba9QNK7/6xSUiIKXQiILz8PMGJ3MnlV+eN3wx2aeztdevxu9plggtG05SMmd8GNVzXrN1IaxXSvz0UwjQ2kygu7aCqO8AZWH49rouw== leo@LEO-PC

說明:我這裏有三個用戶登陸服務器,因此我這裏就有三個ssh-rsa,你們能夠看一下。

4.初始化Git倉庫

注,先選定一個目錄做爲Git倉庫,這裏是/data/git/project.git。

[root@git ~]# cd /data/git/
[root@git git]# git init --bare project.git
[root@git project.git]# ls
branches  config  description  HEAD  hooks  index  info  objects  refs

執行以上命令 Git命令,會建立一個裸倉庫,裸倉庫沒有工做區,由於服務器上的Git倉庫純粹是爲了共享,因此不讓用戶直接登陸到服務器上去改工做區,而且服務器上的Git倉庫一般都以.git結尾。而後,把owner改成git:

[root@git git]# chown -R git.git project.git
[root@git git]# ls -l 
總計 4 
drwxr-xr-x 7 git git 4096 05-09 13:50 project.git

5.禁用shell登陸

注,出於安全考慮,第二步建立的git用戶不容許登陸shell,這能夠經過編輯/etc/passwd文件完成。找到相似下面的一行:

[root@git ~]# cat /etc/passwd | grep git
git:x:1001:1001:git version control:/home/git:/bin/bash
改成:
[root@git ~]# vim /etc/passwd
git:x:1001:1001:git version control:/home/git:/usr/bin/git-shell

這樣,git用戶能夠正常經過ssh使用git,但沒法登陸shell,由於咱們爲git用戶指定的git-shell每次一登陸就自動退出。

6.克隆遠程倉庫

注,如今能夠經過git clone命令克隆遠程倉庫了,在各自的電腦上運行:

git

注,$ git clone git@git.jjhh.com:/data/git/project.git,其中git用戶名,git.jjhh.com服務器,/data/git/prgject.git是倉庫路徑。好了,到這裏服務器的搭建到這裏就完成了,下面咱們來安裝一下客戶端。


3、安裝客戶端

1.Windows 客戶端

1).下載客戶端

注,你們到這裏下載http://msysgit.github.io/。下面簡單演示一下安裝過程,比較簡單:

1

2

3

4

5

好了,到這裏就安裝完成了,安裝好之後會在桌面上有個圖標,你雙擊打開便可。以下圖:

6

2.Linux 客戶端

注,Linux客戶端安裝就比較簡單了,直接用yum安裝一下就好!

[root@test ~]# yum install -y git

到這裏git就安裝完成了,下面咱們查看一下版本:

[root@test ~]# git --version
git version 1.8.2.

下面咱們生成公鑰並複製到服務器上:

[root@test ~]# ssh-keygen -t rsa
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:
48:3c:22:76:02:f1:a2:e5:27:22:cb:4f:a7:a0:98:9d root@test.com
[root@test ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAwrmgNAca77BKzXl6BP11fdaclAoWfW+x4kPDZMPj1zFoML9JfOAS7DGY9quLMitwwfg5+U7zP/egPmAhSKCECubmodzMucnRSkJGgKCt1SlctRNDRN3OIvrecAtdZfDcBaWKLjaVzGPmhweLhr6HzfqeZU09Ccis4yK3RMwip2f+K1ZZVIOKUmL4AVFl9dqtxnQ4HlW61PUxvM/ug2M68Z0jQk5DVG8w5+dvGqX1qr5YABD1NX5Jz6aUbVVx7yyoORENxu6SK83GI/V7eOkrvBMDh9nDdwvDhPhuhBDSfE+Xi92hPJPKdNroF1vx8HMvpHME20MmjSwavPrz+B18MQ== root@test.com

下面咱們複製id_rsa.pub裏的公鑰到服務器的authorized_keys文件中。

[root@git ~]# su git
bash-3.2$ cd
bash-3.2$ vim .ssh/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K7/6xSUiIKXQiILz8PMGJ3MnlV+eN3wx2aeztdevxu9plggtG05SMmd8GNVzXrN1IaxXSvz0UwjQ2kygu7aCqO8AZWH49rouw== leo@LEO-PC
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAwrmgNAca77BKzXl6BP11fdaclAoWfW+x4kPDZMPj1zFoML9JfOAS7DGY9quLMitwwfg5+U7zP/egPmAhSKCECubmodzMucnRSkJGgKCt1SlctRNDRN3OIvrecAtdZfDcBaWKLjaVzGPmhweLhr6HzfqeZU09Ccis4yK3RMwip2f+K1ZZVIOKUmL4AVFl9dqtxnQ4HlW61PUxvM/ug2M68Z0jQk5DVG8w5+dvGqX1qr5YABD1NX5Jz6aUbVVx7yyoORENxu6SK83GI/V7eOkrvBMDh9nDdwvDhPhuhBDSfE+Xi92hPJPKdNroF1vx8HMvpHME20MmjSwavPrz+B18MQ== root@test.com

下面咱們clone一個倉庫到本地的目錄中。

[root@test ~]# cd /data/dev
[root@test dev]# git clone git@git.jjhh.com:/data/git/project.git
Cloning into 'project'...
The authenticity of host 'git.jjhh.com (114.112.173.150)' can't be established.
RSA key fingerprint is ca:ec:a2:7e:e6:89:ca:19:d3:93:7f:4b:c3:c0:c7:fd.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'git.jjhh.com,114.112.173.150' (RSA) to the list of known hosts.
remote: Counting objects: 50, done.
remote: Compressing objects: 100% (42/42), done.
remote: Total 50 (delta 21), reused 0 (delta 0)
Receiving objects: 100% (50/50), 4.02 KiB, done.
Resolving deltas: 100% (21/21), done.
[root@test dev]# ls
project
[root@test dev]# cd project/
[root@test project]# ls
index.html

好了,到這裏咱們的git客戶端到這裏就所有安裝完成了。


4、總結

Q&A,有什麼問題歡迎你們交流討論。最後,但願你們有所收穫^_^……

相關文章
相關標籤/搜索