Git: Set Up a Local Repository Accessible by LAN
A Git repository simplifies the sharing of code to a team. Many teams opt to go the GitHub route but there might be an occasion when you need to spin up a quick repository that is only available to those team members working on your LAN.
When you need to deploy a Git repository on your LAN and you need to give other team members access to it, the goal is to do it quickly and securely. Thanks to git and Secure Shell (SSH), this isn’t nearly as challenging as you might think. And although this setup might not be an option for team members who work outside of your LAN, it’s great for a temporary repository offered to those within your company network.
How does it work? Let me show you.
What You’ll Need
To make this work, you’ll need the following:
- A Linux machine with Git installed.
- An SSH key pair.
- A user with sudo privileges (if the minimum requirements aren’t installed).
That’s it. Let’s make some Git magic.
Installing Git
On the off-chance Git isn’t installed, here’s how you can take care of that:
- Ubuntu-based distributions –
sudo apt-get install git -y
- Fedora-based distributions –
sudo dnf install git -y
- Arch-based distributions –
sudo pacman -S git
Create a Git User and Add the Required SSH Keys
First, we’re going to be talking about a remote and local machine. The remote machine will be the machine that houses the repository and the local machine(s) will access the remote repository.
Once Git is installed, you’ll want to add a specific user for authentication (because you don’t want to give other team members your username and password) on any/all remote machines that will access the repository. To create the new user, issue the command:
1 |
sudo adduser git |
Make sure to give the user a strong/unique password and answer the remaining questions.
Change to the new user with the command:
1 |
su git |
You should now be in the Git user’s home directory. To make sure of that, issue the command:
1 |
cd |
Create the necessary directory that will house the authorized keys with the command:
1 |
mkdir .ssh |
Give that directory the necessary permissions with the following:
1 |
chmod 700 .ssh |
Create the authorized_keys file with:
1 |
touch .ssh/authorized_keys |
Give that file the necessary permissions with the following:
1 |
chmod 600 .ssh/authorized_keys |
Next, head over to the local machine and view the contents of the id_rsa.pub
file for the user who’ll be accessing the remote repository with the command:
1 |
cat /home/USER/.ssh/id_rsa.pub |
Where USER is the Linux username that will be working with the git repository.
If you’ve not already created SSH keys, the command for this is:
1 |
ssh-keygen |
Copy the contents of that file.
Go back to the remote machine and open the authorized_keys file with:
1 |
nano /home/git/.ssh/authorized_keys |
In that file, paste the content from the id_rsa.pub file and save it.
So far so good.
Creating the Repository
It’s now time to create the actual repository. On the remote machine, issue the command:
1 |
mkdir /home/git/repository |
Note: You can name the directory anything you like.
Change into that directory with the command:
1 |
cd /home/git/repository |
Next, create a directory for a project with the command:
1 |
mkdir my_project |
Again, you can name the project directory whatever you like.
Change into that new directory with:
1 |
cd my_project |
Initialize the bare repository with the command:
1 |
git init --bare --shared |
The bare option creates a repository with no content and the shared option ensures multiple people will be able to access it.
All right.
Your repository is ready to be cloned from the remote machine to the local machine.
Cloning the Repository
Go back to the local machine and clone the new repository with the command:
1 |
git clone git@SERVER:/home/git/repository/my_project |
You should see the following in the output:
Cloning into ‘my_project’…
warning: You appear to have cloned an empty repository.
You can verify the remote repository with:
1 |
git remote -v |
You should see something like this:
origin git@192.168.1.166:/home/git/repository/my_project (fetch)
origin git@192.168.1.166:/home/git/repository/my_project (push)
Now that we’ve verified, let’s create a README file on the local machine. From within the my_project directory, issue the command:
1 |
nano README |
You can add whatever information to that file you need. Once you’ve done that, save and close the file. Next, add the file to staging with:
1 |
git add --all |
You’ll then need to create your first commit with:
1 |
git commit -m "Added README file" |
Finally, push the changes to the remote repository with:
1 |
git push origin master |
You should see the README file in the repository on the remote machine. If not, you might have to check out the master branch (on the remote machine) with the command:
1 |
git checkout master |
Let’s try one more thing.
On the remote machine, open the newly added README file with:
1 |
nano /home/git/repository/my_project/README |
Change the contents of that file (you can add your name or anything you like).
Save and close the file.
Place the file into staging with the following:
1 |
git add . |
Add a commit with:
1 |
git commit -m "Edited README file" |
Back on the local machine issue the command:
1 |
git pull |
When the pull completes, you can view the contents of the README and see that the changes are present.
And that’s all there is to creating a LAN-accessible Git repository. Make sure you add the id_rsa.pub
key from any local machine to the authorized_keys file on the repository machine for those who need access and you should be good to go.