Skip to content

Instantly share code, notes, and snippets.

@nardhar
Last active June 14, 2021 20:49
Show Gist options
  • Save nardhar/5e71ba214c5e176e6f5529ff54fd7b14 to your computer and use it in GitHub Desktop.
Save nardhar/5e71ba214c5e176e6f5529ff54fd7b14 to your computer and use it in GitHub Desktop.
Git Server CentOS 7

Configure Git Server on CentOS 7

Server

$ yum install git-core
$ sudo useradd git
$ sudo passwd git
$ sudo su git
$ cd
$ mkdir .ssh
$ touch .ssh/authorized_keys

Client (Local Machine)

$ ssh-keygen -t rsa
$ cat ~/.ssh/id_rsa.pub | ssh git@remote-server "mkdir -p ~/.ssh && cat >>  ~/.ssh/authorized_keys"

Server

$ sudo su git
$ mkdir -p /home/git/project-1.git
$ cd /home/git/project-1.git
$ git init --bare
Initialized empty Git repository in /home/git/project-1.git

Client

$ mkdir -p /home/user/dev/project
$ cd /home/user/dev/project
$ git init
Initialized empty Git repository in /home/user/dev/project
$ git add . // Normal work with git
$ git commit -m "blah blah"
$ git remote add origin git@remote-server:project-1.git
$ git push origin master

if git server asks for password then review /var/log/secure file in server

Server

$ tail -f /var/log/secure

and search for

Authentication refused: bad ownership or modes for directory /home/git/.ssh
or
Authentication refused: bad ownership or modes for file /home/git/.ssh/authorized_keys

To solve this problem we need to change mode and permission of .ssh folder and authorized_keys file and restart sshd.service

Server

$ sudo su git
$ chmod 600 /home/git/.ssh
$ chmod 700 /home/git/.ssh/authorized_keys

Change to root or sudoer

$ sudo vi /etc/ssh/sshd_config

Make sure PubkeyAuthentication is set to yes

Restart sshd.service as root or sudoer

$ systemctl restart sshd.service // restart ssh service

and push again

Client

$ git push origin master

NOTE: Double check permissions on .ssh/ and .ssh/authorized_keys, I was sure I did it the first time, but I checked it again and it was another, setting them up to 700 and 600 respectively allowed me to push without entering a password

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment