在Deepin Linux 上使用ssh问题集锦
2017-03-13 10:00:16.0

 近期在Deepin Linux上使用ssh远程连接其他Linux服务器时,发现有些问题并逐步解决了;特记录下来供大家参考。

环境

  • 个人电脑 Deepin Linux 15.2
  • 远程服务器 CentOS 7

SSH连接慢

在用ssh连接远程服务器时,发现要等待一定时间。排查过程如下:

  1. 将ssh连接过程信息输出

    ➜ tonny@tonny-pc  ~  ssh -v root@115.29.240.144

    然后就会输出一大堆debug,通过debug信息就可以看到连接到什么地方被耽搁了比如会显示如下信息:

    debug1: Next authentication method: gssapi-with-mic
    debug1: Unspecified GSS failure. Minor code may provide more informationNo credentials cache found
  2. 检测连接时间

    ➜ tonny@tonny-pc  ~ time ssh root@115.29.240.144 exit

解决办法

  1. 修改服务器端配置(修改参数值或去掉注释)

    [root@snails ~]# vi /etc/ssh/sshd_config UseDNS=no #关闭DNS反向解析 GSSAPIAuthentication no #关闭SERVER上的GSS认证 IgnoreRhosts yes #打开SERVER上的IgnoreRhosts参数 [root@snails ~]# systemctl restart sshd
  2. 修改本机配置(考虑到没必要去每台服务器上修改)

    ➜ tonny@tonny-pc  ~  sudo vi /etc/ssh/ssh_config
    GSSAPIAuthentication no #关闭SERVER上的GSS认证

超时自动断开连接

  在离开办公桌一段时间后,发现原来的ssh连接已被断开。提示信息如下:

packet_write_wait: Connection to 115.29.240.144 port 22: Broken pipe

解决办法

  1. 修改服务器端配置(修改参数值或去掉注释)

    [root@snails ~]# echo "ClientAliveInterval 60">>/etc/ssh/sshd_config [root@snails ~]# systemctl restart sshd

      重启OPENSSH服务器后该项设置会生效。每一个连接到此服务器上的客户端都会受其影响。应注意启用该功能后,安全性会有一定下降(比如忘记登出时……),所以 建议用客户端设置。
    如果您只想让当前的 ssh 保持连接,可以使用以下的命令:

    ➜ tonny@tonny-pc  ~  ssh -o ServerAliveInterval=60 user@sshserver
  2. 修改本机配置

    [root@snails ~]# echo "ServerAliveInterval 60">>/etc/ssh/ssh_config

采用spawn自动登录服务器(示例)

➜ tonny@tonny-pc  ~ vi ssh_115.29.240.144 #!/usr/bin/expect -f set timeout -1 set passwd "THIS@IS@PASS" spawn ssh -p22 root@115.29.240.144
expect { "yes/no" { send "yes\r";exp_continue } "password:" { send "$passwd\r" }
}
interact
➜ tonny@tonny-pc  ~  chmod +x ssh_115.29.240.144

补充说明

  服务器安全是个永恒的话题,并且安全一直都是相对的。长期检查服务器登录日志、记录登录用户操作等等都是运维人员必须要做的日常事务。当然一开始就加强安全设置会更好。下面的内容主要介绍 如何配置ssh使用密钥登录,禁止口令登录

创建用户并赋予root权限

[root@snails ~]# adduser tonny [root@snails ~]# passwd tonny [root@snails ~]# ll /home/ drwx------ 2 tonny        tonny        4096 7月  20 16:09 tonny
[root@snails ~]# id tonny uid=1002(tonny) gid=1002(tonny) 组=1002(tonny)
[root@snails ~]# visudo ## Allow root to run any commands anywhere root ALL=(ALL) ALL
tonny ALL=(ALL) ALL

编辑sshd配置文件,修改一些默认选项

[root@snails ~]# vi /etc/ssh/sshd_config #使用不常用端口 Port 63210 #仅使用SSH2协议 Protocol 2 #修改密钥生成强度 ServerKeyBits 1024 #禁止root账户通过ssh登录 PermitRootLogin no #禁止使用常规的用户名密码方式登录,此项慎用#在没有生成好Key,并且成功使用之前,要设置为yes PasswordAuthentication no #禁止空密码登录 PermitEmptyPasswords no

生成个人的公钥与私钥

[root@snails ~]# su - tonny [root@snails ~]$ ssh-keygen -t rsa -P '' -C "tonny" #改名为sshd支持的默认公钥文件名 [tonny@snails ~]$ mv ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys #设置公钥文件名,如不设置为400,会无法登录 [tonny@snails ~]$ chmod 400 ~/.ssh/authorized_keys #取出私钥文件(通过sftp下载) [tonny@snails ~]$ rm -rf ~/.ssh/id_rsa

利用私钥文件登录

#自己的电脑,删除原有的公钥信息 ➜ tonny@tonny-pc  ~ ssh-keygen -R 115.29.240.144
➜ tonny@tonny-pc  ~/ssh  mv id_rsa 115.29.240.144.pem 
➜ tonny@tonny-pc  ~/ssh  chmod 400 115.29.240.144.pem 
➜ tonny@tonny-pc  ~/ssh  ssh  -p 63210 tonny@115.29.240.144 -i 115.29.240.144.pem

验证

#服务器 [root@snails ~]# systemctl restart sshd #自己的电脑 ➜ tonny@tonny-pc  ~/ssh  ssh -p 63210 tonny@115.29.240.144                      
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
➜ tonny@tonny-pc  ~/ssh  ssh  -p 63210 root@115.29.240.144 -i 115.29.240.144.pem
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
  1. 为什么说是安全的SSH链接呢?关闭了空密码链接、禁止常规的ssh用户名密码方式登录,只有在服务器上生成过key文件的用户方可访问,key文件+密钥,保护更完善一些。
  2. 友情提示: PasswordAuthentication no配置内容,一定要在已经生成好了至少一个key并可从远程登录之后再使用,否则会导致直接无法登录。

附录:
  经常登录远程服务器操作,可以根据自己的喜好进行快捷配置
例如采用spawnaliasoh-my-zsh支持的custom脚本config配置