리눅스 Git 서버 구축 (SSH 프로토콜)

1 개요[ | ]

Git 서버 구축
Git 저장소 생성
  • 여기서는 ssh 상에 git 서버를 구축하여 사용할 수 있는 방법을 소개한다.
  • ssh 데몬을 통해 서비스가 제공되므로(git over ssh) 별도의 데몬을 구동할 필요가 없다.
  • 서버에 git 계정을 만들어 클라이언트들의 key들을 미리 등록해둔다.
사실 key를 등록하지 않아도 사용가능하지만 git 작업시에 서버 패스워드를 입력해야 하므로 매우 불편하다.
  • 마지막 단계에서 서버의 git 계정의 쉘을 git-shell로 변경하여, git 기능만 사용가능하도록 권한을 제한한다.

2 사전 작업[ | ]

3 확인[ | ]

root@zetagit:~# git --version
git version 2.7.4
→ git은 설치되어 있음
root@zetagit:~# ll /opt | grep git
root@zetagit:~# cat /etc/passwd | grep git
root@zetagit:~#
→ /opt 아래 git 폴더도 없고, git라는 계정도 없음

4 저장소 생성[ | ]

root@zetagit:~# mkdir -p /opt/git/project.git
root@zetagit:~# git init --bare /opt/git/project.git/
Initialized empty Git repository in /opt/git/project.git/

5 계정 생성[ | ]

root@zetagit:~# useradd git -m -s /bin/bash
root@zetagit:~# echo 'git:ServerP@ssw0rd' | chpasswd
root@zetagit:~# cat /etc/passwd | grep git
git:x:1000:1000::/home/git:/bin/bash
root@zetagit:~# chown -R git:git /opt/git/

6 (클라이언트) git 서버로 키 등록[ | ]

  • 클라이언트에서 ssh 키 있는지 확인
testuser@zetaclient1:~$ ll ~/.ssh/id_rsa*
ls: cannot access /home/testuser/.ssh/id_rsa*: No such file or directory
  • 있으면 다음으로, 없으면 생성
testuser@zetaclient1:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/testuser/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/testuser/.ssh/id_rsa.
Your public key has been saved in /home/testuser/.ssh/id_rsa.pub.
The key fingerprint is:
ed:6b:78:50:2d:69:0b:0d:19:f4:bf:97:ae:a3:bb:30 testuser@zetaclient1
The key's randomart image is:
+--[ RSA 2048]----+
|       .oo       |
|        o.       |
|         o.o     |
|        ..*..    |
|        S+.o.    |
|        ...  . . |
|        Eo. . o  |
|        .oo..o   |
|         o=+.o.  |
+-----------------+
  • git 서버에 키 등록(복제)
testuser@zetaclient1:~$ sshpass -p "ServerP@ssw0rd" ssh-copy-id git@zetagit
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'git@zetagit'"
and check to make sure that only the key(s) you wanted were added.

7 (Optional) 쉘 변경[ | ]

  • 서버 측에서 git 계정의 쉘을 git-shell로 변경한다.
  • git 사용자가 git 서버 리눅스 쉘에 접근하는 것을 막는 작업이다.
root@zetagit:~# which git-shell
/usr/bin/git-shell
root@zetagit:~# chsh git -s /usr/bin/git-shell
root@zetagit:~# cat /etc/passwd | grep git
git:x:1000:1000::/home/git:/usr/bin/git-shell
→ 이것을 적용하면 bash 쉘을 사용할 수 없게 되며, 클라이언트 키를 추가로 등록하는 것도 수행할 수 없게 된다.
→ 클라이언트 키를 추가로 등록하려면 chsh git -s /bin/bash를 임시로 적용했다가 등록 후 다시 이것을 적용하도록 하자.
→ (Optional) 추가로 클라이언트를 등록할 일이 없다면 리눅스 관리자도 알 수 없는 랜덤 패스워드 지정을 적용하여 더욱 보안을 강화하는 방법도 있다.

8 (클라이언트) 테스트[ | ]

testuser@zetaclient1:~$ git clone git@zetagit:/opt/git/project.git
Cloning into 'project'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
testuser@zetaclient1:~$ cd project/
testuser@zetaclient1:~/project$ ll
total 12
drwxr-xr-x  3 testuser testuser 4096 Aug  7 16:57 ./
drwx------ 27 testuser testuser 4096 Aug  7 16:57 ../
drwxr-xr-x  7 testuser testuser 4096 Aug  7 16:57 .git/

9 (참고) 추가 저장소 생성/삭제[ | ]

  • 생성
PROJECT=새프로젝트명
mkdir -p /opt/git/$PROJECT.git
git init --bare /opt/git/$PROJECT.git
  • 삭제
rm -rf /opt/git/프로젝트명.git

10 같이 보기[ | ]

11 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}