Mac远程桌面到Linux服务器

假期结束,回到学校开始干活 :]

为了远程使用Linux服务器,折腾了一个下午。最终看来还是用vnc最简单了。

实验室有两台强劲的Linux服务器用来做研究。之前我一直都是用ssh登到服务器上去码代码,反应速度很快,感觉很不错。但是因为在做机器视觉,难免需要看远程的图片。命令行虽然快,总不能每次都把图片拷贝到本地再看,有时候需要可视化中间结果,ssh也行不通。

当本地机器是Linux系统的时候比较好办。可以用ssh加X forwarding的方法。在本地开一个X,然后把远程服务器的X指令通过ssh转发到本地的X,码代码没有什么延迟,感觉还是很不错的。

sudo X :11 vt11 2>&1 >/dev/null &

这样可以在本地新开一个X,Ubuntu下用Ctrl+Alt+F11可以切到第11个虚拟终端

回到之前的终端,开ssh和xterm

xterm -display :11 -e ssh -X server-host &

然后可以切换到第11个虚拟终端来使用远程Linux服务器上的X了。

这样虽然好,但是要求本地机器上有安装X。在Windows和MacOS下虽然有解决方法,但是比较麻烦。

用VNC的话就没有这个问题,毕竟VNC的客户端是很容易找的。

当然需要先ssh登录到Linux服务器上安装vncserver

sudo apt-get install vnc4server

然后启动vncserver

vncserver

这样就搞定了。

在本地的Mac下可以用自带的Screen Sharing App或者著名的Chicken of the VNC连接到server-host:5901来查看和控制远程Linux桌面。

在服务器上启动了vncserver之后,可以通过修改

~/.vnc/xstartup

这个文件,来指定远程的X启动之后要执行什么命令。我喜欢用openbox,所以我的xstartup文件就是这样子

#!/bin/sh

# Uncomment the following two lines for normal desktop:
# unset SESSION_MANAGER
# exec /etc/X11/xinit/xinitrc

