如果我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 上。
编辑:
Per 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 +++
(然后它立即将控制台返回给用户)
autossh
好的,我发现了在远程机器上使用与在本地机器上使用时发生的微小差异。在后一台计算机上,GUI 弹出窗口提示我输入用于连接远程服务器 B 的 ssh 密钥的密码。
当在任何远程机器上使用连接到同一台服务器 B 时,从未出现此密码提示
autossh
,因为那里没有 GUI(无头服务器)(但当您使用ssh
它自己时,它自然会提示输入密码)。因此,我必须在运行之前让我的终端知道密钥密码
autossh
。您可以这样做,例如ssh-agent
在启动之前在运行的终端内使用autossh
。然后它突然起作用了。详细解决方案
该解决方案作为示例给出。
~/.bashrc
在获取文件(或重新打开终端)之前将其添加到您的文件中:然后运行:
输入对应的密码!这是
autossh
实际工作所缺少的部分!使用尚未在遥控器上使用
autossh
的监控端口运行!由于
20000
我的本地计算机使用了端口,因此在使用另一台计算机的相同端口时无法连接。您只能通过运行相应的ssh
命令本身来知道这一点,例如:另请注意,您的本地端口尚未使用,否则您将面临以下情况:
最后但并非最不重要的一点是,不要“盲目地”将监控端口仅增加 +1,因为:
来源:https ://linux.die.net/man/1/autossh