I connected before to vps using below gitlab ci/cd configuration. But in recent server I can't.
my .yml file is like below:
deploy:
environment:
name: production
url: http://erfantahvieh.com/
image: php:7.1.1
stage: deploy
only:
- master
before_script:
- apt-get update -yqq
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
- mkdir -p ~/.ssh
- echo -e "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
- chmod 700 ~/.ssh
- chmod 700 ~/.ssh/id_rsa
- eval "$(ssh-agent -s)"
- ssh-add ~/.ssh/id_rsa
- ssh-keyscan -H $HOST >> ~/.ssh/known_hosts
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
script:
- ssh $USER@$HOST "ls -la"
Know when I run my pipe line the output is like below:
$ mkdir -p ~/.ssh
$ echo -e "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
$ chmod 700 ~/.ssh
$ chmod 700 ~/.ssh/id_rsa
$ eval "$(ssh-agent -s)"
Agent pid 201
$ ssh-add ~/.ssh/id_rsa
Identity added: /root/.ssh/id_rsa (rsa w/o comment)
$ ssh-keyscan -H $HOST >> ~/.ssh/known_hosts
# xxx.xxx.xxx.xxx SSH-2.0-OpenSSH_7.4
# xxx.xxx.xxx.xxx SSH-2.0-OpenSSH_7.4
# xxx.xxx.xxx.xxx SSH-2.0-OpenSSH_7.4
$ [[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
$ ssh $USER@$HOST "ls -la"
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
ERROR: Job failed: exit code 1
What is wrong? Why I connected before by exactly same code but now I can't? Is there any thing that I missed?
Note: I create ssh private key in my vps and put it in secret var in gitlab ci/cd settings like $HOST and $USER.
The problem happen because you should have authorized_keys
file in your .ssh
folder to let other machines to login you server using ssh private key
.
You can create this file if there isn't by below commands.
cd ~/.ss
cp id_rsa.pub authorized_keys
systmctl restart sshd