Create a local Git repository on Linux using SSH
Have you ever found yourself in a situation where you had to deploy a quick git repository on a machine on your LAN? If you’re a developer, chances are you’ve been given this task many times.
Luckily, you don’t really have to depend on a bunch of overly complicated software packages or third-party tools to make this happen. Of course, if you want a nice GUI to make life much easier for everyone who will contribute to your development project, you can turn to a platform like Gitea. But if you prefer the simplicity and ease of deployment that comes with the command line, you’re going to want to try this method. It may not have all the bells and whistles of web options, but it gets the job done reliably and quickly.
So how do you deploy this magical repository? With the help of git and SSH. Both of these tools are available for free for all Linux distributions, so you don’t have to worry about searching or paying for the tools.
Sounds like a winner? I thought so. Make sure that this happens.
All the things you will need
For this to work, you will need a Linux distribution to host the repository, another machine to use the repository, and a user with sudo privileges. I will demo on Ubuntu Server 22.04 but I will also show how to install the software on a RHEL/Fedora based Linux distribution.
Ready? I thought so.
Install the necessary software
The first thing we are going to do is install the necessary software. The only two packages you need are SSH and Git. Most likely, SSH is already installed (as is the case with almost every Linux distro on the planet). Git, on the other hand, may not be found on your machine. So, to install Git on an Ubuntu-based distribution, you need to run the command:
sudo apt-get install git -y
sudo apt–obtain install git –there
If you are on a Red Hat Enterprise Linux/Fedora based distro, this command would be:
One thing to keep in mind is that you need to install git on both the machine hosting the repository and the one using the repository.
Create a special user
We will now create a special account that will be used for the purposes of this repository. For this, the command will be the same, whatever distribution you are using, and is done on the machine hosting the repository. To create the user, run the command:
The adduser command should ask you to set a password for the user. Otherwise, do it with:
Switch to this new user with:
Navigate to the git user’s home directory with:
Now we need to create an .ssh directory with the command:
Give this directory the appropriate permissions with:
The next step is to create the authorized_keys file with the command:
to touch .shh/authorized_keys
Give this file the required permissions with:
chmod 600 .ssh/authorized_keys
chmod 600 .shh/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 that you will use to access the repository. To do this, connect to the client machine and run the command:
Once the key is generated, you will 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:
Navigate to this 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.
Access this folder with:
Initialize the repository with:
Clone the new repository
Return to the client machine and clone the new repository with the command:
git clone git@SERVER:/home/git/git_repo/PROJECT
git clone git@WAITER:/residence/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 will be prompted to enter the SSH key password. After successful authentication, the contents of the project folder will be extracted to your local computer.
You can now add your first commit (let’s add a README) and push the changes back to the repository with:
touch README git add –all git commit -m “Added README file” git push origin master
to touch READ ME
git to add —everything
git commit –m “README file added”
git to push origin Master
So. You have just created a quick Git repository. If you need to give other users access to the repository, just ask them to create SSH keys on their machines and copy them to the server the same way you did above.
Congratulations on setting up a Git repository and having it up and running in minutes.
Comments are closed.