2015-12-29 19 views
0

我經常使用ssh隧道。我打開一個終端來創建隧道(例如ssh -L 1111:servera:2222 [email protected])。然後我打開一個新的終端來完成我的工作。有沒有辦法在終端上建立隧道,並以某種方式將它放在後臺,所以我不需要開闢一個新的終端?我試着把「&」放在最後,但這並沒有成功。在輸入密碼之前,隧道進入了後臺。然後我做了fg,輸入了密碼,我被困在ssh會話中。如何在建立SSH隧道的同一終端上執行其他「東西」

我知道一個可能的解決方案是使用屏幕或tmux或類似的東西。有沒有簡單的解決方案,我失蹤了?

+0

看看[x2go](http://wiki.x2go.org/doku.php)! –

回答

2

還有就是-f-N選項正是爲:

-f  Requests ssh to go to background just before command execution. This is useful if 
     ssh is going to ask for passwords or passphrases, but the user wants it in the 
     background. This implies -n. The recommended way to start X11 programs at a remote 
     site is with something like ssh -f host xterm. 

     If the ExitOnForwardFailure configuration option is set to ``yes'', then a client 
     started with -f will wait for all remote port forwards to be successfully established 
     before placing itself in the background. 

-N  Do not execute a remote command. This is useful for just forwarding ports 
     (protocol version 2 only). 

所以完整的命令將ssh -fNL 1111:servera:2222 [email protected]rverb

防止ssh詢問密碼的一種方法也是使用SSH public keys與代理進行身份驗證,該代理會保存密碼或使用外部圖形程序(如pinentry)提示密碼。

查看autossh可能也很有用,它會在連接斷開時自動重新連接SSH。

+0

我試過了'ssh -f -L 1234:servera:5678 user @ serverb'並得到了'不能在沒有命令執行的情況下進入後臺。' – abalter

+0

是的,我一直懶得設置密鑰。沒有努力去學習如何去做。 autossh看起來很有趣。 – abalter

+0

添加'-N'參數來解決這個問題。 – cdauth