CentOS环境Git服务器搭建并配置公钥访问简单测试

CentOS环境Git服务器搭建并配置公钥访问简单测试


DATE: 2017-09-04 17:03:23

2017-10-26 更新使用私钥时不输入密码

IP地址 用 xxx.xxx.xxx.xx 代替

1. Git服务器环境搭建(Server端)

| SSH方式登录服务器

xiaoqw@ubuntu:~$ ssh root@xxx.xxx.xxx.xx

l 安装开发套装

[root@GitServer ~]# yum groupinstall "Development Tools"
[root@GitServer ~]# yum install gettext-devel openssl-devel perl-CPAN perl-devel zlib-devel

l 创建Git用户组

[root@GitServer ~]# groupadd gituser
[root@GitServer ~]# useradd -g gituser  -d /home/gituser -m -s /bin/bash gituser
[root@GitServer ~]# passwd gituser

l Git初始化配置

[root@GitServer ~]$ cd /home/gituser/
[root@GitServer gituser]$ mkdir project.git
[root@GitServer gituser]$ cd project.git/
[root@GitServer project.git]$ ls
[root@GitServer project.git]$ git --bare init
Initialized empty Git repository in /home/gituser/project.git/

2. 新用户公钥访问管理(Server端)

密钥生成过程

[xiaoqw @GitServer ~]$ ssh-keygen

默认一路回车即可。

Generating public/private rsa key pair.
Enter file in which to save the key (/home/gituser/.ssh/id_rsa):
Created directory '/home/gituser/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/gituser/.ssh/id_rsa.
Your public key has been saved in /home/gituser/.ssh/id_rsa.pub.
The key fingerprint is:
0a:72:ee:86:4e:d9:b1:df:e7:8f:ad:1d:e9:56:86:60 gituser@GitMISAS
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|          E      |
|  . +   S. . .   |
|   * + .    ..o  |
|  o.+ .     oo   |
| ..... .  .=..   |
| .... . .o++=    |
+-----------------+ 

生成的内容

[gituser@GitServer ~]$ ls .ssh/
id_rsa  id_rsa.pub
[gituser@GitServer ~]$ touch ~/.ssh/authorized_keys
[gituser@GitServer ~]$ ls .ssh/
id_rsa  id_rsa.pub authorized_keys

其中,id_rsa.pub是公钥,id_rsa是私钥。 authorized_keys是存储可访问用户公钥的文件。

配置RSA认证(用于后续不使用密码登录)

Host *
    RSAAuthentication yes
    PubkeyAuthentication yes

客户端用户向管理员提交公钥(Client端)

生成密钥

相同的操作,在Client端生成密钥。方法同Server,一路回车。

Administrator@xiaoqw-nb MINGW6$ ssh-keygen -t rsa -C "name@163.com"

提交公钥

如果是平常申请使用的用户:

将Client端的rd_rsa.pub文件邮件发送给管理员,由管理员开通。

如果是搭建Git服务器的管理员新建的测试用户:

那么将这个文件Copy到服务器上即可。 Copy 方式:

Administrator@xiaoqw-nb MINGW64 ~/.ssh
$ scp ~/.ssh/id_rsa.pub root@xxx.xxx.xxx.xx:/tmp/
The authenticity of host 'xxx.xxx.xxx.xx (xxx.xxx.xxx.xx)' can't be established.
ECDSA key fingerprint is SHA256:/ELvbKWFBtogUFil88zA0YMNljckDTyIUjxkiTRNlhc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'xxx.xxx.xxx.xx' (ECDSA) to the list of known hosts.
root@xxx.xxx.xxx.xx's password:
id_rsa.pub                                    100%  405    84.7KB/s   00:00

服务端为Git用户分配权限(Server端)

将客户端的密钥写入密钥存储文件:

[gituser@GitServer ~/.ssh]$ cat /tmp/id_rsa.pub >> authorized_keys

客户端使用测试(Client端)

配置客户端免密登录

$ ssh-add ~/.ssh/id_rsa

Clone仓库

$ git clone gituser@xxx.xxx.xxx.xx:project.git
Cloning into 'project'...
gituser@172.31.102.72's password: 【注:配置免密后不用输入密码】
warning: You appear to have cloned an empty repository.

