Unable to negotiate with xx.xx.xx.xx port xx: no matching key exchange method found.Their offer: diffie-hellman-group1-sha1
今天设置密钥的时候阴差阳错将匹配方式给改了,复杂的就是每个连接服务器的用户都得重新设置一下了。。设置方法如下:
Mac os
在本地
sudo vim /etc/ssh/ssh_config
- 去掉
MACs hmac-md5,hmac-sha1,umac-64@openssh.com
前的"#" - 在末尾加
HostkeyAlgorithms ssh-dss,ssh-rsa KexAlgorithms +diffie-hellman-group1-sha1
- 这时候可以
ssh xx@xxx.xxx.xx.xx
可以发现错误提示已经变成下图所示:
- 为了方便后续操作,
vim /Users/xx/.ssh/known_hosts
去掉以服务器ip开头的那一行,消除访问的ip记录从而重新设置。 ssh-keygen -t rsa -C " "
ssh-copy-id -f xxx@xxx.xxx.xx.xx
后就可以连接了。
Windows
在本地
- 打开
cmd
控制命令行 - 输入
type nul> C:\Users\用户名\.ssh\config
(可以先鼠标操作确定一下目录),这样就创建好了一个无后缀的文件。 - 然后鼠标操作进入
C:\Users\用户名\.ssh\config
,用记事本打开config
输入下列文字从而将方法成功添加。Host xx.xx.xx.xx KexAlgorithms +diffie-hellman-group1-sha1
- 然后关闭文件,再次打开
cmd
,重新生成密钥,输入ssh-keygen -t rsa -b 4096
生成SSH密钥和公钥,密钥id_rsa
和公钥id_rsa.pub
文件都默认保存在(C:\User\用户.ssh)文件夹下,复制公钥所在文件夹`。 - 公钥文件id_rsa.pub传到远程服务器的authorized_keys文件中。
SET REMOTEHOST=xx@xxx.xxx.xx.xx scp xxx\.ssh\id_rsa.pub %REMOTEHOST%:~/tmp.pub ssh %REMOTEHOST% "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat ~/tmp.pub >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys && rm -f ~/tmp.pub"
- 然后连接就可以了。
how to disable diffie-hellman-group1-sha1 for SSH
因为filezila
无法打开支持该算法的服务器,所以最终决定更换算法。
ssh -Q kex
查看支持的匹配算法。diffie-hellman-group1-sha1 diffie-hellman-group14-sha1 diffie-hellman-group-exchange-sha1 diffie-hellman-group-exchange-sha256 ecdh-sha2-nistp256 ecdh-sha2-nistp384 ecdh-sha2-nistp521 curve25519-sha256@libssh.org
sudo vim /etc/ssh/sshd_config
编辑最后一行KexAlgorithms
,用上述除diffie-hellman-group1-sha1
的其他算法去代替。service ssh restart
重启SSH服务。