Where are you using WebAssembly?
Wasm promises to let developers build once and run anywhere. Are you using it yet?
At work, for production apps
At work, but not for production apps
I don’t use WebAssembly but expect to when the technology matures
I have no plans to use WebAssembly
No plans and I get mad whenever I see the buzzword
CI/CD / Linux / Open Source / Software Development

Create a Local Git Repository on Linux with the Help of SSH

Have you ever found yourself in a situation where you need to deploy a quick Git repository on a machine within your LAN? Here's what to do.
Nov 5th, 2022 6:00am by
Featued image for: Create a Local Git Repository on Linux with the Help of SSH

Have you ever found yourself in a situation where you need to deploy a quick Git repository on a machine within your LAN? If you’re a developer, chances are pretty good this task has fallen into your lap on several occasions.

Fortunately, you don’t really need to depend on a bunch of overly complicated software packages or third-party tools to make this happen. Sure, if you want a sweet GUI to make the lives of all those who will contribute to your development project considerably easier, you might turn to a platform like Gitea. But if you prefer the simplicity and ease of deployment that comes along with the command line, you’re going to want to give this method a go. It might not have all the bells and whistles of the web-based options but it does the job reliably and quickly.

And so, how do you deploy this magical repository? With the help of git and SSH. Both of these tools are freely available to all Linux distributions, so you don’t have to worry about searching out or paying for the tools.

Sound like a winner? I thought so. Let’s make it happen.

All the Things You’ll Need

To make this work, you’re going to need a Linux distribution to host the repository, another machine to use the repository, and a user with sudo privileges. I’ll be demonstrating on Ubuntu Server 22.04 but I’ll also show how to install the software on an RHEL/Fedora-based Linux distribution.

Ready? I thought so.

Instal the Necessary Software

The first thing we’ll do is install the necessary software. The only two packages you need are SSH and Git. Most likely SSH is already installed (as it is with nearly every Linux distribution on the planet). Git, on the other hand, may not be found on your machine. So, to install Git on a Ubuntu-based distribution, you’d issue the command:

If you’re on an Red Hat Enterprise Linux/Fedora-based distribution, that command would be:

One thing to keep in mind is that you must install git on the machine hosting the repository and the one using the repository.

Create a Special User

We’re now going to create a special account that will be used for the purposes of this repository. For that, the command will be the same, regardless of what distribution you use, and is done on the machine hosting the repository. To create the user, issue the command:

The adduser command should require you to set a password for the user. If not, do so with:

Change to this new user with:

Change into the git user’s home directory with:

Now, we need to create an .ssh directory with the command:

Give that directory the proper permissions with:

The next step is to create the authorized_keys file with the command:

Give that file the required permissions with:

Create an SSH Key on the Local Machine and Copy It to the Repository Machine

Next, we need to create an SSH key on the machine you’ll use to access the repository. For that, log into the client machine and issue the command:

Once the key is generated, you’ll need to copy the contents of the key. First view the key with:

Where USER is your username.

Back on the machine hosting the repository open the authorized_keys file with:

Paste the contents of the SSH key into this file and save/close it.

Create the New Repository

On the machine hosting the repository, create a new directory with the command:

Change into that new directory with the command:

Create a new project folder with a command like this:

Of course, you can name the folder whatever you like.

Change into that folder with:

Initialize the repository with:

Clone the New Repository

Head back to the client machine and clone the new repository with the command:

Where  SERVER is the IP address of the server hosting the repository and PROJECT is the name of the project folder.

You’ll be prompted for the SSH key password. Upon successful authentication, the contents of the project folder will be pulled to your local machine.

You can now add your first commit (let’s add a README) and push the changes back to the repository with:

There you go. You’ve just created a quick Git repository. If you need to give other users access to the repository, you should only need to have them create SSH keys on their machines and copy them to the server in the same way you did above.

Congratulations on getting a Git repository up and running in mere minutes.

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.