添加用户,并且指定shell为/usr/bin
[root@localhost gitroot]# useradd -s /usr/bin/git-shell git
创建文件存放其他机器的公钥
[root@localhost gitroot]# cd /home/git
[root@localhost git]# mkdir .ssh
[root@localhost git]# cd .ssh/
[root@localhost .ssh]# touch authorized_keys
[root@localhost .ssh]# cd ..
更改所属组
[root@localhost git]# chown -R git.git .ssh
设置权限
[root@localhost git]# chmod 600 .ssh/authorized_keys
查看
[root@localhost git]# ll /home/git/ .ssh/authorized_keys
-rw-------. 1 git git 0 Dec 29 20:19 .ssh/authorized_keys
创建存放gitpub的目录并初始化
[root@localhost git]# mkdir /data/gitpub
[root@localhost git]# cd /data/gitpub
[root@localhost gitpub]# git init --bare sample.git
Initialized empty Git repository in /data/gitpub/sample.git/
##会创建一个裸仓库,裸仓库没有工作区,因为服务器上的Git仓库纯粹是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git结尾
修改仓库的所属组和所属用户
[root@localhost gitpub]# chown -R git.git sample.git/
[root@localhost gitpub]# ll
total 4
drwxr-xr-x. 7 git git 4096 Dec 29 20:23 sample.git
以上操作是在Git服务器上操作,平时Git服务器是不需要开发人员登录修改代码的,它仅仅是充当着一个服务器的角色,就像GitHub一样,平时操作都是在我们自己的机器上操做的。
客户端远程克隆
[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:
00:70:3e:3b:1c:21:a5:11:12:02:10:0a:90:e2:eb:1e root@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
|&+=+= |
|*. * o |
|+ . + . |
| . . + . |
| . + S |
| . . |
|.E |
| .. |
|.. |
+-----------------+
[root@localhost ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCxvjjCFDvyMxCaV+JZhhVrT5ZWUDkgjyL1zXQlTX/Lbe1cxm4Jb+F0VTYcVMbsXAtCFEBShb3/2qdzYb+tBzs+U2krRhns7dvrhav7tVkK+ie2OXxoWvYyUXkeBAQfJF7de+IApSzZbEsa0XPxyrxy4Yy17EkPbMIGmU/qon8KF+00KbUiVmCpK+XN4HHJMMj+ybTsVuP6UcDDx37PVO3hMpcYxJ+/kcxdCAXySGJxZcAzgr0dzWRau0vQH7bQML5uaHeRt/yHp4BtpcdA10IwAxB3+jHpz5hdBIXZcVntBg25xd2PuQxwH9KJtzhMH92JDKPrLt5q/GYjp8+nq2n3 root@localhost.localdomain
在服务端
要把客户端上的公钥放到git服务器/home/git/.ssh/authorized_keys文件里
[root@localhost gitpub]# cd /home/git/
[root@localhost git]# vi .ssh/authorized_keys
sh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCxvjjCFDvyMxCaV+JZhhVrT5ZWUDkgjyL1zXQlTX/Lbe1cxm4Jb+F0VTYcVMbsXAtCFEBShb3/2qdzYb+tBzs+U2krRhns7dvrhav7tVkK+ie2OXxoWvYyUXkeBAQfJF7de+IApSzZbEsa0XPxyrxy4Yy17EkPbMIGmU/qon8KF+00KbUiVmCpK+XN4HHJMMj+ybTsVuP6UcDDx37PVO3hMpcYxJ+/kcxdCAXySGJxZcAzgr0dzWRau0vQH7bQML5uaHeRt/yHp4BtpcdA10IwAxB3+jHpz5hdBIXZcVntBg25xd2PuQxwH9KJtzhMH92JDKPrLt5q/GYjp8+nq2n3 root@localhost.localdomain
在客户端和服务端分别关闭的防火墙
[root@localhost git]# systemctl stop firewalld
[root@localhost git]# 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 git]# setenforce 0
在客户端
[root@localhost opt]# git clone git@192.168.100.11:/data/gitpub/sample.git
Cloning into 'sample'...
此时就可以在当前目录下生成一个sample的目录,这个就是我们克隆的远程仓库了。进入到这里面,可以开发一些代码,然后push到远程,比如git push origin master。