# 架設伺服器

現在我們過一邊伺服器端架設 SSH 訪問的流程。本例將使用 `authorized_keys` 方法來給用戶授權。我們還將假定使用類似 Ubuntu 這樣的標準 Linux 發行版本。首先，創建一個名為 'git' 的用戶，並為其創建一個 `.ssh` 目錄。

```
$ sudo adduser git
$ su git
$ cd
$ mkdir .ssh
```

接下來，把開發者的 SSH 公開金鑰添加到這個用戶的 `authorized_keys` 檔中。假設你通過電郵收到了幾個公開金鑰並存到了暫存檔案裡。重複一下，公開金鑰大致看起來是這個樣子：

```
$ cat /tmp/id_rsa.john.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCB007n/ww+ouN4gSLKssMxXnBOvf9LGt4L
ojG6rs6hPB09j9R/T17/x4lhJA0F3FR1rP6kYBRsWj2aThGw6HXLm9/5zytK6Ztg3RPKK+4k
Yjh6541NYsnEAZuXz0jTTyAUfrtU3Z5E003C4oxOj6H0rfIF1kKI9MAQLMdpGW1GYEIgS9Ez
Sdfd8AcCIicTDWbqLAcU4UpkaX8KyGlLwsNuuGztobF8m72ALC/nLF6JLtPofwFBlgc+myiv
O7TCUSBdLQlgMVOFq1I2uPWQOkOWQAHukEOmfjy2jctxSDBQ220ymjaNsHT4kgtZg2AYYgPq
dAv8JggJICUvax2T9va5 gsg-keypair
```

只要把它們逐個追加到 `authorized_keys` 檔案結尾部即可：

```
$ cat /tmp/id_rsa.john.pub >> ~/.ssh/authorized_keys
$ cat /tmp/id_rsa.josie.pub >> ~/.ssh/authorized_keys
$ cat /tmp/id_rsa.jessica.pub >> ~/.ssh/authorized_keys
```

現在可以用 `--bare` 選項運行 `git init` 來建立一個裸倉庫，這會初始化一個不包含工作目錄的倉庫。

```
$ cd /opt/git
$ mkdir project.git
$ cd project.git
$ git --bare init
```

這時，Join，Josie 或者 Jessica 就可以把它加為遠端倉庫，推送一個分支，從而把第一個版本的專案檔案上傳到倉庫裡了。值得注意的是，每次添加一個新專案都需要通過 shell 登入主機並創建一個裸倉庫目錄。我們不妨以 `gitserver` 作為 `git` 用戶及項目倉庫所在的主機名稱。如果在網路內部運行該主機，並在 DNS 中設定 `gitserver` 指向該主機，那麼以下這些命令都是可用的：

```
# 在 John 的電腦上
$ 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 服務。

作為一個額外的防範措施，你可以用 Git 自帶的 `git-shell` 工具限制 `git` 用戶的活動範圍。只要把它設為 `git` 用戶登入的 shell，那麼該用戶就無法使用普通的 bash 或者 csh 什麼的 shell 程式。編輯 `/etc/passwd` 檔：

```
$ sudo vim /etc/passwd
```

在檔末尾，你應該能找到類似這樣的行：

```
git:x:1000:1000::/home/git:/bin/sh
```

把 `bin/sh` 改為 `/usr/bin/git-shell` （或者用 `which git-shell` 查看它的實際安裝路徑）。該行修改後的樣子如下：

```
git:x:1000:1000::/home/git:/usr/bin/git-shell
```

現在 `git` 用戶只能用 SSH 連接來推送和獲取 Git 倉庫，而不能直接使用主機 shell。嘗試普通 SSH 登錄的話，會看到下面這樣的拒絕資訊：

```
$ ssh git@gitserver
fatal: What do you think I am? A shell?
Connection to gitserver closed.
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://willh.gitbook.io/gitpro/3898c8362c3f1668ad4cfd6649caa064/23d7f72c238fdc7a4d29067374ba3091.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
