Jun 29, 2013

Codeigniter on Alternate Port

Rather than using sub path, I deploy my web on alternate port. But this is also not normally supported in Codeigniter. So here's how I do it.

Add this in /.htaccess:
RewriteCond %{SERVER_NAME} localhost
RewriteCond %{SERVER_PORT} 80
RewriteRule ^%{SERVER_NAME} %{SERVER_NAME}:8000?/$1 [R,NC]

And this as site name:

Now it works just as on real domain and real port, http port 80 that is.

Jun 15, 2013

SSH Login Without Password

I forgot to put a section about login to an ssh account without password for my previous entry about VNC Gateway. The VNC scripts should have been working without password entry. Here is how to do it.

First, make a public key for local account, just follow the simple instructions:

$ ssh-keygen

Then add the public key to remote account list of authorized accounts:

$ cat ~/.ssh/id_rsa.pub | ssh remote_user@remote_system 'cat >> ~/.ssh/authorized_keys'

Now try to login using ssh, it should not ask for password anymore:

$ ssh remote_user@remote_system

May 20, 2013

Ubuntu VNC Gateway

I just have my VPS activated. I spent days wondering what to do with it. Then it hits me.
"Why not use the VPS as remote desktop gateway?"
I have spent more days working out how to do it. This is the note to work it out.

Some requirement:
  • A Virtual Private Server for the gateway
  • A Desktop/Laptop PC for the VNC server
  • Ubuntu Linux 12.04 for both server and PC (specifically in this publish)
  • OpenSSH installation on both gateway and server
  • X11VNC installation on VNC server PC
  • Another system as the VNC client which supports SSH tunnel

  • You want to access your PC from any network connected to the Internet but cannot have the router in the network forward connection to your PC
  • You have a VPS lying around in the net
  • Your VPS running under OpenVZ with tun/tap disabled by host and you don't want to or cannot have it enabled
  • You want the VNC service to automatically running every time the server boots
  • You want the VNC service to be available through gateway every time the VNC server connected

Most Linux system already have ssh installed. So let's just go ahead to setup x11vnc and ssh on the VNC server.

$ sudo apt-get install x11vnc

Next we setup so x11vnc runs at boot. By default, x11vnc is not available as service. So we are going to make an init configuration script for it. But before that, we are going to make a VNC password as an added security.

$ sudo x11vnc -storepasswd /etc/x11vnc.pass

Follow the instruction an enter the new password.

Next we write the configuration script. You can use any modest text editor

description "X11 VNC"

start on login-session-start

    x11vnc -xkb -noxrecord -noxfixes -noxdamage -display :0 -auth /var/run/lightdm/root/:0 -forever -o /var/log/x11vnc.log -rfbauth /etc/x11vnc.pass -rfbport 5900 -bg
end script

Note that /var/run/lightdm/root/:0 is the Xauth file path for lightdm. Xauth is a X session authentication file. Find out the path of Xauth for your desktop manager. The path must be available both before and after login. and accessible for user after login. A log file will be handy if something is not working. I use /var/log/x11vnc.log for the log path. Point x11vnc to the stored password previously created in /etc/x11vnc.pass.

At this point you can restart the server machine to test the script. You can do ps aux | grep x11vnc to see if it is running or not after boot. It should be accessible from local network.

Now we make the network scripts to make ssh remote listening run every time the server connected. The ssh set to timeout after 5 minutes so it must be looped.


sleep 3
while true
    ssh -fyTn -R '5900:localhost:5900' 'root@gateway.com 'sleep 300'
    sleep 300

The -R meaning is to direct every request to port 5900 to port 5900 on vnc server local interface on gateway.com. Run sleep for 300 seconds on gateway.com then close. The next sleep 300 is to wait until it is time to reconnect.

Make the file executable.

chmod 0500 /root/bin/listen_for_x11vnc_on_gateway.sh


case "$IFACE" in
  lo) exit 0;;

