在阿里云的CENTOS上搭建GIT服务器

最近突然想把一些原来在本机的代码用git管理起来,本来想再windows上搭一个服务器,转而一想反正有一个阿里云的centos,干脆用起来吧,于是说干就干。

一、检查服务器版本是否自带git

[root@~]# rpm -qa git
git-1.8.3.1-6.el7_2.1.x86_64
language-shell hljs

如果现实git-版本号这种说明已经安装过了,如果没有使用下面的命令安装

yum install git 
language-shell hljs

二、创建git用户并设置密码

[root@]# useradd my2017
[root@]# passwd my2017
Changing password for user my2017.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

三、生成ssh公钥

[root@]# su my2017
[my2017@]$ cd ~
[my2017@ ~]$ ls
[my2017@ ~]$ pws
bash: pws: command not found
[my2017@ ~]$ pwd
/home/my2017
[my2017@ ~]$ mkdir .ssh
[my2017@ ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/my2017/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/my2017/.ssh/id_rsa.
Your public key has been saved in /home/my2017/.ssh/id_rsa.pub.
The key fingerprint is:
0e:b0:b2:b2:4e:8d:a6:08:12:ea:cb:55:6b:f5:64:e2 my2017@iZ2ze3jauhy9b4gdjn!2s2w1
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|    .            |
|     o           |
|. . ...oSo       |
|..oo. +o=        |
|=+.o o E..       |
|Xo. .            |
|==.              |
+-----------------+
[my2017@ ~]$ cd .ssh/
[my2017@ .ssh]$ cat id_rsa.pub >> ~/.ssh/authorized_keys
[my2017@ .ssh]$ exit
exit

四、创建git仓库

[root@ gitrepository]# mkdir mygit.git
[root@ gitrepository]# cd mygit.git/
[root@ mygit.git]# git --bare init
Initialized empty Git repository in /data/gitrepository/mygit.git/

五、修改权限

[root@ data]# chown -R my2017:my2017 gitrepository/
[root@ data]# cd gitrepository/
[root@ gitrepository]# ls -al
total 12
drwxr-xr-x 3 my2017 my2017 4096 Oct 29 23:14 .
drwxr-xr-x 6 root      root      4096 Oct 29 22:54 ..
drwxr-xr-x 7 my2017 my2017 4096 Oct 29 23:15 mygit.git
[root@ gitrepository]# cd mygit.git/
[root@ mygit.git]# ls -al
total 40
drwxr-xr-x 7 my2017 my2017 4096 Oct 29 23:15 .
drwxr-xr-x 3 my2017 my2017 4096 Oct 29 23:14 ..
drwxr-xr-x 2 my2017 my2017 4096 Oct 29 23:15 branches
-rw-r--r-- 1 my2017 my2017   66 Oct 29 23:15 config
-rw-r--r-- 1 my2017 my2017   73 Oct 29 23:15 description
-rw-r--r-- 1 my2017 my2017   23 Oct 29 23:15 HEAD
drwxr-xr-x 2 my2017 my2017 4096 Oct 29 23:15 hooks
drwxr-xr-x 2 my2017 my2017 4096 Oct 29 23:15 info
drwxr-xr-x 4 my2017 my2017 4096 Oct 29 23:15 objects
drwxr-xr-x 4 my2017 my2017 4096 Oct 29 23:15 refs

配置SSH key

六、本地测试

安装git.exe

https://git-for-windows.github.io/

有时候不好下,多刷新几遍。

安装 TortoiseGit

https://tortoisegit.org/

我的一贯宗旨是能鼠标点的绝逼不敲代码。。。。

tortoisegit报错提示:

Disconnected:No supported authentication methods available
(server sent:publickey,gssapi-keyex,gssapi-with-mic)

直接用git客户端报错:

Warning: Permanently added (ECDSA) to the list of known hosts.
my@: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
fatal: Could not read from remote repository.

解决方法:

禁止git用户登录

禁用git用户shell登陆

出于安全考虑,第二步创建的git用户不允许登录shell,这可以通过编辑/etc/passwd文件完成。

vim /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每次一登录就自动退出。

标签:GITCentos 发布于:2019-10-30 01:25:32