(1)前提:
找一台服务器,首先要安装Git
yum install git
git config --global user.name "lsk" //配置基本信息
git config --global user.email "example@qq.com"
(2)添加git用户,并且设置shell为/usr/bin/git-shell(目的是为了不让git用户远程登陆)
[root@fengxi ~]# useradd -s /usr/bin/git-shell git
(3)创建文件存放客户端机器上的公钥
[root@fengxi ~]# cd /home/git
[root@fengxi git]# mkdir .ssh
[root@fengxi git]# touch .ssh/authorized_keys
更改所属组
[root@fengxi git]# chown -R git.git .ssh
设置权限
[root@fengxi git]# chmod 600 .ssh/authorized_keys
查看
[root@fengxi git]# ll /home/git/.ssh/authorized_keys
-rw-------. 1 git git 0 Nov 10 08:01 /home/git/.ssh/authorized_keys
(4)创建存放gitpub的目录并初始化
//会创建一个裸仓库,裸仓库没有工作区,因为服务器上的Git仓库纯粹是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git结尾
[root@fengxi git]# mkdir /data/gitpub
[root@fengxi git]# cd /data/gitpub
[root@fengxi gitpub]# git init --bare sample.git
Initialized empty Git repository in /data/gitpub/sample.git/
[root@fengxi gitpub]# chown -R git.git sample.git
[root@fengxi gitpub]# ll
total 4
drwxr-xr-x. 7 git git 4096 Nov 10 08:07 sample.git
//以上操作是在Git服务器上操作,平时Git服务器是不需要开发人员登录修改代码的,它仅仅是充当着一个服务器的角色,就像GitHub一样,平时操作都是在我们自己的机器上操做的。
(5)/在客户端上克隆远程仓库
查看客户端的公钥
[root@localhost ~]# 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:
61:9d:03:a7:a2:8b:07:20:f6:d7:bc:31:88:b0:c2:21 root@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
| . . |
| = . |
|E+ . + + |
|=.= ..+o . . |
|.o.o.o =S |
|. o.. + |
| . o . |
| . |
| |
+-----------------+
[root@localhost ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCb71GBt5OgmkZRGiqxNWxWmQx7Jx+Gnzn52GmP6ASuveZPE0ykoRBCRL9mzC8tdB5fmxptKbL0TMESFYXjtRz6OcoGyfXsI84vnLau/pqLxtcY7WJMcB6Ooot42xAET1kqgRnyAtRAKMxNjCKJdPGFrAlwL8VuWCH4RntoP5/e3P8AcTPxYV3UYF/e1KyJ9seCL/Z4PxC9EaEvZPIZGHM73eU2ahGAP2MjDEvuTiwAfNnKJHsF71+pHGVeRVHd5yr8tpWKPJZorQH0B6Mo/zOjbMZGHCAbJG+RR6hbxVKZjsxeNyy4ArELd4bnj7t2kS63fi+nivxjbHp9kKiEMk7Z root@localhost.localdomain
在服务端
要把客户端上的公钥放到git服务器/home/git/.ssh/authorized_keys文件里
[root@fengxi ~]# cd /home/git
[root@fengxi git]# vi .ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCb71GBt5OgmkZRGiqxNWxWmQx7Jx+Gnzn52GmP6ASuveZPE0ykoRBCRL9mzC8tdB5fmxptKbL0TMESFYXjtRz6OcoGyfXsI84vnLau/pqLxtcY7WJMcB6Ooot42xAET1kqgRnyAtRAKMxNjCKJdPGFrAlwL8VuWCH4RntoP5/e3P8AcTPxYV3UYF/e1KyJ9seCL/Z4PxC9EaEvZPIZGHM73eU2ahGAP2MjDEvuTiwAfNnKJHsF71+pHGVeRVHd5yr8tpWKPJZorQH0B6Mo/zOjbMZGHCAbJG+RR6hbxVKZjsxeNyy4ArELd4bnj7t2kS63fi+nivxjbHp9kKiEMk7Z root@localhost.localdomain
客户端和服务端分别关闭的防火墙
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@localhost ~]# setenforce 0
在客户端
[root@localhost ~]# ls /opt
myproject
[root@localhost ~]# cd /opt
[root@localhost opt]# git clone git@192.168.200.25:/data/gitpub/sample.git
Cloning into 'sample'...
warning: You appear to have cloned an empty repository. //报警告提示空仓库
[root@localhost opt]# ls
myproject sample
[root@localhost opt]# cd sample/
查看
[root@localhost sample]# ll -a
total 4
drwxr-xr-x. 3 root root 17 Nov 19 04:06 .
drwxr-xr-x. 4 root root 35 Nov 19 04:06 ..
drwxr-xr-x. 7 root root 4096 Nov 19 04:06 .git