異常表現
com.jcraft.jsch.JSchException: Algorithm negotiation fail
at com.jcraft.jsch.Session.receive_kexinit(Session.java:595)
at com.jcraft.jsch.Session.connect(Session.java:325)
出現這個(ge)問題主(zhu)要是客戶端與服(fu)務器的算法不(bu)一(yi)致導(dao)致的,主(zhu)要是ssh版本不(bu)一(yi)致
解決方案一
使用命令查看(kan)服務器支持的算法
ssh -vvv 目標機器ip
再返回的(de)結果中找到服務端的(de)HostKeyAlgorithms和KexAlgorithms配置(zhi),如
HostKeyAlgorithms ssh-ed25519-cert-v01@openssh.com,ssh-ed25519,ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-dss-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,rsa-sha2-512,rsa-sha2-256,ssh-rsa,ssh-dss
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
手動登陸(lu)實例主機,編輯(ji)ssh配(pei)置(zhi)文件,注釋掉原有的HostKeyAlgorithms和KexAlgorithms配(pei)置(zhi),把(ba)上(shang)一步(bu)找的配(pei)置(zhi)粘貼進去
vi /etc/ssh/sshd_config
重啟sshd
systemctl restart sshd
檢查(cha)ssh運行狀態是(shi)否成功重啟,狀態為running為正常

可以(yi)通(tong)過以(yi)下(xia)命(ming)令檢查配置是否(fou)生效(xiao)
ssh -o ProxyCommand="ncat --proxy {代理ip}:{代理port} --proxy-type socks5 --proxy-auth {代理賬號}:{代理密碼} %h %p" {目標主機ip} -vvv