Configure Git Server with SSH on Ubuntu

If you have a small number of team members working on some projects, then you can setup a Git server via SSH on your office and work on projects as a team very easily. You don’t have to use GitHub or any other services in that case. SSH based Git server is really easy to setup and use. In this article, I am going to show you how to configure a Git server with SSH on Ubuntu and how to use it. So, let’s get started.

Configuring Git Server:

In this section, I am going to show you how to configure an Ubuntu server as a SSH accessible Git server.
First, update the APT package repository cache with the following command:

$ sudo apt update

The APT package repository cache should be updated.

Now, install OpenSSH server and Git with the following command:

$ sudo apt install openssh-server git

Now, press Y and then press <Enter> to confirm the installation.

OpenSSH server and Git should be installed.

Now, create a new user git with the following command:

$ sudo useradd –create-home –shell /bin/bash git

All the Git repositories will be saved in the home directory of the git user /home/git.

Now, login as the git user with the following command:

$ sudo sugit

Now, create a new directory .ssh with the following command:

$ mkdir .ssh

Now, allow only git user to have read, write, exec permissions on the directory .ssh/ as follows:

$ chmod 700 .ssh/

As you can see, the git user only has read (r), write (w), execute (x) permissions on the .ssh/ directory.

$ ls -ld .ssh/

Now, create a new empty file .ssh/authorized_keys as follows:

$ touch .ssh/authorized_keys

Only allow read and write to the file from the git user as follows:

$ chmod 600 .ssh/authorized_keys

As you can see, only the git user has read (r) and write (w) permissions to the file .ssh/authorized_keys.

In the .ssh/authorized_keys file, you have to add the public key of the users whom you want to access the Git repositories on the Git server.

Adding Client Public Key to the Git Server:

To access the Git repositories on the Git server, the client must add his/her public key to the Git server.

The client can generate a public-private key pair as follows:

$ ssh-keygen

Press <Enter>.

Press <Enter>.

Press <Enter>.

Press <Enter>.

Now, the client can find his/her public key as follows:

$ cat ~/.ssh/

Client’s public key should be printed. Now, the client can send this public key to the manager (who manages the Git server). The manager can then add the public key to the Git server. Then the client can access the Git server.

Let’s say, the client sent his/her public key to the Git server manager. The manager uploaded the public key to /tmp/ file on the Git server.

Now, the Git server manager can add the public key of the client as follows:

$ cat /tmp/ >> ~/.ssh/authorized_keys

Now, the .ssh/authorized_keys file should have the public key of the client.

Creating Git Repositories on the Server:

The clients can’t create new Git repositories on the server. The Git server manager must create a repository on the server. Then, the clients can clone, push/pull from the repository.

Now, create a new empty Git repository testrepo on the Git server as follows:

$ git init –bare testrepo

Now, the client only needs to know the IP address of the Git server in order to access the testrepo Git repository.

The Git server manager can find this information as follows:

$ ip a

As you can see, the IP address of the Git server is Now, the server manager can tell it to the clients who will be working on the project.

Cloning Git Repository from the Server:

Once the client knows the IP address and the Git repository name, he/she can clone it to his/her computer as follows:

$ git clone git@>testrepo

Now, type in yes and press <Enter>. You will need to do this once, only the first time.

The testrepo Git repository should be cloned from the server.

A new directory testrepo should be created.

Making Changes and Pushing Changes to Git Server:

Now, the client can add commits to the testrepo/ repository and push the changes to the Git server.

$ cd testrepo/

$ echo "Hello world" > test.txt

$ git add .

$ git commit -m ‘initial commit’
<a href=""><img class="aligncenter size-full wp-image-47672" src="" alt="" width="706" height="171" /></a>
[cc lang="bash"]
$ git push origin

Adding a New Team Member:

Now, let’s say, bob wants to contribute to the testrepo Git repository.

All he has to do is generate a SSH key pair and send the public key to the Git server manager.

$ ssh-keygen

Once the Git server manager has the public key of bob, he can upload it to the Git server and add it to the .ssh/authorized_keys file as follows:

$ cat /tmp/ >> ~/.ssh/authorized_keys

Now, bob can clone the testrepo Git repository from the server as follows:

$ git clone git@

testrepo should be cloned.

A new directory testrepo should be created in bob’s computer.

Now, bob can navigate to the Git repository as follows:

$ cd testrepo/

He should find some existing commits.

$ git log

Now, bob can do his own work and commit it. Then, push the changes to the server.

$ echo "Hello World 2" >> test.txt

$ git add .
$ git commit -m ‘Changed message’

$ git push origin

Now, other people working on the same repository can pull the changes as follows:

$ git pull origin

He/she should find the commits that bob made.

So, this is how you configure a Git Server with SSH on Ubuntu and use it. Thanks for reading this article.

Sandclock IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, Sandclock IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

How to Create Branches on Git

Git is one of the best version control system out there. It is very popular among almost every type of software developers...

22 Essential Git Commands

Git has become the quintessential version control system. The rise of Git’s popularity can be attributed to its speed,...

Basics of Git Merging and Deleting Branches

Branching can help you keep your work organized. However, you need to be able to merge your work in order to make the work...
Bài Viết

Bài Viết Mới Cập Nhật

Hướng dẫn chuyển đổi windows server windows evaluation to standard và active windows server 2008 + 2012 + 2016 + 2019

How to Update Ubuntu Linux

Squid Proxy Manager cài đặt và quản lý Proxy Squid tự động trên ubuntu

Hướng dẫn cài đặt Apache CloudStack

Hướng dẫn ký file PDF bằng chữ ký số (chữ ký điện tử) và sửa lỗi mới nhất 2021 foxit reader