Ktor部署至VPS遇com.jcraft.jsch.JSchException: Auth fail认证失败
部署Ktor后端至VPS时遇到JSch认证失败问题
我是编程新手,使用Ktor搭建后端并尝试部署至虚拟专用服务器(VPS),但多次部署均遇到com.jcraft.jsch.JSchException: Auth fail错误,采用SSH密钥进行认证。
我的Build.gradle.kts文件
val ktor_version: String by project val kotlin_version: String by project val logback_version: String by project val kmongo_version:String by project val commons_codec_version:String by project plugins { kotlin("jvm") version "1.7.21" id("io.ktor.plugin") version "2.1.3" id("org.jetbrains.kotlin.plugin.serialization") version "1.7.21" id("com.github.johnrengelman.shadow") version "5.2.0" } group = "com.androidjunior9" version = "0.0.1" application { mainClass.set("io.ktor.server.netty.EngineMain") project.setProperty("mainClassName", mainClass.get()) val isDevelopment: Boolean = project.ext.has("development") applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment") } repositories { mavenCentral() maven { url= uri("https://maven.pkg.jetbrains.space/public/p/ktor/eap") } } val sshAntTask = configurations.create("sshAntTask") dependencies { implementation("io.ktor:ktor-server-core-jvm:$ktor_version") implementation("io.ktor:ktor-server-websockets-jvm:$ktor_version") implementation("io.ktor:ktor-server-content-negotiation-jvm:$ktor_version") implementation("io.ktor:ktor-serialization-kotlinx-json-jvm:$ktor_version") implementation("io.ktor:ktor-server-call-logging-jvm:$ktor_version") implementation("io.ktor:ktor-server-netty-jvm:$ktor_version") implementation("ch.qos.logback:logback-classic:$logback_version") implementation("io.ktor:ktor-server-sessions:$ktor_version") testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version") testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version") implementation("io.ktor:ktor-server-auth-jvm:$ktor_version") implementation("io.ktor:ktor-server-auth-jwt-jvm:$ktor_version") // Kmongo implementation("org.litote.kmongo:kmongo:$kmongo_version") implementation("org.litote.kmongo:kmongo-coroutine:$kmongo_version") implementation("commons-codec:commons-codec:$commons_codec_version") sshAntTask("org.apache.ant:ant-jsch:1.10.12") } tasks.withType<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar> { manifest { attributes( "Main-Class" to application.mainClass.get() ) } } ant.withGroovyBuilder { "taskdef"( "name" to "scp", "classname" to "org.apache.tools.ant.taskdefs.optional.ssh.Scp", "classpath" to configurations.get("sshAntTask").asPath ) "taskdef"( "name" to "ssh", "classname" to "org.apache.tools.ant.taskdefs.optional.ssh.SSHExec", "classpath" to configurations.get("sshAntTask").asPath ) } task("deploy") { dependsOn("clean", "shadowJar") ant.withGroovyBuilder { doLast { val knownHosts = File.createTempFile("knownhosts", "txt") val user = "root" val host = "Ip" val key = file("keys/newkey") val jarFileName = "jarfile" try { "scp"( "file" to file("build/libs/$jarFileName"), "todir" to "$user@$host:/root/service", "keyfile" to "$key", "trust" to true, "knownhosts" to knownHosts ) "ssh"( "host" to host, "username" to user, "keyfile" to "$key", "trust" to true, "knownhosts" to knownHosts, "command" to "mv /root/service/$jarFileName /root/service/service.jar" ) "ssh"( "host" to host, "username" to user, "keyfile" to "$key", "trust" to true, "knownhosts" to knownHosts, "command" to "systemctl stop service" ) "ssh"( "host" to host, "username" to user, "keyfile" to "$key", "trust" to true, "knownhosts" to knownHosts, "command" to "systemctl start service" ) } finally { knownHosts.delete() } } } }
尝试过的解决方法
- 使用新的SSH密钥对
- 修改sshd_config配置
- 调整文件权限
- 确认提供的SSH密钥正确
- 更换依赖版本
最终解决方案
检查Ubuntu版本,JSch与Ubuntu最新版本兼容性不佳,建议使用20.04或18.04等旧版本。
内容的提问来源于stack exchange,提问作者Shivam Kadam
相关产品推荐
相关产品推荐

