无法在 gitlab 中使用 ssh 对远程 vps 进行身份验证
Cant authenticate to remote vps using ssh in gitlab
我之前使用下面的 gitlab ci/cd 配置连接到 vps。但是在最近的服务器我不能。
我的 .yml 文件如下所示:
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"
知道当我 运行 我的管道输出如下所示:
$ 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
怎么了?
为什么我以前使用完全相同的代码连接但现在不能连接?
我有什么遗漏的吗?
注意:我在我的 vps 中创建了 ssh 私钥并将其放入 secret var 中
gitlab ci/cd 设置如 $HOST 和 $USER.
问题的发生是因为你的 .ssh
文件夹中应该有 authorized_keys
文件,让其他机器使用 ssh private key
.
登录你的服务器
如果没有此文件,您可以通过以下命令创建。
cd ~/.ss
cp id_rsa.pub authorized_keys
systmctl restart sshd
我之前使用下面的 gitlab ci/cd 配置连接到 vps。但是在最近的服务器我不能。
我的 .yml 文件如下所示:
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"
知道当我 运行 我的管道输出如下所示:
$ 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
怎么了? 为什么我以前使用完全相同的代码连接但现在不能连接? 我有什么遗漏的吗?
注意:我在我的 vps 中创建了 ssh 私钥并将其放入 secret var 中 gitlab ci/cd 设置如 $HOST 和 $USER.
问题的发生是因为你的 .ssh
文件夹中应该有 authorized_keys
文件,让其他机器使用 ssh private key
.
如果没有此文件,您可以通过以下命令创建。
cd ~/.ss
cp id_rsa.pub authorized_keys
systmctl restart sshd