Apr 21 2010

使用SSH隧道翻墙!

Category: Uncategorizedwuxicn @ 4:38 PM
使用SSH隧道翻墙你必须有一个国外可以通过SSH登录的空间。

做法很简单(感谢Sprayfly):
1. 将下面内容存成文件tunnel.sh:

#!/bin/bash
# --------------------------------------
#
#     Title: SSH HTTP Proxy Script
#    Author: Jonathan Lumb
#     Email: jonolumb (at) gmail (dot) com
#  Homepage: http://sprayfly.com
#      File: tunnel.sh
#   Created: July 05, 2009
#
#   Purpose: Establishes/closes a secure HTTP proxy
#
# --------------------------------------
########### Setup SSH Proxy #############
# You will need to have your server setup to run with public and private keys
export SSH_HOST=username@host
############ End of Setup #############

if [ ! -f /tmp/.tunnel ]
then
    echo "创建 SSH 隧道"
    ssh -f -D 9999 $SSH_HOST "if [ -f ~/.tunnel ]; then rm ~/.tunnel; fi; while [ ! -f ~/.tunnel ]; do echo > /dev/null; done" &
    touch /tmp/.tunnel
else
    echo "关闭 SSH 隧道"
    ssh $SSH_HOST "touch ~/.tunnel"
    rm /tmp/.tunnel
fi

exit

然后给这个文件增加执行权限:chmod +x tunnel.sh

2. 建立SSH的RSA授权文件:ssh-keygen -t rsa注意:密码留空。
这样在本地的 ~/.ssh/ 下就会产生两个文件 id_rsaid_rsa.pub,将这两个文件的访问权限设置为0700:chmod 0700 id_rsa id_rsa.pub
然后将密钥授权文件id_rsa.pub文件传到远程的~/.ssh/ 下,命名为:authorized_keys,操作:
scp id_rsa.pub username@host.com:~/.ssh/authorized_keys

3. 做完上面两步后就可以开启你的SSH tunnel了:
开启/关闭SSH隧道:./tunnel.sh
然后在浏览器中设置socks代理(http代理必须留空):127.0.0.1端口:9999

补充说明一点:要关闭ssh隧道还可以在远程的 $HOME 目录下建立一个 .tunnel 文件,这样本地ssh隧道就会自动关闭了,建立方法:touch .tunnel

Tags: , , ,

2 Responses to “使用SSH隧道翻墙!”

  1. huang says:

    请问ssh的登录密码在那里设置啊?

  2. wuxicn says:

    这是通过公钥授权方式登录的,不用输入密码,按照我写的一步一步做就行。如果想了解公钥授权的详细方法,见我的另一篇post: http://icoder.me/2010/07/05/openssh-public-key-authentication/

Leave a Reply