ssh connection through proxy
30 June 2020
最近经常在Azure Global上的Ubuntu下工作。无奈,在中国访问Azure国际云速度是相当的慢。好在有国际代理,那么问题来了,如何通过代理 ssh
远程登录Ubuntu呢?
方法是这样的:
alias ubuntu='ssh -i ~/.ssh/id_rsa km@kimi.im -o "ProxyCommand=nc -X connect -x 127.0.0.1:1080 %h %p"'
以上这个 alias
就可以通过本地1080端口的代理访问远程服务器
kimi.im
,用户名为 km
。
不过,如果你没有装 nc
的话,敲了 ubuntu
命令时候,会提示说找不到
nc
, Cygwin
环境用 apg-cyg
安装 nc
:
$ apt-cyg install nc
Installing nc
--2020-06-30 20:47:40-- https://mirrors.tuna.tsinghua.edu.cn/cygwin//x86_64/release/nc/nc-1.107-4.tar.bz2
Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8.193, 2402:f000:1:408:8100::1
Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.8.193|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 17895 (17K) [application/octet-stream]
Saving to: ‘nc-1.107-4.tar.bz2’
nc-1.107-4.tar.bz2 100%[===================>] 17.48K --.-KB/s in 0s
2020-06-30 20:47:41 (36.0 MB/s) - ‘nc-1.107-4.tar.bz2’ saved [17895/17895]
nc-1.107-4.tar.bz2: OK
Unpacking...
Package nc requires the following packages, installing:
cygwin
Package cygwin is already installed, skipping
Package nc installed
另外,我还在远程运行 clojure
的 nrel
,通过 Emacs
cider
连接,所以还要把代理加到 cider
的远程连接上:
(defun nrepl--ssh-tunnel-command (ssh dir port) "Command string to open SSH tunnel to the host associated with DIR's PORT." (with-parsed-tramp-file-name dir v ;; this abuses the -v option for ssh to get output when the port ;; forwarding is set up, which is used to synchronise on, so that ;; the port forwarding is up when we try to connect. (format-spec "%s -v -N -L %p:localhost:%p %u'%h' %x" `((?s . ,ssh) (?p . ,port) (?h . ,v-host) (?u . ,(if v-user (format "-l '%s' " v-user) "")) (?x . "-o \"ProxyCommand=nc -X connect -x 127.0.0.1:1080 %h %p\"")))))
好了,这样 ssh
和 cider
都可以快速访问Azure了。