## 4.4 配置服务器
使用`authorized_keys`方法来对用户进行认证。
**设置认证**
创建一个操作系统用户`git`,并为其建立一个`.ssh`目录:
```
$ sudo adduser git
$ su git
$ cd
$ mkdir .ssh && chmod 700 .ssh
$ touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys
```
为系统用户`git`的`authorized_keys`文件添加一些开发者 SSH 公钥:
```
$ cat /tmp/id_rsa.john.pub >> ~/.ssh/authorized_keys
```
为开发者新建一个空仓库:
```
$ cd /opt/git
$ mkdir project.git
$ cd project.git
$ git init --bare
```
假定这个设置了`git`用户和 Git 仓库的服务器使用`gitserver`作为主机名。 同时,该服务器运行在内网,并且已在 DNS 配置中将`gitserver`指向此服务器。
那么任意一个拥有权限的用户,均可以推送本地库进行远程库的初始化:
```
# on John's computer
$ cd myproject
$ git init
$ git add .
$ git commit -m 'initial commit'
$ git remote add origin git@gitserver:/opt/git/project.git
$ git push origin master
```
其他开发者可以克隆此仓库,并推回各自的改动:
```
$ git clone git@gitserver:/opt/git/project.git
$ cd project
$ vim README
$ git commit -am 'fix for the README file'
$ git push origin master
```
注意:目前所有(获得授权的)开发者用户都能以系统用户`git`的身份登录服务器从而获得一个普通 shell。 如果你想对此加以限制,则需要修改`passwd`文件中(`git`用户所对应)的 shell 值。
**设置权限**
借助一个名为`git-shell`的受限 shell 工具,你可以方便地将用户`git`的活动限制在与 Git 相关的范围内。
首先确保`git-shell`已存在于`/etc/shells`文件中:
$ cat /etc/shells # see if `git-shell` is already in there. If not...
$ which git-shell # make sure git-shell is installed on your system.
$ sudo vim /etc/shells # and add the path to git-shell from last command
使用`chsh <username>`命令修改任一系统用户的 shell:
```
$ sudo chsh git # and enter the path to git-shell, usually: /usr/bin/git-shell
```
这样,用户`git`就只能利用 SSH 连接对 Git 仓库进行推送和拉取操作,而不能登录机器并取得普通 shell。
- 介绍
- 第一章 起步
- 1.1 关于版本控制
- 1.2 Git 简史
- 1.3 Git 基础
- 1.4 命令行
- 1.5 安装 Git
- 1.6 初次运行 Git 前的配置
- 1.7 获得帮助
- 第二章 基础
- 2.1 获取仓库
- 2.2 记录每次更新到仓库
- 2.3 查看提交历史
- 2.4 撤销操作
- 2.5 远程仓库的使用
- 2.6 打标签
- 2.7 Git 别名
- 第三章 分支
- 3.1 分支简介
- 3.2 分支的新建与合并
- 3.3 分支管理
- 3.4 分支开发工作流
- 3.5 远程分支
- 3.6 变基
- 第四章 服务器上的 Git
- 4.1 协议
- 4.2 在服务器上搭建 Git
- 4.3 生成 SSH 公钥
- 4.4 配置服务器
- 4.5 Git 守护进程
- 4.6 Smart HTTP
- 4.7 GitWeb
- 4.8 GitLab
- 4.9 第三方托管的选择
- 第五章 分布式 Git
- 5.1 分布式工作流程
- 5.2 向一个项目贡献
- 5.3 维护项目
- 第六章 GitHub
- 6.1 账户的创建和配置
- 6.2 对项目做出贡献
- 6.3 维护项目
- 6.4 管理组织
- 6.5 脚本 GitHub
- 第七章 Git 工具
- 7.1 选择修订版本
- 7.2 交互式暂存
- 7.3 储藏与清理
- 7.4 签署工作
- 7.5 搜索
- 7.6 重写历史
- 7.7 重置揭密
- 7.8 高级合并
- 7.9 Rerere
- 7.10 使用Git调试
- 7.11 子模板
- 7.12 打包
- 7.13 替换
- 7.14 凭证存储
- 第八章 自定义 Git
- 8.1 配置 Git
- 8.2 Git 属性
- 8.3 Git 钩子
- 8.4 使用强制策略的一个例子
- 第九章 Git 与其他系统
- 9.1 作为客户端的 Git
- 9.2 迁移到 Git
- 第十章 Git 内部原理
- 10.1 底层命令和高层命令
- 10.2 Git 对象
- 10.3 Git 引用
- 10.4 包文件
- 10.5 引用规格
- 10.6 传输协议
- 10.7 维护与数据恢复
- 10.8 环境变量
