2013年1月21日 星期一

常用的 ssh 轉 port 方式

 記錄常用的幾種 SSH 通道與轉 port 的使用
  • Tunneling VNC Connections Over SSH 
一般 vnc 協定並無資料加密,建議使用有加密方式,或將通道以加密方式包裹。
下面例子則是以 ssh tunnel 方式,讓 vnc server 的兩端,以加密的通道 (SSH) 進行溝通。此方式的條件是 vnc server 機器上也需要有 ssh daemon 。
場景:VNC server(vncserv.host) 上的 vnc posrt 為 5901 (若一般 vnc viewer 則為vncserv.host:1)
    • 在 local (控制端)執行指令並以建立 SSH 通道
$ ssh -L 5901:localhost:5901 -N -f -l user vncserv.host
    • 在 local 端起動一般 vnc viewer 程式,但連線位置是指為 localhost:1
$ vncviewer localhost:1
  • Tunneling Transmission remote deamon Connections Over SSH
 Transmission remote通常以 web: 在localhost:9091進行管理, 但預設並無以 SSL 登入帳號認証方式,故透過 SSH tunnel 是個較安全的方式
    • 在 local (控制端)執行指令並以建立 SSH 通道
$ ssh -L 9191:localhost:9091 -N -f -l user my.transmission.host

建立後則可用下列方式進行 web 連線

http://localhost:9191/
  • Reverse SSH tunnel SSH connection to a NAT'ed box
反向的 SSH 連線上。例如,要 ssh 到 NAT 內的機器,除非在 NAT 上設定,不然無法 ssh 連通至內部 ssh service。透過 reverse SSH tunnel connection 則可。
    •  先在 NAT 內的機器對外(public.host) 建立連線
$ ssh -R [port00]:localhost:[port01] -N -f -p [port] user@public.host


port00 : 在 public.host 上想開啟的 port
port01 : 在 NAT 這台機器中 (localhost) 自己 ssh daemon 的 port
    • 在外部的機器(public.host)則透過自己的 port00 進行與 NAT 中的機器連線
$ ssh -p [port00] user-id@localhost


Note:
  • -L 5901:localhost:5901 : Specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side. Here you are using port 5901 on the localhost to be forward to sshserver.mydomain.com on the 5901 port.
  • -N : Do not execute a remote command i.e. just forward ports.
  • -f : Requests ssh to go to background just before command execution. Requests ssh to go to background just before command execution. Once password supplied it will go to background and you can use prompt for type commands on local system.

Reference:
http://www.cyberciti.biz/tips/tunneling-vnc-connections-over-ssh-howto.html
http://www.positon.org/post/Tunnel-SSH-inverse-pour-connexion-SSH-vers-une-machine-NAT%C3%A9e
http://www.study-area.org/tips/ssh_tips.htm

2013年1月3日 星期四

Linux 下如何在 VM 中連接 socket output

在 Ubuntu 下以 VirtualBox 為例,搭配  socat 套件使用 Fedora 17 的 serial console mode
  • 先確認系統中有 socat 套件。可用 sudo apt-get install socat 安裝
  • 『設定值』-> 『序列埠』: 啟用
  • 『連接埠號』選: COM1 (需對應後面的 boot parameter, COM1 為 ttyS0, COM2 為 ttyS1 , 類推)
  • 連接埠模式:選「主機管線」;不勾選「連接到現有設備/通訊端」(ps: v5.x 之前,需勾選『建立管線』);設定連接埠路徑 : /tmp/serial0 。設定如右。
  •  啟動 VM,此時 /tmp/serial0  這 socket 應該被建立
  • 在 host machine ,終端機下 socat unix-connect:/tmp/serial0 stdio,echo=0,raw ,會等待輸出
  • 在 guest machine 中,在開機參數中加入 console=ttyS0,115200n81 ( 或 38400,76800 , 若是用 Debian live-boot and live-config 的 live system , 則需再加 live-getty )
  • boot 並從 host 終端機使用 serial console



Ceasar's Photo

www.flickr.com