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:
1 |
sudo apt-get install git -y |
If you’re on an Red Hat Enterprise Linux/Fedora-based distribution, that command would be:
1 |
sudo dnf install git -y |
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:
1 |
sudo adduser git |
The adduser command should require you to set a password for the user. If not, do so with:
1 |
sudo passwd git |
Change to this new user with:
1 |
su git |
Change into the git user’s home directory with:
1 |
cd |
Now, we need to create an .ssh directory with the command:
1 |
mkdir .ssh |
Give that directory the proper permissions with:
1 |
chmod 700 .ssh |
The next step is to create the authorized_keys file with the command:
1 |
touch .ssh/authorized_keys |
Give that file the required permissions with:
1 |
chmod 600 .ssh/authorized_keys |
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:
1 |
ssh-keygen |
Once the key is generated, you’ll need to copy the contents of the key. First view the key with:
1 |
cat /home/USER/.ssh/id_rsa.pub |
Where USER is your username.
Back on the machine hosting the repository open the authorized_keys file with:
1 |
nano /home/git/.ssh/authorized_keys |
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:
1 |
mkdir /home/git/git_repo |
Change into that new directory with the command:
1 |
cd /home/git/git_repo |
Create a new project folder with a command like this:
1 |
mkdir PROJECT |
Of course, you can name the folder whatever you like.
Change into that folder with:
1 |
cd PROJECT |
Initialize the repository with:
1 |
git --init bare |
Clone the New Repository
Head back to the client machine and clone the new repository with the command:
1 |
git clone git@SERVER:/home/git/git_repo/PROJECT |
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:
1 2 3 4 |
touch README git add --all git commit -m "Added README file" git push origin master |
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.