Pel*_*ier 290 networking linux security ssh shell
我在工作中使用 SSH 隧道绕过各种 idotic 防火墙(我的老板没问题:))。问题是,一段时间后 ssh 连接通常会挂起,并且隧道被破坏。
如果我至少可以自动监控隧道,我可以在它挂起时重新启动隧道,但我什至没有想到这样做的方法。
可以告诉我如何防止我的 ssh 连接挂起的人当然可以加分!
Kei*_*thB 347
听起来你需要autossh。这将监视 ssh 隧道并根据需要重新启动它。我们已经使用它几年了,它似乎运行良好。
autossh -M 20000 -f -N your_public_server -R 1234:localhost:22 -C
Run Code Online (Sandbox Code Playgroud)
关于 -M 参数的更多细节在这里
Ces*_*arB 44
所有有状态防火墙在一段时间内没有看到该连接的数据包后都会忘记该连接(以防止状态表变得充满连接,而两端都已死亡而没有关闭连接)。大多数 TCP 实现会在很长时间后发送一个 keepalive 数据包,而不会收到另一方的消息(2 小时是一个常见值)。但是,如果有状态防火墙在发送保活数据包之前忘记了连接,则长期存在但空闲的连接将死亡。
如果是这种情况,解决方案是防止连接变为空闲。OpenSSH 有一个名为ServerAliveInterval的选项,可用于防止连接空闲时间过长(作为奖励,即使连接空闲,它也会检测到对等方何时死亡)。
Jaw*_*awa 31
我已经使用以下 Bash 脚本在前一个终止时继续生成新的 ssh 隧道。当您不想或无法安装其他软件包或使用编译器时,使用脚本会很方便。
while true
do
ssh <ssh_options> [user@]hostname
sleep 15
done
Run Code Online (Sandbox Code Playgroud)
请注意,这需要一个密钥文件来自动建立连接,但 autossh 也是如此。
Ian*_*anB 29
Systemd 非常适合于此。
创建一个/etc/systemd/system/sshtunnel.service包含以下内容的服务文件:
[Unit]
Description=SSH Tunnel
After=network.target
[Service]
Restart=always
RestartSec=20
User=sshtunnel
ExecStart=/bin/ssh -NT -o ServerAliveInterval=60 -L 5900:localhost:5900 user@otherserver
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
(修改 ssh 命令以适应)
sshtunnel因此请确保用户首先存在systemctl enable sshtunnel将其设置为在启动时启动的问题systemctl start sshtunnel立即开始2018 年 1 月更新:某些发行版(例如 Fedora 27)可能会使用 SELinux 策略来阻止 systemd init 使用 SSH,在这种情况下,需要创建自定义策略以提供必要的豁免。
小智 24
在您自己的 mac 或 linux 机器上配置您的 ssh,每 3 分钟保持服务器 ssh 处于活动状态。打开终端并在您的家中使用您的隐形 .ssh:
cd ~/.ssh/
Run Code Online (Sandbox Code Playgroud)
然后创建一个 1 行配置文件:
echo "ServerAliveInterval 180" >> config
Run Code Online (Sandbox Code Playgroud)
你还应该添加:
ServerAliveCountMax xxxx (high number)
Run Code Online (Sandbox Code Playgroud)
默认值为 3,因此 ServerAliveInterval 180 将在 9 分钟(ServerAliveInterval 指定的 3 分钟间隔中的 3 分钟)后停止发送。
Mat*_*way 16
对于那些不想(或)不能使用 AutoSSH 的人...
我有一个 NAS,我想从互联网访问,我无法使用端口转发,因为我的 ISP 使用 CGNAT(我的公共 IP 并不是真正的公共 IP,我位于另一个路由器后面,我无法控制) )。因此,为了访问我的 NAS,我有一个 VPS(我从 OVH 租用它,每月费用非常低),并且它有一个固定的公共 IP 地址。因此,要从互联网访问我的 NAS,我只需在 NAS 和 VPS 之间创建一个 SSH 隧道,该隧道始终可靠地保持开放状态(用于全天候访问)。然而,由于不活动(尽管 ssh 进程保持运行),我的 SSH 隧道被“关闭”。通过让客户端(在我的例子中是 VPS)使用保持活动选项“ping”服务器(在我的例子中是 NAS),可以轻松克服这个问题。
要创建 SSH 隧道,我发出以下命令(从 NAS):
ssh -NT -o ServerAliveInterval=60 -o ServerAliveCountMax=10 -o ExitOnForwardFailure=yes -i /var/services/homes/foouser/.ssh/id_rsa -R 8080:localhost:80 -R 4443:localhost:443 foouser@<VPS>
Run Code Online (Sandbox Code Playgroud)
解释一下这个命令:
-N- 不执行远程命令;这对于转发端口很有用。-T- 禁用伪 tty 分配。-R 8080:localhost:80- 指定远程(服务器)主机上的给定端口将转发到本地端的给定主机和端口。在这种情况下,意味着将远程服务器的80端口转发到客户端的8080端口。-i /path/to/key- 指定用于建立 ssh 会话的 ssh 密钥的路径,否则您将必须输入用户名(如果未提供)和密码来建立 ssh 会话。ServerAliveInterval- 客户端在向服务器发送“服务器活动”消息以保持连接活动之前等待的秒数。ServerAliveCountMax- 可能在没有服务器回复的情况下发送的“服务器活动”消息的数量。如果达到此阈值,ssh 将与服务器断开连接,从而终止会话。ExitOnForwardFailure- 如果设置为“yes”,如果 ssh 无法设置所有请求的动态、隧道、本地和远程端口转发(例如,如果任一端无法绑定和侦听指定端口),则应终止连接。foouser@<VPS>foouser- 指定用于与服务器建立远程端口转发 ssh 会话的用户帐户<VPS>。还值得向服务器(在我的例子中,在我的 VPS 上)添加一些 ssh 配置选项;通过添加以下文件(如果尚不存在):
[foouser@vps ~]$ cat /home/foouser/.ssh/config
Host *
TCPKeepAlive yes
ClientAliveInterval 30
ClientAliveCountMax 9999
Run Code Online (Sandbox Code Playgroud)
注意:您可以将*(这意味着将此配置应用于“所有主机”)替换为特定主机 - 在我的情况下,我的 NAS(即连接到我的 VPS 的主机)位于我的路由器后面;我的路由器的公共 IP 地址经常更改,因为它是 DHCP 分配的(来自我的 ISP),所以我坚持使用“所有主机”。
SystemD 进程 (Synology NAS)
我也有这个命令(将 SSH 隧道作为 systemd 进程启动的命令,如果有人感兴趣,这里是脚本:
foouser@nas:~$ cat /etc/systemd/system/sshtunnel-web.service
[Unit]
Description=SSH Tunnel for WebStation
After=network.target
[Service]
Restart=always
RestartSec=1
User=foouser
ExecStart=/bin/ssh \
-NT \
-o ServerAliveInterval=60 \
-o ServerAliveCountMax=10 \
-o ExitOnForwardFailure=yes \
-i /var/services/homes/foouser/.ssh/id_rsa \
-R 8080:localhost:80 \
-R 4443:localhost:443 \
foouser@<VPS>
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
要启动并启用 SSH 隧道服务:
foouser@nas:~$ sudo systemctl daemon-reload
foouser@nas:~$ sudo systemctl start sshtunnel-web.service
foouser@nas:~$ sudo systemctl enable sshtunnel-web.service
Run Code Online (Sandbox Code Playgroud)
这对我来说已经可靠地工作了几个月。这包括在我的家庭路由器、VPS 服务器和 NAS 多次重新启动后保持可靠。
小智 13
在我看来,你们肯定都误解了 ServerAliveCountMax。据我了解文档,它是服务器活动消息的数量,这些消息可以在不终止连接的情况下得到答复。因此,在我们在这里讨论的情况下,将其设置为高值只会确保不会检测到和终止挂起的连接!
简单地设置 ServerAliveInterval 应该足以解决防火墙忘记连接的问题,并且将 ServerAliveCountMax 设置为低值将使始发端注意到失败并在连接失败时终止。
您想要的是,1) 连接在正常情况下永久保持打开状态,2) 检测到连接失败并在失败时退出,以及 3) 每次重新发出 ssh 命令退出(你如何做到这一点非常依赖于平台,Jawa 建议的“while true”脚本是一种方式,在 OS XI 上实际上设置了一个启动项)。
小智 12
ServerAliveInterval如果隧道问题是由过期的 NAT 会话生成,请始终使用SSH 选项。
如果连接完全中断,请始终使用重生方法,这里至少有三个选项:
while true do ssh ...; sleep 5; done) 不删除 sleep 命令,ssh可能会很快失败并且您将重新生成太多进程/etc/inittab, 要访问在其他国家/地区运送和安装的盒子,在 NAT 之后,无需端口转发到盒子,您可以将其配置为创建返回给您的 ssh 隧道:
tun1:2345:respawn:/usr/bin/ssh -i /path/to/rsaKey -f -N -o "ServerAliveInterval 180" -R 55002:localhost:22 user@publicip 'sleep 365d'
Run Code Online (Sandbox Code Playgroud)Ubuntu 上的 upstart 脚本,其中/etc/inittab不可用:
start on net-device-up IFACE=eth0
stop on runlevel [01S6]
respawn
respawn limit 180 900
exec ssh -i /path/to/rsaKey -N -o "ServerAliveInterval 180" -R 55002:localhost:22 user@publicip
post-stop script
sleep 5
end script
Run Code Online (Sandbox Code Playgroud)或始终使用这两种方法。
小智 8
我用这个解决了这个问题:
编辑
~/.ssh/config
Run Code Online (Sandbox Code Playgroud)
并添加
ServerAliveInterval 15
ServerAliveCountMax 4
Run Code Online (Sandbox Code Playgroud)
根据ssh_config 的手册页:
ServerAliveCountMax
Sets the number of server alive messages (see below) which may be
sent without ssh(1) receiving any messages back from the server.
If this threshold is reached while server alive messages are
being sent, ssh will disconnect from the server, terminating the
session. It is important to note that the use of server alive
messages is very different from TCPKeepAlive (below). The server
alive messages are sent through the encrypted channel and there?
fore will not be spoofable. The TCP keepalive option enabled by
TCPKeepAlive is spoofable. The server alive mechanism is valu?
able when the client or server depend on knowing when a connec?
tion has become inactive.
The default value is 3. If, for example, ServerAliveInterval
(see below) is set to 15 and ServerAliveCountMax is left at the
default, if the server becomes unresponsive, ssh will disconnect
after approximately 45 seconds. This option applies to protocol
version 2 only.
ServerAliveInterval
Sets a timeout interval in seconds after which if no data has
been received from the server, ssh(1) will send a message through
the encrypted channel to request a response from the server. The
default is 0, indicating that these messages will not be sent to
the server. This option applies to protocol version 2 only.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
240772 次 |
| 最近记录: |