ssh

...

SSH Alive

客户端通过ssh链接到服务器时, 隔一段时间未执行操作, 会出现如下错误信息

client_loop: send disconnect: Broken pipe

解决办法一:客户端调整 alive 配置,编辑 ~/.ssh/config

# 全局配置,对所有链接生效
Host *
      ServerAliveInterval 60
      ServerAliveCountMax 30

# 对指定链接生效
Host xxx
      HostName ip
      User root
      IdentityFile ~/.ssh/xxx.pem
      ServerAliveInterval 60
      ServerAliveCountMax 30

解决办法二:调整服务端alive配置

如果有服务器的管理员权限,可以修改sshd的配置添加客户端保活配置

# Server 每隔多少秒向 Client 发送一次保活信息
ClientAliveInterval 60
# Server 向 Client 发送多少次保活信息后未收到客户端响应后断开
ClientAliveCountMax 30

服务端配置修改后, 需要重启 sshd 服务以便生效

systemctl restart sshd

使用PEM文件

首先修改 PEM 文件的权限

sudo chmod 600 <key.pem>

直接使用 pem 文件

ssh -i <xxx.pem> root@ip:port

也可以先使用 ssh-add 添加此 pem 文件

ssh-add -k <xxx.pem>

然后就可以正常使用了

ssh root@ip:port

ssh

ssh 本地端口转发

一些应用场景:

  • 安全要求高,希望通信被加密
  • 一些约定端口,可能受限,比如mysql的3306
  • k8s集群内的服务,不能直接访问,但是能通过ssh链接到master节点上, 间接访问比如mysql

通过ssh通道来访问mysql,在本地命令行输入以下命令

// 访问本地3306端口,等于通过ssh root@sshdip访问dbip:3306
// ssh -CNgv -L 3306:dbip:3306 root@sshdip // dev1

ssh -fNg -L 3307:127.0.0.1:3306 myuser@remotehost.com

The command tells ssh to log in to remotehost.com as myuser, go into the background (-f) and not execute any remote command (-N), and set up port-forwarding (-L localport:localhost:remoteport ). In this case, we forward port 3307 on localhost to port 3306 on remotehost.com.

然后就可以正常链接远程mysql数据库了,就像链接本地数据库一样

mysql -h 127.0.0.1 -P 3307 -u <username> -p <password>

The command tells the local MySQL client to connect to localhost port 3307 (which is forwarded via ssh to remotehost.com:3306). The exchange of data between client and server is now sent over the encrypted ssh connection.

当然也可以用你顺手的任何mysql客户端/可视化工具来访问3307端口

参考

posted @ 2017-12-15 10:23  taadis  阅读(110)  评论(0)    收藏  举报
扫码关注

扫码关注我