{"id":3576,"date":"2018-05-29T21:32:28","date_gmt":"2018-05-30T04:32:28","guid":{"rendered":"http:\/\/mattfife.com\/?p=3576"},"modified":"2022-03-24T09:45:41","modified_gmt":"2022-03-24T16:45:41","slug":"setting-up-your-own-git-server-wi","status":"publish","type":"post","link":"https:\/\/mattfife.com\/?p=3576","title":{"rendered":"Setting up your own git server with individual user accounts"},"content":{"rendered":"<p>This is sort of covered in other spots, but not as clearly and from scratch. Here&#8217;s a complete guide that shows how to set up your own git server and git clients. I found this setup handy when trying out some more complex git merging commands and experimenting with remotes while learning git.<\/p>\n<p>I tested this on Ubuntu 16.04 by creating an Ubuntu virtual machine and then cloning it. 1 VM for the server, and 2 for clients. I used bridged networking so each would get their own IP address, but that&#8217;s not required as long as the VM&#8217;s can communicate with each other over TCP\/IP.<\/p>\n<p>There are two ways to set the git accounts up. The first way shown has all client users accessing the repository through the same server user account. While each user&#8217;s submissions will be labeled correctly in the git log, having everyone use the same system account isn&#8217;t safe computing practices for groups. Instead, if you follow the instructions in the optional part you can use individual user accounts and keep things more safe.<\/p>\n<h3>Client\/Server Setup<\/h3>\n<p><span style=\"text-decoration: underline;\">First, on the server:<\/span><\/p>\n<ol>\n<li>Make sure ssh is installed on the server:\n<pre>server$ sudo apt-get install openssh-server<\/pre>\n<\/li>\n<li>Make sure sshd is running\/listed when you do a ps. If not, reboot or restart it.\n<pre>server$ ps -A | grep sshd<\/pre>\n<\/li>\n<li>Make sure git is installed:\n<pre>server$ sudo apt-get install git-core<\/pre>\n<\/li>\n<li>Add a user to the server that will hold the git repositories\n<pre>server$ sudo adduser git\nserver$ sudo passwd git\nserver$ su - git\nserver$ mkdir -p .ssh<\/pre>\n<\/li>\n<\/ol>\n<p><span style=\"text-decoration: underline;\">Next, on your client:<\/span><\/p>\n<ol>\n<li>Make sure git is installed\n<pre>client$ sudo apt-get install git-core<\/pre>\n<\/li>\n<li>Create an ssh key. While not strictly required, it&#8217;s a good idea to add a passcode to the key when prompted during key creation.\n<pre>client$ ssh-keygen -t rsa<\/pre>\n<p>This should create a file called id_rsa.pub in your ~\/.ssh directory. See documentation on ssh-keygen for full details.<\/li>\n<li>Copy the ssh key to the server&#8217;s git directory:\n<pre>client$ scp ~\/.ssh\/id_rsa.pub git@server.com:\/home\/git\/client_id_rsa.pub<\/pre>\n<\/li>\n<\/ol>\n<p><span style=\"text-decoration: underline;\">Back on server:<\/span><\/p>\n<ol>\n<li>Add the client user&#8217;s key to the ssh list in the \/home\/git\/.ssh directory\n<pre>server$ mkdir ~\/.ssh<\/pre>\n<\/li>\n<li>Append the client user key to the list of authorized keys\n<pre>server$ cat ~\/client_id_rsa.pub &gt;&gt; ~\/.ssh\/authorized_keys<\/pre>\n<\/li>\n<li>Create a new group called &#8216;gituser&#8217; we&#8217;ll use for users to access our repository in \/home\/git\/\n<pre>sudo groupadd gituser\nsudo usermod -a -G gituser git<\/pre>\n<\/li>\n<li>Log out completely and back in. You MUST do this for group assignment to take effect orsubsequent chgrp\/chmod commands won&#8217;t work.<\/li>\n<li>Make the git repository and tell it to share based on the group the user belongs to.\n<pre>server$ cd ~git\nserver$ mkdir -p mydepot\nserver$ cd mydepot\nserver$ git init --bare --shared=group\nInitialized empty Git repository in \/home\/git\/mydepot\/<\/pre>\n<\/li>\n<li>Set the permissions on the repository directory so that anyone in the new &#8216;gituser&#8217; group can access it.\n<pre>chgrp -R gituser \/home\/git\/mydepot\nchmod -R g+rw \/home\/git\/mydepot\nchmod g+s `find \/home\/git\/mydepot -type d`<\/pre>\n<\/li>\n<\/ol>\n<p><span style=\"text-decoration: underline;\">Back on client (if it is a clean client without files for the repo):<\/span><\/p>\n<ol>\n<li>Test your ssh connection by trying to ssh into the server (using the git user)<\/li>\n<li>Create the local project:\n<pre>client$ mkdir -p depot\/project1\nclient$ cd depot\/project1\nclient$ git config --global user.email \"you@client.com\"\nclient$ git config --global user.name \"clientUsername\"<\/pre>\n<\/li>\n<li style=\"text-align: left;\">Clone the remote to your local system\n<pre>client$ git clone ssh:\/\/git@serverurl_or_ip:\/home\/git\/mydepot\/ .<\/pre>\n<\/li>\n<\/ol>\n<p>Enter your username password and you&#8217;re done. The clone and the remote should be connected. Push\/Fetch as normal. See the optional part below if you don&#8217;t want to use a global git user account on the server.<\/p>\n<p><span style=\"text-decoration: underline;\">Or &#8211; Back on client that HAS existing files you want to get to the server:<\/span><\/p>\n<p>Lets say you have a client that already has a bunch of files or even a git repository and you want to start using a remote repository. Here&#8217;s how you can add those local files into the remote server repository you just created.<\/p>\n<ol>\n<li>Initialize the repository where your client files are\n<pre>client$ git init\n  Initialized empty Git repository in &lt;blah&gt;\nclient$ git add .\nclient$ git commit\n  &lt;Write something about this being a commit from the client&gt;<\/pre>\n<\/li>\n<li>If you are going to using the git user account for all users, connect the project to your server this way:\n<pre>client$ git remote add origin ssh:\/\/git@serverurl_or_ip:\/home\/git\/mydepot\/<\/pre>\n<p>If you don&#8217;t want to use the git account, then you must first create a user account on the server that matches the client userid (making sure to set the group\/user properties on the server account), then use this:<\/p>\n<pre>client$ git remote add origin ssh:\/\/serverurl_or_ip:\/home\/git\/mydepot\/<\/pre>\n<p>Enter the password for your username or the &#8216;git&#8217; server user depending on which one you used.<\/li>\n<li>Set up git configuration to avoid warnings and push:\n<pre>client$ git config --global push.default simple\nclient$ git push --set-upstream origin master<\/pre>\n<p>You will be prompted for the passkey you used when you created your RSA key in the above push step. Enter that passkey (not your git\/user account password).<\/li>\n<\/ol>\n<h3>Optional &#8211; Using user accounts instead of a global &#8216;git&#8217; account on the server.<\/h3>\n<p>The previous instructions had everyone use the same &#8216;git&#8217; server user account when checking in &#8211; which means everyone must have the &#8216;git&#8217; server account password. The log will show the right names, but security-wise this isn&#8217;t always best to use one global account on servers.<\/p>\n<p>If you have more than one user but want everyone to log in separately, simply create a user account on the server like this:<\/p>\n<p>On client for each client user:<\/p>\n<ol>\n<li>Create a ssh key on your client as before.<\/li>\n<li>Copy that key .pub to the server and append it to the authorized_keys file as above.\n<pre>client$ scp .ssh\/myclient_id_rsa.pub git@serverurl_or_ip:\/home\/git<\/pre>\n<\/li>\n<\/ol>\n<p>On server:<\/p>\n<ol>\n<li>Append the client&#8217;s public key to the authorized keys\n<pre>server$ cat ~\/myclient_id_rsa.pub &gt;&gt; ~\/.ssh\/authorized_keys<\/pre>\n<\/li>\n<li>Create a user account that matches the userid on the client\n<pre>server$ sudo useradd client_username\nserver$ sudo passwd client_username<\/pre>\n<\/li>\n<li>Make sure the new user account has access to the \/home\/git\/ project directories by setting their group membership:\n<pre>server$ sudo usermod -a -G gituser client_username<\/pre>\n<\/li>\n<\/ol>\n<p>From now on, you don&#8217;t need to specify the git user account. Do not put the git@ part into the git clone url and use the username&#8217;s password when asked to log in:<\/p>\n<pre>client$ git clone ssh:\/\/serverurl_or_ip:\/home\/git\/mydepot .\n<\/pre>\n<p>This method works great, but does require that you keep the client and server userid account passwords synced.<\/p>\n<h3>Setting up a Windows client:<\/h3>\n<p>Once the server is set up, you&#8217;re almost there. Microsoft has written a good guide. You&#8217;ll need OpenSSH or Windows 10 installed the generate an ssh key (if you don&#8217;t have one already).<br \/>\n<a href=\"https:\/\/docs.microsoft.com\/en-us\/vsts\/git\/use-ssh-keys-to-authenticate?view=vsts\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.microsoft.com\/en-us\/vsts\/git\/use-ssh-keys-to-authenticate?view=vsts<\/a><\/p>\n<h3>Resource links:<\/h3>\n<ul>\n<li><a href=\"https:\/\/dev.to\/zduey\/how-to-set-up-an-ssh-server-on-a-home-computer\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/zduey\/how-to-set-up-an-ssh-server-on-a-home-computer<\/a><\/li>\n<li><a href=\"https:\/\/git-scm.com\/book\/en\/v2\/Git-on-the-Server-Setting-Up-the-Server\" target=\"_blank\" rel=\"noopener\">https:\/\/git-scm.com\/book\/en\/v2\/Git-on-the-Server-Setting-Up-the-Server<\/a><\/li>\n<li><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-a-private-git-server-on-a-vps\" target=\"_blank\" rel=\"noopener\">https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-a-private-git-server-on-a-vps<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/6448242\/git-push-error-insufficient-permission-for-adding-an-object-to-repository-datab\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/6448242\/git-push-error-insufficient-permission-for-adding-an-object-to-repository-datab<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This is sort of covered in other spots, but not as clearly and from scratch. Here&#8217;s a complete guide that shows how to set up your own git server and git clients. I found this setup handy when trying out some more complex git merging commands and experimenting with remotes while learning git. I tested this on Ubuntu 16.04 by creating an Ubuntu virtual machine and then cloning it. 1 VM for the server, and 2 for clients. I used&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/mattfife.com\/?p=3576\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[6,5],"tags":[],"class_list":["post-3576","post","type-post","status-publish","format-standard","hentry","category-technicalproblemsolutions","category-technical"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4WECr-VG","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts\/3576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3576"}],"version-history":[{"count":23,"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts\/3576\/revisions"}],"predecessor-version":[{"id":6373,"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts\/3576\/revisions\/6373"}],"wp:attachment":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}