๐ŸŽ‰ Level Up! Setting Up Your First Git Server on CentOS Stream 9!

๐ŸŽ‰ 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 the git user can read, write, and execute in that folder.
  • touch: Creates the authorized_keys file.
  • chmod 600: Makes sure only the git user 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/git folder to house everything.
  • We made the git user the owner (chown).
  • The init --bare command creates the server-side repo. We name it with the .git suffix 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:

  1. 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.
  2. Firewall Configuration: Make sure your CentOS Stream 9 firewall (firewalld) is properly configured to allow incoming SSH connections (usually port 22).
  3. Multiple Users: Learn how to manage multiple developers’ SSH keys in the authorized_keys file so your whole team can collaborate.

Ready to start pushing code to your new Git home? Let the development begin! ๐Ÿ’ป


Leave a Reply

Your email address will not be published. Required fields are marked *


Translate ยป