Autossh 在本地的工作方式与在远程服务器上的工作方式不同

Autossh 在本地的工作方式与在远程服务器上的工作方式不同

如果我autossh在本地计算机上启动,如下所示:

autossh -M 20000 -f - nNT -L 192.168.144.1:5433:127.0.0.1:5432 remote-user@remote-server-B

ssh它会自动在后台启动以下命令,这正是我所期望的:

ps aux | grep -i ssh
/usr/lib/autossh/autossh -M 20000 -f - nNT -L 192.168.144.1:5433:127.0.0.1:5432 remote-user@remote-server
/usr/bin/ssh -L 20000:127.0.0.1:20000 -R 20000:127.0.0.1:20001 -N -L 192.168.144.1:5433:127.0.0.1:5432 remote-user@remote-server-B

但在远程服务器 A 上启动的相同命令仅显示以下内容ps

/usr/lib/autossh/autossh -M 20000 -f - nNT -L 192.168.144.1:5433:127.0.0.1:5432 remote-user@remote-server-B

看来它没有启动底层ssh命令。

(请注意,如果我ssh在远程服务器A上手动启动命令,那么我就可以正确连接到远程服务器B。)

我无法弄清楚为什么该命令在远程计算机上的行为与在本地计算机上的行为不同......

这两个autossh版本都1.4g在我的本地计算机和远程服务器 A 上。

两台机器都运行 Ubuntu 20.04。

编辑

根据 Stéphane Chazelas 的评论:在两台不同的远程计算机上尝试该命令时,我实际上注意到两种略有不同的行为:

1.

$ strace -fe execve autossh -M 20000  -f  -nNT -L 192.168.144.1:5433:127.0.0.1:5432 remote-user@remote-server-B
execve("/usr/bin/autossh", ["autossh", "-M", "20000", "-f", "-nNT", "-L", "192.168.144.1:5433:127.0.0.1:5432", "remote-user@remote-server-B"], 0x7fff532c2f68 /* 40 vars */) = 0
strace: Process 23539 attached
[pid 23539] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=23539, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
strace: Process 23540 attached
[pid 23540] execve("/bin/egrep", ["egrep", "-q", "--", "-f?M ?[0-9]+"], 0x55b83714eae8 /* 38 vars */) = 0
[pid 23540] execve("/home/username/bin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x55b692ae0a28 /* 38 vars */) = -1 ENOENT (No such file or directory)
[pid 23540] execve("/home/username/.local/bin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x55b692ae0a28 /* 38 vars */) = -1 ENOENT (No such file or directory)
[pid 23540] execve("/home/username/anaconda3/condabin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x55b692ae0a28 /* 38 vars */) = -1 ENOENT (No such file or directory)
[pid 23540] execve("/opt/blenderc/bin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x55b692ae0a28 /* 38 vars */) = -1 ENOENT (No such file or directory)
[pid 23540] execve("/usr/local/sbin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x55b692ae0a28 /* 38 vars */) = -1 ENOENT (No such file or directory)
[pid 23540] execve("/usr/local/bin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x55b692ae0a28 /* 38 vars */) = -1 ENOENT (No such file or directory)
[pid 23540] execve("/usr/sbin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x55b692ae0a28 /* 38 vars */) = -1 ENOENT (No such file or directory)
[pid 23540] execve("/usr/bin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x55b692ae0a28 /* 38 vars */) = -1 ENOENT (No such file or directory)
[pid 23540] execve("/sbin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x55b692ae0a28 /* 38 vars */) = -1 ENOENT (No such file or directory)
[pid 23540] execve("/bin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x55b692ae0a28 /* 38 vars */) = 0
[pid 23540] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=23540, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
execve("/usr/lib/autossh/autossh", ["/usr/lib/autossh/autossh", "-M", "20000", "-f", "-nNT", "-L", "192.168.144.1:5433:127.0.0.1:5432", "remote-user@remote-server-B"], 0x55b83714f470 /* 38 vars */) = 0
strace: Process 23541 attached
[pid 23538] +++ exited with 0 +++
strace: Process 23542 attached
[pid 23542] execve("/usr/bin/ssh", ["/usr/bin/ssh", "-L", "20000:127.0.0.1:20000", "-R", "20000:127.0.0.1:20001", "-nNT", "-L", "192.168.144.1:5433:127.0.0.1:5432", "remote-user@remote-server-B"], 0x7ffce7ca53e0 /* 38 vars */) = 0
[pid 23542] +++ exited with 255 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=23542, si_uid=1000, si_status=255, si_utime=2, si_stime=0} ---
strace: Process 23543 attached
[pid 23543] execve("/usr/bin/ssh", ["/usr/bin/ssh", "-L", "20000:127.0.0.1:20000", "-R", "20000:127.0.0.1:20001", "-nNT", "-L", "192.168.144.1:5433:127.0.0.1:5432", "remote-user@remote-server-B"], 0x7ffce7ca53e0 /* 38 vars */) = 0

(最后 3 行定期无限重复,每次使用不同的进程 ID)

2.

$ strace -fe execve autossh -M 20000  -f  -nNT -L 192.168.144.1:5433:127.0.0.1:5432 remote-user@remote-server-B
execve("/usr/bin/autossh", ["autossh", "-M", "20000", "-f", "-nNT", "-L", "192.168.144.1:5433:127.0.0.1:5432", "remote-user@remote-server-B"], 0x7fff7be64dc8 /* 34 vars */) = 0
strace: Process 3850 attached
[pid  3850] +++ exited with 0 +++
strace: Process 3851 attached
[pid  3849] --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=3850, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
[pid  3851] execve("/bin/egrep", ["egrep", "-q", "--", "-f?M ?[0-9]+"], 0x55690bd42a08 /* 32 vars */) = 0
[pid  3851] execve("/usr/local/sbin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x5637ad7d9958 /* 32 vars */) = -1 ENOENT (No such file or directory)
[pid  3851] execve("/usr/local/bin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x5637ad7d9958 /* 32 vars */) = -1 ENOENT (No such file or directory)
[pid  3851] execve("/usr/sbin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x5637ad7d9958 /* 32 vars */) = -1 ENOENT (No such file or directory)
[pid  3851] execve("/usr/bin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x5637ad7d9958 /* 32 vars */) = -1 ENOENT (No such file or directory)
[pid  3851] execve("/sbin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x5637ad7d9958 /* 32 vars */) = -1 ENOENT (No such file or directory)
[pid  3851] execve("/bin/grep", ["grep", "-E", "-q", "--", "-f?M ?[0-9]+"], 0x5637ad7d9958 /* 32 vars */) = 0
[pid  3851] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=3851, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
execve("/usr/lib/autossh/autossh", ["/usr/lib/autossh/autossh", "-M", "20000", "-f", "-nNT", "-L", "192.168.144.1:5433:127.0.0.1:5432", "remote-user@remote-server-B"], 0x55690bd433a0 /* 32 vars */) = 0
strace: Process 3852 attached
[pid  3849] +++ exited with 0 +++
+++ exited with 1 +++

(然后它立即将控制台返回给用户)

答案1

好吧,我发现autossh在远程计算机上使用与在本地计算机上使用时发生的微小差异。

在后一台计算机上,GUI 弹出窗口提示我输入用于连接到远程服务器 B 的 ssh 密钥的密码。

当在任何远程计算机上使用连接到同一服务器 B 时,从未出现过此密码短语提示autossh,因为那里没有 GUI(无头服务器)(但当您使用它ssh自己时,它自然会提示输入密码短语)。

因此,我必须在运行之前让我的终端知道密钥密码autossh。您可以这样做,例如ssh-agent在启动之前在正在运行的终端内使用autossh。然后它突然起作用了。

详细解决方案

该解决方案作为示例给出。

在获取它之前将其添加到您的~/.bashrc文件中(或重新打开终端):

ssh-eval() {
   eval $(ssh-agent) && ssh-add "${1}"
}

export -f ssh-eval

然后运行:

ssh-eval ~/.ssh/your_ssh_key

输入对应的密码短语!这是autossh实际工作中缺失的部分!

autossh使用监视端口运行不是已经在遥控器上使用了!

autossh -M 20002 -f - nNT -L 192.168.144.1:5433:127.0.0.1:5432 remote-user@remote-server-B

由于端口20000被我的本地计算机使用,因此当使用另一台计算机的相同端口时无法连接。您只能通过运行相应的ssh命令本身来知道,例如:

$ ssh -L 20000:127.0.0.1:20000 -R 20000:127.0.0.1:20001 -N -L 192.168.144.1:5433:127.0.0.1:5432 remote-user@remote-server-B

Enter passphrase for key '/home/username/.ssh/your_ssh_key': 
****

Warning: remote port forwarding failed for listen port 20000

另请注意,您的本地端口尚未使用,否则您将面临如下情况:

...
bind: Address already in use
channel_setup_fwd_listener_tcpip: cannot listen to port: 5433

最后但并非最不重要的一点是,不要“盲目地”将监视端口仅增加 +1,如下所示:

-M 端口[:echo_port]

指定要使用的基本监视端口。如果没有 echo 端口,则该端口及其上方的端口 ( port + 1) 应该没有其他端口在使用。 autossh 将在基本监控端口上发送测试数据,并在上面的端口上接收它。例如,如果您指定“-M 20000”,autossh 将设置转发,以便可以在端口 20000 上发送数据并在 20001 上接收数据。

来源:https://linux.die.net/man/1/autossh

相关内容