How to set up Git on Linux

Git is a version control system for software development

1. git 설치 및 계정 생성 (client->server)

ssh root@11.22.33.44 #ssh로 원격 서버(여기서는 IP주소가 11.22.33.44라고 가정)에 접속
root@11.22.33.44's password:  [server의 root 암호 입력]

yum install git #git 설치

adduser git #git 계정 생성 (git이 아닌 다른 이름도 상관없음)
passwd git #git 암호 변경
Changing password for user git.
New password:  [dexter 입력] (dexter가 아닌 다른 암호도 상관없음)
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:  [dexter 입력] (앞의 암호 다시 입력)

su git #생성한 계정(git)으로 전환
cd #홈 디렉토리(/home/git)로 이동
ssh-keygen -t rsa #rsa 암호화 방식으로 key를 생성

Generating public/private rsa key pair.
Enter file in which to save the key (/home/git/.ssh/id_rsa):  [그냥 Enter 입력]
Created directory '/home/git/.ssh'
Enter passphrase (empty for no passphrase): [그냥 Enter 입력]
Enter same passphrase again: [그냥 Enter 입력]

ls -al ~/.ssh #/home/git/.ssh에 id_rsa, id_rsa.pub 두 개 파일들이 생성되었는지 확인

exit #git 계정에서 탈출
exit #root 계정에서 탈출 (원격 서버 접속 해제)

2. git 설치 및 기본 설정 (client)

su #root 계정으로 변경
yum install git #git 설치
exit #root 계정에서 탈출

git --version #git 버전 확인
git config --global user.name "wano" #git에서 사용할 계정 설정 (여기서는 wano)
git config --global user.email "wano@gmail.com" #이메일 설정 (여기서는wano@gmail.com)
git config --global color.ui "auto" #색상 설정
git config --global --list #설정 확인

3. SSH Key 생성 (client)

ssh-keygen -t rsa #rsa 암호화 방식으로 key를 생성

Generating public/private rsa key pair.
Enter file in which to save the key (/home/wano/.ssh/id_rsa):  [그냥 Enter 입력]
Created directory '/home/wano/.ssh'
Enter passphrase (empty for no passphrase): [그냥 Enter 입력]
Enter same passphrase again: [그냥 Enter 입력
]

ls -al ~/.ssh #/home/git/.ssh에 id_rsa, id_rsa.pub 두 개 파일들이 생성되었는지 확인
#id_rsa: private key, id_rsa.pub: public key

4. Public Key를 서버에 전송 (client)

cd .ssh #/home/wano/.ssh 디렉토리로 이동
scp id_rsa.pub git@11.22.33.44: #서버의 git 계정 home에 공개키를 복사
git@11.22.33.44's password:  [server의 git 암호 입력: dexter]

5. 전송받은 Public Key를 등록 (client->server)

ssh git@11.22.33.44 #ssh로 원격 서버에 접속
git@11.22.33.44's password:  [server의 git 암호 입력: dexter]

cd #홈 디렉토리(/home/git)로 이동
ls -al:  #전송된 id_rsa.pub 파일이 있는지 확인
cat id_rsa.pub >> .ssh/authorized_keys #authorized_keys 파일을 생성하고 내용을 추가
rm -f id_rsa.pub #id_rsa.pub 삭제
chmod 700 .ssh  #.ssh 디렉토리 권한 설정 (매우 중요)
chmod 600 .ssh/authorized_keys #authorized_keys 파일 권한 설정 (매우 중요)

exit #root 계정에서 탈출 (원격 서버 접속 해제)

6. 접속 테스트 (client->server)

ssh git@11.22.33.44 #ssh로 원격 서버에 다시 접속 (암호를 안물어보면 성공!)
#만약 암호를 물어보면 설정에 문제가 있는 것이므로 -v, -vv, -vvv 옵션 등을 추가하여 디버깅해볼것. 또한, ssh-add를 실행한 후 시도해볼것.
exit #root 계정에서 탈출 (원격 서버 접속 해제)

7. Git Repository 생성 (client->server)

ssh root@11.22.33.44 #ssh로 원격 서버에 접속 (root 계정 사용)
root@11.22.33.44's password:  [server의 root 암호 입력]

mkdir -p /gitRepository/Zelos.git #/gitRepository/Project1.git 디렉토리 생성 (다른 이름도 상관없음)
chown git:git /gitRepository #소유권 변경
chown -R git /gitRepository/Zelos.git #소유권 변경

su git # git으로 계정 변경
cd /gitRepository/Zelos.git #/gitRepository/Project1.git 디렉토리로 이동
git --bare init #Zelos 프로젝트 git repository 초기화
Initialized empty Git repository in /gitRepository/Zelos.git
cd #홈 디렉토리(/home/git)으로 이동

ln -s /gitRepository/Zelos.git . #/home/git/Zelos.git 심볼릭 링크 생성
exit #git 계정에서 탈출
exit #root 계정에서 탈출 (원격 서버 접속 해제)

8. 최초의 clone & commit (client)

cd ~/work #~/work로 이동
git clone git@11.22.33.44:Zelos.git #~/work/Zelos 생성 (clone)
Cloning into 'Zelos' ...
warning: You appear to have cloned an empty repository.

cd Zelos #/home/wano/work/Zelos로 이동
echo "Hello Zelos" > readme.txt #readme.txt 생성
git add readme.txt #add
git commit -m 'first commit' #commit
git push origin master #push
git status #status 확인
git log #log 확인
git pull #최신 내용으로 업데이트

9. git 동기화 (client)

작업 완료 후, 이동
git pull
git add --all
git commit -m '.' 
git push

10.보안을 위해서 쉘기능 제한 (optional) (client->server)

ssh root@11.22.33.44 #ssh로 원격 서버에 접속 (root 계정 사용)
root@11.22.33.44's password:  [server의 root 암호 입력]

/etc/passwd의 내용 중,
git:x:1001:1001::/home/git:
/bin/bash의 내용을 다음과 같이 변경해준다.
git:x:1001:1001::/home/git:
/usr/bin/git-shell

exit #root 계정에서 탈출 (원격 서버 접속 해제)

ssh git@11.22.33.44 #ssh로 원격 서버에 접속 (git 계정 사용) 같이 변경해준다.
이제 ssh로 접속을 시도하면 Connection to 11.22.33.44 closed. 에러를 출력하면서 거부된다.