此时,代码库是空的。配置用户信息并创建工程。

Administrator@xiaoqw-nb MINGW64 ~/.ssh
$ git config --global user.name "xiaoqw"
$ git config --global user.email "dreamstone_xiaoqw@163.com"

创建文件

$ cd project/
$ ps > firstfile

提交文件

$ ls
firstfile
$ git add firstfile
warning: LF will be replaced by CRLF in firstfile.
The file will have its original line endings in your working directory.
$ git commit
Aborting commit due to empty commit message.

Administrator@xiaoqw-nb MINGW64 ~/.ssh/project (master)
$ git add firstfile
$ git commit -m "test file" #代码提交信息
[master (root-commit) 07c57c1] test file
 1 file changed, 4 insertions(+)
 create mode 100644 firstfile
$ git push
gituser@xxx.xxx.xxx.xx's password:
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 356 bytes | 178.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To xxx.xxx.xxx.xx:project.git
 * [new branch]      master -> master

至此,Git公钥登录方式配置使用完成。

关于使用

Linux操作系统:

安装Git后直接在Terminal命令行操作即可, git clone gituser@xxx.xxx.xxx.xx:project.git

Windows操作系统:

需要安装 Git for Windows 工具,打开Git Bash操作。 project目录下,需要配置邮箱和姓名,然后执行操作如gitlog等。否则会有错误: fatal: bad default revision 'HEAD'

需要执行 git commit 解决问题。

常用功能 命令 示例
获取代码 git clone git clone gituser@xxx.xxx.xxx.xx:project.git
查看状态 git status -s
新增文件 git add
显示日志 git log
提交更新 git commit -m "msg" git commit -m "add log logic"
配置用户名 git config --global user.name "xiaoqw"
配置邮箱 git config --global user.email "xiaoqw@mail.com.cn"
提交至远端 git remote add origin gituser@xxx.xxx.xxx.xx:project.git git push origin master

更多操作,请参考:

Git官网手册https://git-scm.com/docs 网络教程 http://www.runoob.com/git/git-tutorial.html

版权声明

FindFor


首发 FindFor,转载请附链接!

赞赏支持

感谢支持!


建站不易,感谢支持!

推荐阅读
libnfc安装配置(Linux)
## 1、安装依赖库 ```shell sudo apt-get install libusb-dev libpcsclite-dev libusb libpcsclite1 libccid pcscd ``` ## 2、下载libnfc 选择一个libnfc存放路径,执行git命令下载libnfc ```shell git clone https://github.com/nfc-tools/l
138

dirsearch简单使用
` dirsearch`是一个用于在Web服务器上枚举目录和文件的工具,支持多线程,具有一些启发式算法,能够识别隐藏目录。以下是使用`dirsearch`的一些基本步骤: 1. **安装dirsearch**: 首先,确保你的系统上已经安装了Python。然后,可以通过以下方式安装`dirsearch`: `git clone https://gi
32

dirb使用
`dirb`(Directory Buster)是一个用于在Web服务器上枚举目录和文件的工具。它通过发送HTTP请求并分析响应来探测目标主机上存在的目录和文件。`dirb`的主要目的是帮助安全测试人员和黑客发现目标网站上隐藏的目录,这些目录可能包含敏感信息或安全漏洞。 以下是一些`dirb`工具的基本用法和参数: 1. **基本用法**: `dirb http://targ
40

ubuntu20.04.4安装golang语言
## 1、官网下载安装包 Go下载 - Go语言中文网 - Golang中文社区 https://studygolang.com/dl ## 2、将下载下来的安装包解压到env路径下 ``` tar xf go1.18.linux-amd64.tar.gz ``` ## 3、添加环境变量 通过将Go目录的位置添加到$PATH环境变量中,系统将知道在何处可以找到Go可执行二进制文件。 ``` vim
91

升级 Linux 服务器上的 TLS 版本至 1.2 或更高版本
## 1、检查当前 TLS 版本: 首先,您需要确认当前服务器上所使用的 TLS 版本。您可以通过运行以下命令来检查 OpenSSL 版本和支持的 TLS 版本: ```shell openssl version openssl ciphers -v ``` 如果您的 OpenSSL 版本较旧,可能需要升级 OpenSSL 以支持更高的 TLS 版本。升级 OpenSSL 的具体步骤取决于您所使用
51

