Setup Git Server in CentOS 6.3

0. Environment:

Server machine: CentOS 6.3 x86

Client machine: Windows 10 Pro x86_64

 

1. Install ssh server

[server machine shell]
#yum install openssh openssh-server
#chkconfig sshd on #/etc/init.d/sshd start

2. Create user git

[server machine shell]
#useradd git
#passwd git

3. Create an empty repository

[server machine shell]
#cd /home/git
#git init --bare sample.git
#chown -R git:git sample.git

4. Conifigure ssh client in client machine

Download Git for Windows from https://git-for-windows.github.io/

Direct link(for 64bit system): https://github.com/git-for-windows/git/releases/download/v2.6.1.windows.1/Git-2.6.1-64-bit.exe

Install it to default location(C:\Program Files\Git), add C:\Program Files\Git\usr\bin to system environment PATH

Adding C:\Program Files\Git\usr\bin to PATH to enable command ssh and ssh-keygen in cmd.exe

Replace the  email address with yours to generate public & private ssh keys

[client machine cmd]
>ssh-keygen -t rsa -C "my_email@hotmail.com"

Press Enter three times to accept default setting, sample output

Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/<your account name>/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/<your account name>/.ssh/id_rsa.
Your public key has been saved in /c/Users/<your account name>/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:+GR+6jWy7FtN3hFBszUCq08nk3fzlIBPzI0AbTkwOo4 my_email@hotmail.com
The key's randomart image is:
+---[RSA 2048]----+
|         ++ooo+..|
|        . .=* +=.|
|       o  .o.*o. |
|      o.. . + ...|
|     E..S. =.+.+.|
|       =  o+=..oo|
|        + =.o . .|
|       . B .     |
|       oB.       |
+----[SHA256]-----+

5. Transfer client machine's public key to server machine to enable login without password

[client machine cmd]
>ssh git@<server address>

$midkr .ssh
$chmod 700 .ssh
$cd .ssh
$touch authorized_keys
$chmod 600 authorized_keys

Sample output

The authenticity of host '<server address> (<server address>)' can't be established.
RSA key fingerprint is SHA256:hsFlk18MlWP3wtNVGKcBrZijZMvCjsdlhJg/SPAn9Z0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '<server address>' (RSA) to the list of known hosts.
git@<server address>'s password:
Last login: Sun Oct 18 07:58:35 2015 from <client ip>
[git@localhost ~]$ mkdir .ssh
[git@localhost ~]$ chmod 700 .ssh
[git@localhost ~]$ cd .ssh
[git@localhost .ssh]$ touch authorized_keys
[git@localhost .ssh]$ chmod 600 authorized_keys

Append client's public key to server's authorized_keys file by copy-and-paste

[client machine cmd -> connected to server via ssh]
$vi ~/.ssh/authorized_keys

client machine public key location: %USERPROFILE%\.ssh\id_rsa.pub

server machine authorized_keys file location: /home/git/.ssh/authorized_keys

Sample authorized_keys file

[git@localhost ~]$ vi .ssh/authorized_keys

Logout from server and connect again to test, if ssh doesn't prompt for password, your configuration for ssh is correct.

 6. Clone remote repository

[client machine cmd]
>git clone git@<server address>:sample.git

7. Test

Create a README.md file and push to server repository

[client machine cmd]
>touch README.md
>notepad README.md       #input some content into README.md, then save
>git add README.md
>git commit -m "Intial commit for sample project"
>git push origin master

8 Done. if you want to submit code in another machine, you need to:

  a. Append the public key of the new machine to server's authorized_keys file

  b. Clone repository to new machine

  c. Modify the files in new machine local repository, and push to server(remote repository)

 


 

If you don't want the user git login into server via shell, you can modify the type of shell for this user

[server machine shell]
#vi /etc/passwd

Locate to the end of file, change  git:**********/bin/bash to git:**********/usr/bin/git-shell 

After that, user still can use name git to push their code to server, but login via ssh is denied

C:\Users\<account name>>ssh git@<server address>
Last login: Sun Oct 18 10:42:30 2015 from <client ip>
fatal: What do you think I am? A shell?
Connection to 192.168.1.109 closed.

C:\Users\<account name>>

 

posted @ 2015-10-18 17:22  pangyujie  阅读(406)  评论(0编辑  收藏  举报