普通登录

  1. ssh -p 2211 root@140.206.185.170
  2. # 使用默认端口22
  3. ssh root@140.206.185.170

免密登录

先安装git

  1. # 生成sshkey, 如果已经和github绑定过,则这一步不用执行
  2. ssh-keygen
  3. # 复制到目标服务器
  4. ssh-copy-id root@192.168.0.106
  5. # 等效于:cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
  6. # 重启服务
  7. service sshd restart
  8. # 登录
  9. ssh root@192.168.0.106

修改ssh登录端口

  1. vim /etc/ssh/sshd_config
  2. service sshd restart

SSH心跳

  1. sudo vim /etc/ssh/sshd_config
  2. # 每30秒向客户端发送一次心跳
  3. ClientAliveInterval 30
  4. ClientAliveCountMax 6

登录异常

找不到host key

  1. Could not load host key: /etc/ssh/ssh_host_rsa_key
  2. Could not load host key: /etc/ssh/ssh_host_ecdsa_key
  3. Could not load host key: /etc/ssh/ssh_host_ed25519_key

解决(生成key):

  1. sudo ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
  2. sudo ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
  3. sudo ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key

没有权限登录

  1. $ ssh alvin@172.17.163.113
  2. alvin@172.17.163.113: Permission denied (publickey).

解决方案:配置密码可以登录。

  1. sudo sed 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config