Môi trường cài đặt: * Centos 6.8 ## 1. Tạo user cho git server Giả sử sẽ tạo một user với username là "git". ~~~ Bash $ sudo useradd git $ sudo su git $ cd ~ $ mkdir .ssh && chmod 700 .ssh ~~~ ## 2. Tạo khóa đăng nhập cho user Thêm khóa công khai cho user "git". ~~~ Bash $ cd ~/.ssh $ touch authorized_keys && chmod 600 authorized_keys # Copy & paste khóa công khai vào file authorized_keys. (Có thể thêm nhiều khóa) ~~~ ## 3. Tạo repo git trên server Giả sử sẽ tạo 1 repo sample.git tại thư mục `/opt/git/sample.git`. Chú ý: user "git" phải có quyền RWX+ đối với thư mục `sample.git`. ~~~ Bash $ cd /opt $ mkdir git $ mkdir git/sample.git $ cd git/sample.git $ git init --bare ~~~ ## 4. Clone repo git vừa tạo về máy local Sử dụng khóa private để kết nối với server và clone git về máy như bình thường. **Ghi chú cho trường hợp sử dụng git-bash để clone:** Cần tạo file ssh config chứa thông tin kết nối với server trước khi thực hiện clone git về máy local. Đối với Window, tạo file config ở thư mục `C:\User\user_name\.ssh\config` với nội dung sau: ~~~ # config Host gitserv Hostname remote.server.com IdentityFile path/to/private/key.pem IdentitiesOnly yes ~~~ Chú ý: `IdentityFile` sử dụng private key có đuôi '.pem', không phải đuôi '.ppk'. Sau khi thêm file config với nội dung như trên, có thể add repo git từ host `gitserv` về máy local như sau: ~~~ Bash $ git remote add origin git@gitserv:/opt/git/sample.git ~~~ ## 5. Giới hạn quyền của user "git" Với thiết lập như trên, user "git" có thể sử dụng shell để đăng nhập vào server. Trong trường hợp muốn giới hạn user "git" chỉ sử dụng cho mục đích push/pull source code từ repo git trên server, cần thay đổi login shell của user "git" sang `git-shell`. Login shell của mỗi user được quản lý trong file `/etc/passwd` dưới dạng như sau: ~~~ Bash git:x:1000:1000::/home/git:/bin/sh # /home/git -> thư mục home của user git # /bin/sh -> login shell của user git ~~~ **Cách làm:** Tìm đường dẫn của `git-shell`: ~~~ Bash $ which git-shell /usr/bin/git-shell ~~~ Thay login shell của user git bằng đường dẫn vừa tìm được: ~~~ Bash $ sudo chsh -s /usr/bin/git-shell git ~~~