ssh git服务器_如何:通过SSH的Git服务器

ssh git服务器

Git and SSH are both powerful tools, and git/ssh work well together. We introduce how to set up git server via ssh in this post. Git server through SSH is easy and fast to set up, although every user will have access to all repositories in the git server over SSH and every user is the git administrator. This is okay for a small group of git members who trust each other. But for better privilege control, you should try gitolite or gitosis.

Git和SSH都是强大的工具,并且git / ssh可以很好地协同工作。 在本文中,我们介绍如何通过ssh设置git服务器。 尽管每个用户都可以通过SSH访问git服务器中的所有存储库,并且每个用户都是git管理员,但是通过SSH设置Git服务器非常容易且快速。 对于一小撮彼此信任的git成员来说,这是可以的。 但是,为了更好地进行权限控制,您应该尝试使用甘醇二甲醚或gitosis。

If you need to set up a git server for multiple users which may contain contributors and administrator, you may try gitolite as recommended by the gitolite author. You may check out How to Set Up A Gitolite Git Server – A Ten-Minute Tutorial for how to set up a git server using gitolite.

如果您需要设置多个用户的Git服务器可能含有贡献者和管理员,您可以尝试gitolite所建议的gitolite作者 。 您可以查看如何设置Gitolite Git服务器-十分钟教程 ,了解如何使用gitolite设置git服务器。

If you prefer gitosis, please refer to: Setting Up a Git Server Using Gitosis and Managing Repositories on Git Server Using Gitosis. The gitosis proves quit stable from my experience, although it does not have as many features as gitolite.

如果您喜欢gitosis,请参阅: 使用Gitosis设置Git服务器并使用Gi​​tosis 管理Git服务器上的存储库 。 根据我的经验,虽然它不具有像乙醇钠榴石一样多的特征,但事实证明它确实很稳定。



In this post how to set up a basic git server and a more complex one (the git server is a server inside of a local area network) will be introduced.

在这篇文章中,将介绍如何设置基本的git服务器和更复杂的服务器(git服务器是局域网内的服务器)。

通过SSH教程的基本git服务器 (A basic git server through SSH tutorial)

In this part we will build up a git server through ssh connection. We use ssh to pull or push data from or to git server. The git server can be directly connected. Suppose that we set up git server on machines example.org.

在这一部分中,我们将通过ssh连接构建一个git服务器。 我们使用ssh从git服务器中拉出或推送数据。 git服务器可以直接连接。 假设我们在机器example.org上设置了git服务器。

服务器端git用户和主目录 (Server side git user and home)

logon to the git server by ssh username@example.org. username is the account name that have administrator privilege (or can sudo) on the git server.
Install git package

通过ssh username@example.org登录git服务器。 username是在git服务器上具有管理员特权(或可以是sudo)的帐户名。
安装git包

# yum install git

Add the user for git

为git添加用户

# useradd -m -d /home/git -u 1005 git

Configure the git user’s shell

配置git用户的shell

# vim /etc/passwd

Here we assume git’s home directory is /home/git. Then we can change git’s shell from /bin/bash to /usr/bin/git-shell to forbid logging in for a shell for the git account. It can be made by editing /etc/passwd, but this is not suggested. One good method (thanks to victor) is to use the usermod command:

这里我们假设git的主目录是/ home / git。 然后,我们可以将git的shell从/ bin / bash更改为/ usr / bin / git-shell以禁止登录git帐户的shell。 可以通过编辑/ etc / passwd来完成,但是不建议这样做。 一种好方法(感谢victor )是使用usermod命令:

# usermod -s /usr/bin/git-shell git

However, there may be problem. To make this work, the /usr/bin/git-shell should be put into /etc/shells to avoid “user ‘git’ has invalid shell, rejected” error. (Thanks to Tiago for this)

