
๐ Level Up! Setting Up Your First Git Server on CentOS Stream 9!
Hey there, fellow Linux explorers and coding enthusiasts! Ready to take your version control game to the next level? We’ve all been there: pushing code to a big, external service. But what if you could host your own projects, right on your freshly installed CentOS Stream 9 box? Imagine the control, the speed, and the sheer coolness of running your very own, private Git server!
This isn’t as complicated as it sounds. We’re going to walk through the initial setup to get a basic, secure, and fully functional Git repository host up and running. Let’s dive into the Linux terminal! ๐
๐ ๏ธ Step 1: Laying the Foundation โ Installing Git
First things first, our CentOS Stream 9 server needs to know what Git is! Since we’re using a modern distribution, the package manager (dnf) makes this a breeze.
Open your terminal and run:
Bash
sudo dnf install git -y
That’s it! Git is now installed. To make sure it’s happy, you can check the version:
Bash
git --version
If it shows a version number, you’re golden! ๐
๐ค Step 2: Creating the Dedicated Git User
Security is key! We don’t want people logging into our server using the root user or our personal admin account just to push code. Let’s create a specific, non-login user named git that will own all the repositories.
Run this simple command:
Bash
sudo useradd git
This command creates a new user, and by default, it can’t log in with a password. This is actually a good thing for security! We’ll use SSH keys for authentication, which is the gold standard for Git server access.
๐ Step 3: Secure Access with SSH Keys (The Secret Sauce!)
This is where the magic happens. We’ll set up the authorized_keys file so developers (which might just be you for now!) can push and pull code securely.
A. Switch to the git User
We need to set up the SSH directory and permissions under the new user:
Bash
sudo -i -u git
You are now the git user! Your prompt will change to reflect this.
B. Create the SSH Directory
Now, let’s make the required .ssh folder:
Bash
mkdir ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
Quick explanation:
mkdir: Creates the directory.chmod 700: Makes sure only thegituser can read, write, and execute in that folder.touch: Creates theauthorized_keysfile.chmod 600: Makes sure only thegituser can read and write to this file. Security first!
C. Add Your Public Key
This is the most crucial part. You need to get the public SSH key (usually named id_rsa.pub or similar) from your local computer and paste its entire contents into the server’s ~/.ssh/authorized_keys file.
On your local machine, you would run (copy the output!):
Bash
cat ~/.ssh/id_rsa.pub
Windows
type $env:USERPROFILE.ssh\id_rsa.pub
Then, back on the server (as the git user), edit the file:
Bash
vi ~/.ssh/authorized_keys
Paste your key on a new line, save, and exit. Note, id_rsa.pub and authorized_keys are the default, if you are first time doing this you might not have the keys yet, it’s great if you already have but here’s a tutorial (Forge Your Digital Key: Creating Secure SSH Keys!) if you don’t have them yet.
When you’re done, type exit to switch back to your regular admin user.
๐ฆ Step 4: Creating Your First Bare Repository
A “bare” repository is a special type of Git repository that doesn’t have a working tree (no actual files checked out). It’s what servers use to hold the history and metadata. Just in case, you forgot, the git user we created cannot login on it’s own so we didn’t have to create a password, and that means it won’t be able to do anything that requires sudo privileges, so before you proceed, you need to type exit to go back to the original user with the sudo access so you can proceed.
Let’s create a place for our repos and then the first one, perhaps for a “My Awesome Project”:
Bash
sudo mkdir /srv/git
# Ensure the top-level directory is owned by git
sudo chown -R git:git /srv/git
# Ensure the git user can access the contents
sudo chmod -R ug+rwX /srv/git
# Create an empty project
sudo git init --bare /srv/git/my_awesome_project.git
- We created the
/srv/gitfolder to house everything. - We made the
gituser the owner (chown). - The
init --barecommand creates the server-side repo. We name it with the.gitsuffix by convention.
๐ฅณ Step 5: Connecting From Your Local Machine!
The moment of truth! On your local machine (the one where you copied the public key from), you can now clone your brand new server repository!
If your server’s hostname or IP address is 192.168.1.100, your command will look like this:
Bash
git clone git@192.168.1.100:/srv/git/my_awesome_project.git
If everything worked, you’ll see a message about an empty repository, and a new folder named my_awesome_project will appear! You are officially hosting your own code! Give yourself a high five! ๐
What’s Next? The Adventure Continues!
You’ve built a rock-solid foundation for a private version control system. But the journey doesn’t end here!
Here are some awesome next steps to enhance your setup:
- Exploring Web Interfaces: Consider installing tools like Gitea or GitLab to give your server a beautiful, web-based UI for managing users and browsing repositories.
- Firewall Configuration: Make sure your CentOS Stream 9 firewall (
firewalld) is properly configured to allow incoming SSH connections (usually port 22). - Multiple Users: Learn how to manage multiple developers’ SSH keys in the
authorized_keysfile so your whole team can collaborate.
Ready to start pushing code to your new Git home? Let the development begin! ๐ป