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 上。
編輯:
Per Stéphane Chazelas 的評論:在兩台不同的遠端機器上嘗試命令時,我實際上註意到兩種略有不同的行為:
$ 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)
$ 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
在獲取文件(或重新打開終端)之前將其添加到您的文件中: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 $$ 指定要使用的基本監控埠。如果沒有回顯埠,則此埠和緊接其上方的埠(埠 + 1)應該是其他任何東西都沒有使用的東西。autossh 將在基礎監控埠上發送測試數據,並在上面的埠上接收它。例如,如果您指定“-M 20000”,autossh 將設置轉發,以便它可以在埠 20000 上發送數據並在 20001 上接收它。