본문 바로가기

Web Service/AWS

EC2로 git 서버 구축하기

1. AWS에서 EC2 instance를 만든다. (ubuntu로 진행)

2. sudo su

3. apt-get install git

4. 다음 명령어로 git 전용 사용자 계정을 만든다.

# useradd -m -d /home/git -u 1005 git

5. git 계정으로 shell에 접속하는 것을 막기 위해

# usermod -s /usr/bin/git-shell git

명령어로 git의 /bin/bash를 /usr/bin/git-shell로 바꾼다.

6. /etc/shells 파일의 마지막 줄에 '/usr/bin/git-shell' 을 추가한다.

7. 다음 명령어로 COMMAND_DIR 환경변수에 해당하는 디렉토리가 존재하도록 한다.

# cd /home/git

# mkdir git-shell-commands

# chmod 755 git-shell-commands

8. 다음 과정을 통해 local에서 ssh로 EC2의 git 계정으로 접속할 수 있도록 변경한다.(여기서 john은 git)

간단히 설명하자면 git라는 사용자의 private key를 ec2-user(나의 경우는 ubuntu) 계정을 통해 생성하여 local로 받아오는 것이다. 받아온 key는 ~/.ssh/config에 등록하여 ssh -i 옵션 없이 사용할 수 있다.

=======================================================================

Step 0. Login by default user, “ec2-user”:

1
static-9:ec2_thelostlogbook utkarsh$ ssh -i my_key.pem ec2-user@111.111.11.111

Step 1. Create a new user, we will call our new user “john”:

1
[ec2-user@ip-11-111-111-111 ~]$ sudo adduser john

Set password for “john” by:

1
2
[ec2-user@ip-11-111-111-111 ~]$ sudo su
[root@ip-11-111-111-111 ec2-user]$ passwd john

Add “john” to sudoer’s list by:

1
[root@ip-11-111-111-111 ec2-user]$ visudo

and add this to the last line:

1
john   ALL = (ALL)    ALL

Alright! We have our new user created, now you need to generate the key file which will be needed to login, like we have my_key.pem in Step 0.

Now, exit and go back to ec2-user, out of root.

Step 2. Creating the public and private keys:

1
[ec2-user@ip-11-111-111-111 ~]$ su john

Enter the password you created for “john” in Step 1.

1
2
3
4
5
6
7
[john@ip-11-111-111-111 ec2-user]$ cd /home/john/
[john@ip-11-111-111-111 ~]$ ssh-keygen -b 1024 -f john -t dsa
[john@ip-11-111-111-111 ~]$ mkdir .ssh
[john@ip-11-111-111-111 ~]$ chmod 700 .ssh
[john@ip-11-111-111-111 ~]$ cat john.pub > .ssh/authorized_keys
[john@ip-11-111-111-111 ~]$ chmod 600 .ssh/authorized_keys
[john@ip-11-111-111-111 ~]$ sudo chown john:ec2-user .ssh

In the above step, john is the user we created and ec2-user is the default user group.

1
[john@ip-11-111-111-111 ~]$ sudo chown john:ec2-user .ssh/authorized_keys

Step 3. Now you just need to download the key called “john”

I use scp to download/upload files from EC2, here is how you can do it:

You will still need to copy the file using ec2-user, since you only have the key for that user name. So, you will need to move the key to ec2-user folder and chmod it to 777.

1
2
[john@ip-11-111-111-111 ~]$ sudo cp john /home/ec2-user/
[john@ip-11-111-111-111 ~]$ sudo chmod 777 /home/ec2-user/john

Now come to local machine’s terminal, where you have my_key.pem file and do this:

1
static-9:ec2_thelostlogbook utkarsh$ scp -i my_key.pem ec2-user@111.111.11.111:/home/ec2-user/john john

The above command will copy the key “john” to the present working directory on your local machine. Once you have copied the key to your local machine, you should delete “/home/ec2-user/john”, since it’s a private key.

Now, one your local machine chmod john to 600.

1
static-9:ec2_thelostlogbook utkarsh$ chmod 600 john

Step 4. Time to test your key:

1
static-9:ec2_thelostlogbook utkarsh$ ssh -i john john@111.111.11.111

So, in this manner, you can setup multiple users to use one EC2 instance!!

=======================================================================

9. Repository를 만든다.
# su -s /bin/bash - git
$ cd~
$ mkdir example.git
$ cd example.git
$ git --bare init
10. 첫 commit
$ mkdir example
$ cd example
$ git init
$ touch README
$ git commit -m 'first commit'
$ git remote add origin ssh://git@example.org/~/example.git
$ git push origin master

참조 사이트:
http://www.fclose.com/b/linux/366/set-up-git-server-through-ssh-connection/
http://utkarshsengar.com/2011/01/manage-multiple-accounts-on-1-amazon-ec2-instance/

http://stackoverflow.com/questions/5803768/in-xcode-4-how-do-i-add-a-remote-github-repository-to-an-existing-local-project

(Xcode에서 remote repository 추가하기)