但是,可能有问题。 为了使此工作有效,应将/ usr / bin / git-shell放入/ etc / shells中,以避免“用户'git'具有无效的shell,被拒绝”错误。 (为此感谢Tiago

In addition, COMMAND_DIR (the path “$HOME/git-shell-commands”) must exist and any of the executables in it can be invoked. The user must have read and execute permissions to the directory in order to execute the programs in it. Hence, we should create the COMMAND_DIR in git’s home and give read and execute permission to git:

此外, COMMAND_DIR (路径“ $ HOME / git-shell-commands”)必须存在,并且其中的任何可执行文件都可以被调用。 用户必须具有对目录的读取和执行权限,才能在其中执行程序。 因此,我们应该在git的主目录中创建COMMAND_DIR并授予git的读取和执行权限:


# cd /home/git/
# mkdir git-shell-commands
# chmod 755 git-shell-commands
将id_rsa.pub添加到git的.ssh / authorized_keys (Add id_rsa.pub to git’s .ssh/authorized_keys)

log on to git server, using the account that have root or sudo privilege

使用具有root或sudo特权的帐户登录git服务器

ssh username@example.org

Copy pub key to a temp directory

将发布密钥复制到临时目录


# cp .ssh/id_rsa.pub /dev/shm/

operate in git’s home as root

以root身份在git的家中进行操作

# cd /home/git/.ssh

backup before changing is a good habit

更改之前先备份是个好习惯

# cp authorized_keys authorized_keys.bak

append pub key to git’s autorized keys list

将发布密钥附加到git的授权密钥列表中


# cat /dev/shm/id_rsa.pub >> authorized_keys
创建仓库 (Create repository)

log on example.org using the account that can edit git’s files.

使用可以编辑git文件的帐户登录example.org。

If you have set the git user account’s shell to git-shell on the git server, you need to add the -s /bin/bash in the su command to use bash as the shell instead of git-shell.

如果您在git服务器上将git用户帐户的shell设置为git-shell ,则需要在su命令中添加-s /bin/bash ,以使用bash作为shell而不是git-shell

Create the repository directory (as the git user on server)

创建存储库目录(作为服务器上的git用户)


# su -s /bin/bash - git
$ cd ~
$ mkdir example.git

Initial the repository, –bare means only objects is stored on server (as the git user on server)

初始存储库,–bare表示仅对象存储在服务器上(作为服务器上的git用户)


# su -s /bin/bash - git
$ cd ~/example.git
$ git --bare init
第一次提交: (First commit:)

The first commit and push on local machine will create the initial repository.
Initialize the local repository

首次提交并在本地计算机上推送将创建初始存储库。
初始化本地存储库

$ mkdir example
$ cd example
$ git init

Add a initial empty README file

添加一个初始的空自述文件

$ touch README

Add README to the repository

将自述文件添加到存储库

$ git add README

Commit the changes (adding a file)

提交更改(添加文件)

$ git commit -m 'first commit'

Add the remote git repository address

添加远程git存储库地址

$ git remote add origin ssh://git@example.org/~/example.git

Push the commit to remote repository

将提交推送到远程存储库

$ git push origin master
编程时: (When programming:)

We need to clone the repository for one time:

我们需要克隆存储库一次:

$ git clone ssh://git@example.org/~/example.git

Then every time we want to edit some files:

然后,每次我们要编辑一些文件时:

$ cd example
$ git pull  # pull the newest version from the repository

After changing some files:

更改某些文件后:

$ git commit -a -m 'msg'  # commit the changes with a message msg
$ git push # push the changes to the repository


通过SSH教程更复杂的git服务器 (A more complex git server through SSH tutorial)

In this part we will build up a git server through ssh connection. We use ssh to pull or push data from or to git server. The git server is inside of a local area network. We use port forwarding to connect to it. Suppose that we set up git server on virtual machines vm111, the gateway server of the net work which vm111 is inside of is gate.example.org, and port 22111 on gate.example.org is port forwarded to vm111:22.

在这一部分中,我们将通过ssh连接构建一个git服务器。 我们使用ssh从git服务器中拉出或推送数据。 git服务器在局域网内部。 我们使用端口转发来连接它。 假设我们建立的git服务器上的虚拟机vm111,网工作,这是vm111内的网关服务器gate.example.org,gate.example.org端口22111端口转发vm111:22。

服务器端git用户和主目录 (Server side git user and home)

logon to the git server by ssh username@gate.example.org -p 22111. username is the account name that can sudo on the git server.

通过ssh username@gate.example.org -p 22111登录git服务器。 username是可以在git服务器上sudo的帐户名。


# yum install git
# useradd -m -d /home/git -u 1005 git
# vim /etc/passwd

Then change git’s shell from /bin/bash to /usr/bin/git-shell to forbid logging on for a shell for the git account. And remember to set the /etc/shells file (refer to the “basic git” section above).

然后将git的shell从/ bin / bash更改为/ usr / bin / git-shell以禁止登录git帐户的shell。 并记住设置/ etc / shells文件(请参阅上面的“基本git”部分)。

将id_rsa.pub添加到git的.ssh / authorized_keys (Add id_rsa.pub to git’s .ssh/authorized_keys)

ssh gate.example.org -p 22111  # log on to vm111, using the account that can sudo
# cp .ssh/id_rsa.pub /dev/shm/    # copy pub key to a temp directory
# su -s /bin/bash - git                        # operate in git's hom
$ cd /home/git/.ssh
$ cp authorized_keys authorized_keys.bak   # backup before changing is a good habit
$ cat /dev/shm/id_rsa.pub >> authorized_keys # append pub key to git's authorized keys list
创建仓库 (Create repository)

log on gate.example.org -p 22111 # using the account that can sudo

使用可以sudo的帐户登录gate.example.org -p 22111


# su -s /bin/bash git
$ cd /home/git
$ mkdir example.git    # the repository directory
$ cd example.git
$ git --bare init      # initial the repository, --bare means only objects is stored on server
第一次提交: (First commit:)

on local laptop:

在本地笔记本电脑上:

$ mkdir example
$ cd example
$ git init
$ touch README
$ git add README
$ git commit -m 'first commit'
$ git remote add origin ssh://git@gate.example.org:22111/~/example.git
$ git push origin master
编程时: (When programming:)

We need to clone the repository for one time:

我们需要克隆存储库一次:

$ git clone ssh://git@gate.example.org:22111/~/example.git

Then every time we want to edit some files:

然后,每次我们要编辑一些文件时:

$ cd example
$ git pull  # pull the newest version from the repository

After changing some files:

更改某些文件后:

$ git commit -a -m 'msg'  # commit the changes with a message msg
$ git push # push the changes to the repository

翻译自: https://www.systutorials.com/set-up-git-server-through-ssh-connection/

ssh git服务器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值