Tag Archives: ssh

服务器 ssh 自动断开问题

有好几种解决方案
1、最不好的一种,设置终端超时自动输入字符。对 iTerm 而言 Profile 的 Session tab 里面可以设置。缺点很明显,超时时会自动输入字符影响使用。
2、客户端 ssh 配置定时保持连接,方法是修改 ~/.ssh/config 文件,加入
Host *
ServerAliveInterval=60
这样每 60 秒给服务器发送一个 no-op 包用来保持连接。
3、对单次连接可以使用 ssh -o ServerAliveInterval=30 user@host 命令来传递 ServerAliveInterval 参数
4、服务端 ssh 配置,方法是修改 /etc/ssh/sshd_config 文件,取消
TCPKeepAlive yes
ClientAliveInterval 30
ClientAliveCountMax 3
5、行代码的注释开启定时发包保持连接的功能。记得重启 ssh 服务激活配置。
6、当然还有更高级的技术。可以试试 mosh,除了保持连接外,还可以实现自动连接。需要分别在服务器和客户端安装软件。
2018-03-08 15:31:14
既然讲到了 mosh,讲下怎么安装吧。三行命令,以 debian 服务器,mac 客户机为例:
1、服务端安装 apt-get install mosh
2、配置语言 vim ~/.bashrc 添加 export LC_ALL="en_US.UTF-8",source ~/.bashrc
3、客户端 brew install mosh
下面就可以使用了
mosh user@host

批量替换 github 的 https 连接为 ssh 连接

要把 git repo 的 https 连接方式改成 ssh 的一般有两种方法:
一、修改 repo 下 .git 文件夹里的 config 文件,将 [remote "origin"] 里的 https://github.com/ 改成 git@github.com:
二、使用 git 提供的命令来修改 git remote set-url origin git@github.com:user/repo.git
两种方法不做修改的话都没法实现批量处理,对整个 workspace 内那么多 repo 没个批量处理的方法那还不疯了。
其实批量处理也很简单啦,一条命令的事情,就是对上面的方法一作一些小修改,用了三个比较常用的命令 find, grep, sed
$ find ~/workspace -exec grep "https://github.com/" ‘{}’ ; -exec sed -i "" "s^https://github.com/^git@github.com:^g" {} ;
查找文件夹下所有文件,找到其中的 https://github.com/ 替换成 git@github.com: