【问题标题】:jenkins tomcat ssh permission deniedjenkins tomcat ssh权限被拒绝
【发布时间】:2020-02-03 05:12:43
【问题描述】:

尝试在管道中使用 Jenkins ssh 代理插件将 war 文件复制到 tomcat(两者都在 Ec2 上的 Ubuntu 18.04 上运行)。

使用标准示例代码并使用基于密钥的身份验证。 Tomcat 已启动并正在运行。

stage('Deploy to Tomcat'){
        sshagent(['Tomcat-cred']) {
         sh 'scp -o StrictHostKeyChecking=no target/*.war ubuntu@xxxx:/opt/tomcat/latest/webapps/'
      }
   }

获取错误:scp:/opt/tomcat/latest/webapps//myweb-0.0.7-SNAPSHOT.war:权限被拒绝

已设置具有“最新”(符号链接)权限的 tomcat 用户

这是我的文件权限:

drwxrwxrwx 7 tomcat tomcat  4096 Sep 16 15:52 webapps.

我现在尝试了一些更改:

scp: /opt/tomcat/latest/webapps/: 没有这样的文件或目录

scp: /opt/tomcat/apache-tomcat-9.0.26/webapps/: 没有这样的文件或目录

目录当然存在:

    ubuntu@ip-xxxxx:/opt/tomcat/apache-tomcat-9.0.26$ ls

  webapps

【问题讨论】:

  • tomcat用户真的有权限在ubuntu@xxxx:/opt/tomcat/latest/webapps/上写吗?直接从命令行尝试怎么样。也许这不是 sshAgent 的问题..
  • 当我与 Jenkins 进行 ssh 时,我使用 'ubuntu' 用户 - 但该用户也应该具有权限。在上面的帖子中添加了更多细节。现在错误不同了——“没有这样的文件”
  • 我尝试在 google 中搜索 'scp no such file or directory but file exists',并且有几种解决方案,但我不确定哪一个可以解决您的问题。也许你可以尝试其中一些解决方案。

标签: tomcat jenkins jenkins-pipeline ubuntu-18.04 ssh-agent


【解决方案1】:

我想出了一些解决办法..

1.将文件发送到另一个目录,然后再次复制到目标目录

stage('Deploy to Tomcat'){
      sshagent(['Tomcat-cred']) {
         sh """
           scp -o StrictHostKeyChecking=no target/*.war ubuntu@xxxx:/home/ubuntu
           ssh -o StrictHostKeyChecking=no ubuntu@xxxx 'cp -r /home/ubuntu/*.war /opt/tomcat/latest/webapps/'
         """
      }
   }

2.不只是使用sshagent插件

stage('Deploy to Tomcat'){
      script {
         // you may need to set ssh keys on target server
         sh 'scp -o StrictHostKeyChecking=no target/*.war ubuntu@xxxx:/opt/tomcat/latest/webapps/'
      }
   }

希望你能解决这个问题..

【讨论】:

  • @tonarimochi - 谢谢
【解决方案2】:

这个问题原来是一个错误喜剧。对不起。

问题在于 IP 地址在您停止并重新启动后在 AWS 上不断变化(正常行为)。我一直在弄乱 Jenkins 配置和 Jenkinsfile 中的 IP——Tomcat 服务器和 Jenkins 服务器 IP,不知何故其中一个是错误的(很难跟上)。但是我一直在检查终端——在终端上你当然需要有正确的 IP,这使得调试变得困难。

哦,好吧,有时简单的事情也会搞砸

【讨论】:

  • 很高兴知道问题已经解决了!很高兴你找到了原因并分享到这里:D
【解决方案3】:

在 Tomcat_server 中更改 webapps 的所有权可以避免此错误

chown -R ec2-user:ec2-user  /opt/apache-tomcat-9.0.29/webapps

【讨论】:

    猜你喜欢
    • 2018-10-29
    • 2011-02-11
    • 2015-06-18
    • 2021-07-11
    • 1970-01-01
    • 2019-11-24
    • 2016-10-15
    • 2014-11-08
    • 2019-03-11
    相关资源
    最近更新 更多