This box will be fixed on your screen using Firefox, Opera9 or IE7.

SSH: Port Forwarding

在互联网上自由无虞地冲浪方法有很多种,不管是什么原理,归根结底前提都要有至少一台能连接的境外主机。现在很多朋友都购买了美国的虚拟主机,除了美国主机价格实在,性价比高,而且他们还提供 SSH 管理。插一句,对于 Linux 学习来说,看手册是真正的王道,所以我们先来看看手册上怎么描述:

ssh (SSH client) is a program for logging into a remote machine and for executing commands on a remote machine. It is intended to replace rlogin and rsh, and provide secure encrypted communications between two untrusted hosts over an insecure network.

SSH 就是在不安全网络的两台互不信任的主机之间进行加密通信执行远程管理的。手册上继续接着往下看,SSH 有个很基本的功能:端口转发──

-D [bind_address:]port

Specifies a local “dynamic” application-level port forwarding. This works by allocating a socket to listen to port on the local side, optionally bound to the specified bind_address. Whenever a connection is made to this port, the connection is forwarded over the secure channel, and the application protocol is then used to determine where to connect to from the remote machine. Currently the SOCKS4 and SOCKS5 protocols are supported, and ssh will act as a SOCKS server. Only root can forward privileged ports. Dynamic port forwardings can also be specified in the configuration file.

PiC重点来了,SSH 可以指定本地动态的应用级的端口转发,数据仍然通过加密隧道转发,并且支持 SOCKS4 和 SOCKS5 协议。比如随便指定转发一个 9981 端口(ssh -D 9981 remotehost),SSH 登录后那么本地就可以使用 localhost:9981 这个加密的 Socks5代理。由于 Firefox 中可以直接使用 socks 代理,设置好后浏览网页感觉速度还是非常不错的(取决于你与远程主机之间的速度)。为了便于切换代理,很多人都安装了扩展,其实使用 PAC (自动代理配置脚本)文件是个不错的方法(如图下方所示),还可以少安装一个扩展。

但问题也来了,不可能每次我们要使用代理的时候,都把 SSH 远程管理打开,还要保留一个 shell 窗口,这样真是比较麻烦。呵呵,实际上阅读完手册,我们可以一条命令组合一组参数,让它只使用端口转发功能并在后台安静运行:

SSH -qTfnN -D port remotehost(用证书验证就直接主机名,没用的还要加上用户名密码)

-q Quiet mode. 安静模式,忽略一切对话和错误提示。
-T Disable pseudo-tty allocation. 不占用 shell 了。
-f Requests ssh to go to background just before command execution. 后台运行,并推荐加上 -n 参数。
-n Redirects stdin from /dev/null (actually, prevents reading from stdin). -f 推荐的,不加这条参数应该也行。
-N Do not execute a remote command. 不执行远程命令,专为端口转发度身打造。

7 Comments

  1. wells Comments @ 08:49:53 on 2007-02-15

    不过使用代理还是会被GFW过滤……

  2. Dark Comments @ 19:29:37 on 2007-02-15

    普通代理当然会被过滤,但我这里说的是通过SSH隧道加密的,是通行无阻的。

  3. toy Comments @ 10:25:51 on 2007-02-16

    速度确实不错 :)

  4. ezhangyu Comments @ 20:48:51 on 2007-02-18

    对不起,借此请教一个问题:我的明基S73EG笔记本使用BENQ的BOIS升级程序升级BOIS,在机器重启时提示“err 4:Vfloppy failed”,按任意键机子启动进入XP,提示BOIS升级失败,以后每次启动机子时都有这个提示,要按键后才能进入启动,请问是不是这个BOIS升级程序使用了虚拟软盘技术?这个问题应如何解决?谢谢!

  5. Dark Comments @ 09:28:16 on 2007-02-23

    ezhangyu,vfloppy是另外一位兄台的“专利”,等他看到后会回答的,我无法越俎代庖。

  6. lh Comments @ 22:29:03 on 2008-02-20

    opera就无法使用socks代理了,哎,这个方法不错,等我研究一下再说

  7. 端口转发 Comments @ 17:14:46 on 2019-04-11

    把2>端口转å>到某个蜜罐 p>