[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
openbox-session&
#x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#x-window-manager &

另外,在启动vncserver的时候可以使用参数修改远程桌面的分辨率

vncserver  -geometry 1280x1024

这么一来就可以在命令行下写代码,同时再开一个vnc窗口看图,感觉不错!

这篇文章有列举一些远程到Linux服务器上的其它解决方法,如果有服务器的root用户权限的话,freeNX貌似也是一个不错的选项。

Check out your svn copy through ssh without those annoying password prompts

This is an instruction I wrote to help people in our lab to access codebase on our server. I have removed all the specific information.

Since setting up something on Windows is always such a challenging work, help this one helps.

The background is that, on the server side, you are using ssh to protect your codebase. Instead of setting up another group of users and passwords for the svn repository separately, the server asks user for one’s Windows credential. So, on the client side, user are using commands in the form of “svn co svn+ssh://path/to/repository” to checkout a copy. Please note that the scheme for the url of repository is “svn+ssh” instead of “svn”. It tells the svn to connect to the server by using ssh then checkout the source code.

I may post a tutorial on how to setup the server ON WINDOWS in the future.

Please follow this instruction to check out a sample repository.

1. Install SVN Client “TortoiseSVN”
Open http://tortoisesvn.net/downloads.html in your web-browser, choose “TortoiseSVN 64-Bit”/”TortoiseSVN 32-Bit” to download the installation program. Open it and keep clicking “next” to finish the installation.

2. Have a try
Open your windows explorer, get in a folder such as “C:\Repo”, right click on anywhere blank, choose “SVN Checkout…”.
The URL of repository is “svn+ssh://path/to/your/repository”.
Click “Ok”.
You will be prompted for your username and password for THREE times then you should get the files.

3. I can’t stand this!
Yes it’s quite annoying to repeat filling your credential, remove the folder and we will try to get rid of this.

4. Utilities
If you are on an unix-based machine, google for “ssh key” you get A LOT OF tutorials on this topic, for example http://linuxproblem.org/art_9.html.
If you are on WINDOWS, open this PuTTY Download Page http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
You will need PuTTY.exe, Pageant.exe and PuTTYgen.exe

5. Generate Key Pairs
Launch PUTTY.exe, fill in the Host Name as “Your.Repository.Host.Name”, click “Open”.
Input your credential, note that when you type in your password there will be “no response”, it is ok, just go ahead.
You should meet our Hippo now.
Type in “ssh-keygen.exe”
Keep hitting the “Enter” button.
Type in “cd ~/.ssh”
Type in “mv id_rsa.pub authorized_keys”
Type in “ls” and you should see this:
“authorized_keys id_rsa”
id_rsa is your private key.
Type in “cat id_rsa”
Scroll up and highlight/select lines from “—–BEGIN RSA PRIVATE KEY—–” to “—–END RSA PRIVATE KEY—–“, including these two lines.
Hit “Ctrl+C” to copy them.

6. Make your private key file
Create a new text file in certain folder, e.g. “My Documents”, “Ctrl+V” to paste lines in this file, save it in a name e.g. “ssh_key.txt”.
Launch PUTTYgen.exe, choose “load”, in the dialog change the extension to “All Files” and find your “ssh_key.txt”, click “open”.
Click “Ok” when anything pop out.
Choose “Save Private Key”, get a name for it such as “ssh_key.ppk” and click “save”.

7. Come on, we are almost there
Launch Pageant.exe, nothing happen? Right, it hides in the task bar. Right click on its icon and choose “Add Key”.
Find your “ssh_key.ppk” and choose “Open”.

8. Have another try
Ok, Repeat Step 2, you should be able to get all the files without entering any credential.
Cheers!

让iOS使用SSH Tunnel

为了突破局域网的限制,目前主流的方法还是SSH和VPN。
如果使用VPN的话,可以很方便的在自己的iPhone/iTouch/iPad设备上连VPN,上外网。
肯定还有不少朋友是在用SSH的。在自己的机器上一般使用以下命令来连接ssh,并且绑定到127.0.0.1:8080。

$ssh -D 8080 username@sshserver.com

之后,我们可以在设置proxy的地方指定proxy为127.0.0.1:8080。

但是在iOS上,如果不越狱的话是不能用SSH的。这样,我们必须让ssh绑定到外部IP,并且让iOS知道这个proxy。

假设本机的IP为192.168.1.2

步骤如下:

1. 不能再用上面这个命令来使用SSH了,命令如下

$ssh -g -D 192.168.1.2:8080 username@sshserver.com

这让SSH把连接转发到192.168.1.2:8080这个地址,-g让外部机器可以连到这个地址

2. 我们需要一个PAC文件。这是一个指定如何配置proxy的文件。
新建文件proxy.pac
内容如下

function FindProxyForURL(url, host)
{
    return "SOCKS 192.168.1.2:8080";
}

3. 我们还需要让本机之外的其他机器可以访问到这个pac文件。比如,我们这里用HTTP来做。
Mac本身就支持Web Sharing,其实是一个Apache的Server。
在”System Preferences”->“Sharing”里,给Web Sharing打上勾.


把上面的proxy.pac文件拷贝到~/Sites/下面,这时,我们可以在浏览器里试试这个地址
http://192.168.1.2/~username/proxy.pac
看看是不是能够看到文件内容?

在lion下,可能会出现没办法勾选Web Sharing的情况。把httpd.conf用原始版本覆盖即可

$sudo cp /etc/apache2/original/httpd.conf /etc/apache2/httpd.conf

4.万事俱备,现在只要在iOS上设置就行了。
通过”Settings”->”WLAN Networks”找到现在连上的Wifi,查看details(蓝色箭头)
在最下面有设置http proxy的地方。
选择”Auto”, 填入 http://192.168.1.2/~username/proxy.pac

5.搞定!

试着上上外网看吧 :]

参考: