Posts Tagged: ssh


5
Jul 09

在 Linux 下使用你的网站主机作为加密代理服务器

曾经介绍过如何使用 Amazon EC2 建立自己的代理服务器来“翻墙“。用这个方法翻墙虽然网速很快,但还是有些缺点。Amazon EC2 是收费的,而且按小时算,所以不能随时都开着我的代理服务器。另外,每次想用代理的时候还得先等一两分钟,让服务器启动。

然而我今天看到了这个贴子,这才发现还可以使用一个普通的网站主机作为我的代理服务器。与 Amazon EC2 不同,普通的网站主机是 24/7 运行的,而且你本来就要用它做你的网站,不用额外花钱。以上帖子是专门写给使用 Dreamhost 的 Windows 用户。不过,凡是有支持 SSH 服务网站主机的 Linux 用户也可以享受翻墙的自由感。我为 Linux 用户编了一个小的 BASH 脚本,其主要功能为:

  • 与网站主机创建一个 SSH 隧道连接
  • SSH 隧道用完之后,安全地关闭 SSH 隧道连接

用这个脚本,我试了一下上平时被 GFW 封杀的 youtube.com… 结果 Firefox 差点让我晕倒了! 用这个代理,网速快极了… 感觉好像我还在英国上网似的 (那边一般都用 8M 以上的宽带) – 你可以看一下视频:

介绍一下脚本的内容

export SSH_HOST=admin@yoursite.com # username@host

这个需要你自己改一下… 就是你的 webhost 给你的 SSH 用户名和主机地址。 格式为 用户名@地址 (凡是用过 SSH 的人都应该知道写什么)

if [ ! -f /tmp/.tunnel ]

看看是否已经有 SSH 隧道开着。有的话就将其关闭。没有就创建新的 SSH 隧道。

ssh -f -D 9999 $SSH_HOST "if [ -f ~/.tunnel ]; then rm ~/.tunnel; fi; while [ ! -f ~/.tunnel ]; do echo > /dev/null; done" &

创建新的 SSH 隧道,设定本地 SOCKS 端口为 9999。隧道将会开着直到远程目录 “~/” 里有人创建 .tunnel 此文件为止。

touch /tmp/.tunnel

在你的电脑上创建 .tunnel 这个文件。这样脚本就知道正开着一个 SSH 隧道。

ssh $SSH_HOST "touch ~/.tunnel"

在远程目录 “~/” 里创建 .tunnel 此文件,这将会断开你电脑与代理服务器的连接

rm /tmp/.tunnel

删除 .tunnel 这个文件。这样脚本就知道 SSH 隧道已关闭。

下载并执行脚本
可以在此下载脚本

下载后放在你的首目录,比如在 ~/bin/。用一个文字编辑器修改脚本里的 ssh 变量并用 chmod 改一下文件权限

chmod u+x tunnel.sh

创建连接
在终端中执行脚本,执行后可以关闭终端。

open_tunnel.png

关闭连接
再次在终端中执行脚本并关闭 Firefox (如果 Firefox 还在用隧道的话,连接暂时无法关闭)

close_tunnel.png

用 SSH 密钥进行授权
你或许注意到了… 以上脚本里没有任何地方可以写下你的 SSH 密码。这就是因为我在用 SSH 密钥来进行电脑与服务器的授权工作,这样更自动化而且比将密码直接写入文本的文件里要安全多了。先打开一个终端,根据一下步骤进行密钥配置:

[jonolumb@jonoxps .ssh]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jonolumb/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/jonolumb/.ssh/id_rsa
Your public key has been saved in /home/jonolumb/.ssh/id_rsa.pub
The key fingerprint is:
h7:10:49:46:ab:2t:3b:a3:36:2z:15:56:d4:f2:b7:3d jonolumb@jonoxps

请注意,配置时密码要留为空白。

将生成的 /home/jonolumb/.ssh/id_rsa.pub 的内容复制到远程服务器目录下,具体位置是:
~/.ssh/authorized_keys
要保证远程的 authorized_keys 和 本地的 id_rsa 文件的权限均为 700。
这样就可以自动登录到远程服务器了。

配置 Firefox 使用代理
创建了 SSH 隧道之后,就进入 Firefox 并打开:

首选项 --> 高级 --> 连接设置

选择“手动配置代理”并将其设定为:
HTTP 代理: localhost 端口: 9999

firefox_proxy.png

如果你用这个脚本遇到什么问题,请跟我取得联系。我很想听你们的意见。谢谢!


6
Apr 08

New Host – Media Temple

Recently, I decided I was paying far too much for my web hosting package with Heart Internet UK, especially considering that they lacked support for some really important features such as Trackbacks and Pingbacks in Wordpress and did not support Subversion (useful for testing the latest development version of Wordpress). Despite having excellent customer support, the servers were often slow and the setup was a bit clunky – it was time to make a move!

Luckily, my friend Jacob over at jdpictures.co.uk was also a former Heart Internet customer itching to move to a better host. He had done some research and recommended Media Temple’s Grid Service hosting to me. Whilst I’m not going to go deep into the details of this new state of the art hosting technology, basically it means that if your site gets a spike in traffic, the server setup will immediately adapt to deal with it – meaning that your site should never slow down or be suspended. Media Temple also allows you to map multiple domain names to one hosting account for free (so sprayfly.com, inchina.co.uk, jdpictures.co.uk etc. can all be hosted on one server). This basically means that me and Jacob are able to share a hosting package with Media Temple (and share the costs) without having to make any compromises whatsoever.

Jacob decided to sign up for the service and give it a test – since then we have both been progressively migrating our files and databases over to the new server and making changes to the DNS records. Finally sprayfly.com, inchina.co.uk and radotages.co.uk (i.e. all my sites) are now hosted on Media Temple and I will shortly be cancelling my Heart Internet account for good.

So far, I have encountered very few problems during the migration and am impressed with the speed of the new servers. The Media Temple control panel is very smooth and easy to use and some of the new features that weren’t provided by Heart Internet have already come in handy. I have been using an SSH shell to transfer my files between servers (using rsync) and also used the shell for database dumps and imports which has made the transfer a breeze. It is of course possible that there are a few hiccups that I’ve not spotted – so if you notice anything unusual then please let me know (leave a comment).

To think that I have been forking out so much cash to Heart Internet each month when all along I could have got more for less is quite frustrating – but at least I’ve made the move now…