茶馆注册商标需要注册哪几个类别?
茶馆注册商标需要综合考虑多个方面,以下是详细介绍: ### 商标查询与评估 在进行茶馆商标注册之前,首先需要进行商标查询与评估,以确定所申请商标是否已经被他人注册或使用,避免侵权纠纷。可以通过中国国家知识产权局商标局的官方网站进行查询。 ### 明确商标注册类别 - **核心类别** - **第30类**:主要包括咖啡、茶、可可和咖啡代用品;米;食用淀粉和西米;面粉和谷类制品;面包、糕
142

咖啡馆注册商标应该注册哪几类?咖啡馆还有配套的公众号和小程序,商标是否需要追加类型?
## 咖啡馆注册商标应该注册哪几类? 咖啡馆注册商标时,需要综合考虑核心业务、相关产品及未来发展可能涉及的领域,以下为你详细介绍需要重点关注的商标类别: ### 核心经营服务类 - **第43类**:提供食物和饮料服务;临时住宿。 - **重要子类别**: - 4301 组包含咖啡馆、餐厅、自助餐厅、快餐馆等服务。咖啡馆日常经营提供饮品、餐食等服务,此类别是必须注册的核心
325

[保姆级] Vue3 开发文档
#### 获取 this `Vue2` 中每个组件里使用 this 都指向当前组件实例,this 上还包含了全局挂载的东西、路由、状态管理等啥啥都有 而 Vue3 中没有 this,如果想要类似的用法,有两种,一是获取当前组件实例,二是获取全局实例,如下自己可以去打印出来看看 ```vue import { getCurrentInstance } from 'vue' // proxy
251

【转载】树莓派时间同步方法,来自博客园
树莓派系统时间不对在《初识树莓派》一文最后一张截图中其实隐藏了一个我没有太在意的时间问题,今天在开发树莓派监控程序的时候才发现。从图中我们可以知道上次登录时间是1970年1月1日星期四的00:03:17分,由此我们可以推断,新安装系统的树莓派默认系统时间应该是1970年1月1日开始的。很明显,这个时间与我们期望的当前时间是不符的。我们需要将系统时间修改为和当前时间同步。第一步:启用网络时间协议为了
365

Web前端开发众包资源共享,探索者联盟兼职开发者招募
# 项目情况 联盟接到合作伙伴的开发需求,有一系列的APP要开发。有很多个APP哦。 ## 需求描述 1. 参考指定APP,基于H5+Vue开发页面。 2. APP并不复杂,无需使用脚手架,cdn引入开发即可。 ## 交付要求 1. 源码交付 2. 代码风格良好,可顺利二次开发 3. 只要满足代码质量要求,能正常走通业务逻辑即可,无严格的APP测试步骤。 ## 预算 具体预算看APP复杂度。目前普
575

探索互联网新机遇,加入精英社群,共创辉煌职业篇章!
【探索互联网新机遇,加入精英社群,共创未来!】 在这个日新月异的互联网时代,每一处都蕴藏着无限可能与创新机遇。我们诚邀您成为“探索者联盟”的一员,与我们一起深度剖析市场动态,精准把握行业趋势,共同开启一段激动人心的职业旅程! 加入我们,您将享受到: - **市场分析**:深度解读互联网行业最新动态,洞悉市场先机。 - **职业规划**:一对一专业指导,定制个性化职业发展路径,助力您步步高升。
336

计算机基础算法篇(二)—— 对称加密算法
##对称加密算法   在对称加密算法中,数据发信方将明文(原始数据)和加密密钥一起经过特殊加密算法处理后,使其变成复杂的加密密文发送出去。收信方收到密文后,若想解读原文,则需要使用加密用过的密钥及相同算法的逆算法对密文进行解密,才能使其恢复成可读明文。在对称加密算法中,使用的密钥只有一个,发收信双方都使用这个密钥对数据进行加密和解密,这就要求解密方事先必须知道加密密钥。 ##常
571