2025-12-18T120057


Preliminary Setups

Step 1: Enable SSH Service from DSM

Enable SSH service from Your Synology DSM’s Control Panel (* for security consideration, I would strongly suggest you to change the port away from the default 22, here for showcase purpose I’m using 2222)

2025-12-18T103228

Step 2: Install and Enable Git Server

Install and run the “Git Server” package from your Synology DSM’s Package Center:

2025-12-18T104454

(Optional) Step 3: Create a New Shared Folder

I’d like to clean my directories clean, my rule is that directory segregate the file of different purpose, hence I’ll create a new “Shared Directory” under DSM to store Git related repositories. (*this step may be optional for you)

2025-12-18T105127


SSH Key Setup

Using Locally Stored SSH Key

You can following this post by golimb, below are the TLDR if you are using a MacOS device:

  1. Generate ssh-key pairs:

    • run ssh-keygen -t rsa -b 4096 -C 'user@domain.com'
    • it should generate two files: the one that ends with .pub is your public key (of which we will use in the next step), and the other one is your private key
  2. Copy the public key:

    • open the *.pub file and copy its content
    • it will have the pattern similar to: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAA...
  3. SSH into your Synology NAS using user/password:

    • open your terminal and run ssh {admin-user}@{ip-to-my-synology} -p 2222
    • navigate to your user’s root file, i.e. cd /volume1/home/hello.world
    • open .ssh folder (create one if it doesn’t exist), i.e. mkdir .ssh && cd .ssh
    • open authorized_keys file (create one if it does exists), i.e. vi authorized_keys
    • pate the public key copied earlier ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAA... to the authorized_keys file and save it
  4. Change file permissions

    • Change user folder permission: sudo chmod 755 /volume1/homes/{admin-user}
    • Change .ssh folder permission: sudo chmod 755 /volume1/homes/{admin-user}/,ssh
    • Change .ssh/authorized_key file permission: sudo chmod 644 /volume1/homes/{admin-user}/.ssh/authorized_keys
  5. Test Connection

    • Run the following command from terminal: ssh {admin-user}@{nas-ip-or-host} -p {specifiedCustomPort} -o "IdentitiesOnly=yes" -i {privateKey}

    • If you want a more convenient way of logging in without specifying the bunch of flags, consider using ssh config file (link), for instance:

      1
      2
      3
      4
      5
      6
      
      Host my-nas
        HostName nas.example.com 
        User admin-user
        Port 2222
        IdentityFile ~/.ssh/my_nas_ed25519
        IdentitiesOnly yes
      

      Then you can login via ssh my-nas

(Optional) Using 1Password SSH Key Agent

If you are using 1Password SSH key agent like me, you can use the public key copied from your 1Password, and use it instead of the ssh-keygen generated ssh key:

2025-12-18T112041

And then you can add the following in your host computer’s ~/.ssh/config file (e.g. /Users/user_name/.ssh/config on MacOS):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Host <ALIAS-FOR-THIS-HOST>
  HostName <IP-ADDRESS-TO-YOUR-NAS>
  Port <PORT-NUMBER-FOR-SSH>
  User <SYNOLOGY-USER-WITH-REQUIRED-PRIVILLEDGE>
  IdentityAgent "~/Library/Group Containers/LOREMIPSUM.com.1password/t/agent.sock"
  
# EXAMPLE 
Host my-hello-world-nas
  HostName 100.100.100.100
  Port 2222
  User hello.world
  IdentityAgent "~/Library/Group Containers/LOREMIPSUM.com.1password/t/agent.sock"

Once setup, when you need to use any relevant private key setup and stored in your 1Password, you will be prompted to use your TouchID / Password like the following:

2025-12-18T110104

(Find out more at: https://developer.1password.com/docs/ssh/agent/)


Using Git Service in NAS

In order to use git service in Synology NAS, you will have to go through the following steps:

  1. SSH onto NAS: ssh nas-ip-or-host

  2. Create New Directory (as Root for Git Repository): cd /volume1/git && mkdir -p directory-as-git-repo (the -p flag will only create the directory if it doesn’t exist)

  3. Setup/Initialise Repository on Server (that Acts as Hub): git init --bare (the --bare flag creates a repository that does not have a working directory)

  4. Abort from SSH: via Ctrl + D shortcut, or exit command

  5. Cloning Repository from Local Machine : via git clone ssh://nas-ip-or-host:/volume1/git/temp-repo-for-test-deletable

I’ve produced a code snippet that does it for me:

1
2
3
4
5
REPOSITORY_NAME="temp-repo-for-test-deletable"
NAS_HOST_NAME="my-nas-host-name-or-ip-address"
SHARE_FOLDER_FOR_GIT="/volume1/git"
ssh $NAS_HOST_NAME "cd / && cd $SHARE_FOLDER_FOR_GIT && mkdir -p $REPOSITORY_NAME && cd $REPOSITORY_NAME && git init --bare --quiet"
git clone ssh://$NAS_HOST_NAME:$SHARE_FOLDER_FOR_GIT/$REPOSITORY_NAME

2025-12-18T120057


Use Git Repository in NAS as an Backup (to the Actual Environment)

Below are some extra code snippets I experimented for this:

  • Pull existing repository:

    1
    2
    
         git clone ssh://nas-ip-or-domain:/directory-to-the-git-repo
    e.g. git clone ssh://my-nas:v/olume1/git/my-python-project
    
  • Add existing repository as remote:

    1
    2
    
         git remote add remote-name ssh://nas-ip-or-domain:/directory-to-the-git-repo
    e.g. git remote add synology ssh://my-nas:/volume1/git/my-python-project
    
  • Create a new branch from current branch (and push to the added remote)

    1
    2
    
         git checkout -b your-branch-name
    e.g. git checkout -b main-synology
    
    1
    2
    
         git push remote-name your-branch-name
    e.g. git push synology main-synology
    
  • Add push the current bran to both existing remote, and synology (add extra remove push destination)

    1
    2
    
         git remote set-url --add --push remote-name ssh://nas-ip-or-domain:/directory-to-the-git-repo && git push
    e.g. git remote set-url --add --push origin ssh://my-nas:/volume1/git/my-python-project && git push
    

    ⬆️ this command will add an extra pushurl = ... to your .git/config file and use that as the ONLY default push destination, so I would recommend adding the existing / default url first using git remote get-url origin | xargs git remote set-url --add --push origin

If you’re like me, all you need is to keep a backup of an existing GitHub repository in your NAS (i.e. one-sided sync), you can use the following command:

  1. create the git repository in your NAS with the same name (using the snippet we used earlier)
  2. add the existing (manually add the --push url to prevent it being overridden by the NAS’s URL next step)
  3. add your NAS’s git repository to your origin remote (using git remote set-url --add --push command)
  4. run push the origin remote (now it will push both to GitHub/GitLab and your NAS)
1
2
3
4
5
6
7
REPOSITORY_NAME="example-repo-name"
NAS_HOST_NAME="my-synology"
SHARE_FOLDER_FOR_GIT="/volume1/git"
ssh $NAS_HOST_NAME "cd / && cd $SHARE_FOLDER_FOR_GIT && mkdir -p $REPOSITORY_NAME && cd $REPOSITORY_NAME && git init --bare --quiet"
git remote get-url origin | xargs git remote set-url --add --push origin
git remote set-url --add --push origin ssh://$NAS_HOST_NAME:$SHARE_FOLDER_FOR_GIT/$REPOSITORY_NAME
git push origin

2025-12-24T114740


Reference

  • Golimb - Synology SSH Key Authentication: link
  • Digital Aloha - How To Setup And Use Git Server On A Synology NAS: link
  • 1Password Developer - 1Password SSH agent: link
  • Synology Knowledge Center - Git Server: link
  • Тsфdiиg - Microsoft doesn’t want you to know this (Use SSH Reachable Server as Git Server): link