case "$MODE" in


    case "$ADDRFAM" in
      inet|inet6|NetworkManager) : ;;
      *) exit 0 ;;
    case "$PHASE" in
        exec /root/bin/listen_for_x11vnc_on_gateway.sh &

    killall listen_for_x11vnc_on_gateway.sh

exit 0

Make the file executable then make links to it on /etc/network.

sudo chmod 0500 /root/if-updown/x11vnc-gateway
ln -s /root/if-updown/x11vnc-gateway /etc/network/if-up.d/
ln -s /root/if-updown/x11vnc-gateway /etc/network/if-post-down.d/

It is going to ignore loopback, and other connection other then inet, inet6 (IPv6), and NetworkManager. Go ahead and connect to the Internet. You can check using ps aux | grep ssh to make sure that it is running.

To connect to the server through the gateway, set your VNC client to connect to port 5900 on gateway.com through ssh port 22 on gateway.com. We don't need to configure the routers on both networks.

The ssh scripts must run without entering passwords. See my blog here to read on how to do it.

I am sure that the scripts are really ugly. So if there is a Linux guru out there, please don't hesitate to make suggestions.

Mar 14, 2013

Android Emulator Using QEMU-KVM

I am assembling the scattered notes and solutions for running Android Emulator using QEMU-KVM. Here is the official from Android Developer Website.

First install the QEMU-KVM on your system properly. I am using Ubuntu.

Noticed the parameter to add before running the emulator:

-qemu -m 512 -enable-kvm

For me the emulator just won't show, or randomly does, or start slow. Then I add -clock ? parameter to choose timer alarm method. Now the emulator shows up immediately.

First find out what timer alarm method to choose with:

$ qemu-system-x86_64 -clock ?

You'll get something like:

Available alarm timers, in order of precedence:

I choose dynticks so the parameter for running the emulator is now:

-qemu -clock dynticks -m 512 -enable-kvm

To start the emulator scaled, add the scale ratio:

-scale 0.57 -qemu -clock dynticks -m 512 -enable-kvm

Noticed that the emulator still boots slow. But at the rate of the real device. To save time you can tick the Snapshot option for the emulator image. You can't use GPU support if you do that, you have to choose either one.

Add the -snapshot parameter using default-boot as value.

-snapshot default-boot

About the GPU support, if ran into warning messages about openGL, use the libs in the emulator binary folder. Insert before the rest of the LD_LIBRARY_PATH environment variable. Use it like this:

env LD_LIBRARY_PATH="$HOME/opt/adt-bundle-linux-x86_64/sdk/tools/lib:$LD_LIBRARY_PATH" $HOME/opt/adt-bundle-linux-x86_64/sdk/tools/emulator64-x86 -avd Tab_7_Snapshot -netspeed full -netdelay none -scale 0.57 -snapshot default-boot -qemu -clock dynticks -m 512 -enable-kvm

I am getting garbage message nativeGetEnabledTags. To clear it from Logcat, I use this message filter:


... and put it in Saved Filter.

If you have sounds from the emulator but looses sounds from your desktop, it's because QEMU takes over all alsa, not going through dmix. Add these to your environment variables:


You can put those in /etc/profile.d/ or you can just add it when running the emulator from command line.

That's it! :-)

Edit 5/9/2015:

This is how I start the emulator now. AVD named "high" using Android 5.1.1 x86, and ANDROID_SDK_ROOT set to SDK path. Remember to add  "$ANDROID_SDK_ROOT/tools" to PATH. Don't set LD_LIBRARY_PATH in .bashrc, you only want the library for simulator. It's on Intel Celeron with vt-d:

env LD_LIBRARY_PATH="$ANDROID_SDK_ROOT/tools/lib64:$LD_LIBRARY_PATH" emulator -avd high -scale 0.51

Edit 6/20/2015:

For running on Mac, follow the instructions at Android Developer. Run the .dmg file mentioned there for Mac.

Cordova WebSQL Path

The SQLite database file for Cordova WebSQL resides in: /data/data/<app.package.name>/app_webview/databases/file__0/1