Search code examples
sshgitlabgitlab-ci-runner

gitlab runner ssh private key 644 file permission error


When running a gitlab ci/cd pipeline, ssh gives 0644 bad permission error. Variable is stored as a file type in the settings>variable section in gitlab.
.gitlab-ci.yml file looks like:

stages:
  - deploy
  
before_script:
  - apt-get update -qq
  - apt-get install -qq git
  - mkdir -p ~/.ssh
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
deploy_1:
  stage: deploy
  only:
    - master
  tags:
    - master
  script:
    - ssh -i $SSH_KEY user@ip "mkdir -p runner_test"

deploy_2:
  stage: deploy
  only:
    - master
  tags:
    - master
  script:
    - ssh -i $SSH_KEY user@ip "mkdir -p runner_test"

Error:

$ ssh -i $SSH_KEY host@ip "mkdir -p runner_test"
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/home/user/builds/gPnQDT8L/0/username/server.tmp/SSH_KEY' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/home/user/builds/gPnQDT8L/0/username/server.tmp/SSH_KEY": bad permissions
user@ip: Permission denied (publickey).
Cleaning up file based variables

How do I change the private key permissions from 644 to 600 or 400.


Solution

  • You can see the same error in this thread.

    The fixed version of that file:

    server:
      stage: deploy
      script:
      - apt-get install -y openssh-client rsync
      - chmod 400 $SSH_KEY
      - scp -o StrictHostKeyChecking=no -P $SSH_PORT -i $SSH_KEY public/server.zip $SSH_URI:modpack/server.zip
    

    A simple chmod 400 $SSH_KEY should be enough.