VNC useful commands

Below is a list of vnc commands I often use to manage vnc sessions on my ubuntu linux box.

# start a session
vncserver -geometry 1600x1200 -depth 24 -alwaysshared

# specific display numbers
## sublime ##
vncserver -geometry 1600x1000 -depth 24 -alwaysshared :200
## eclipse ##
vncserver -geometry 1600x1000 -depth 24 -alwaysshared :300

# see what sessions are active
ls ~/.vnc/*.pid

# kill a session
vncserver -kill :[session_num]

# help
vncserver -h

Here is more online references for more options you can set.
http://www.realvnc.com/products/open/4.1/man/Xvnc.html

For osx (as my client box), I use Chicken of the VNC. It gets the job done. 😉

Setting up a VPN server on a Mountain Lion Server

This is a guide to set up a simple L2TP VPN connection.

My mac mini server has mountain lion osx v. 10.8.2. I have also signed up for a “domain” at http://www.no-ip.com/. You may want to make sure you can ssh to your server first from the outside network before moving forward.

I have my server behind a linksys router so I need to set up the router first. Bring up the router page, mine is at 192.168.1.1. Then click Security -> Firewall. Then uncheck the Block anonymous Internet Request. Yea, sounds scary isn’t it? Then click Save Settings, the router blacks out, then comes back.

osx_vpn_firewall1

Next, click Applications & Gaming -> Single Port Forward. This is where you put in all the port forwarding info for our VPN service. The port you will need to forward are: 500, 1701, 4500, and 50. They will need to get forwarded to your server IP.

osx_vpn_firewall2

Then open up your Server Application. Go to the VPN window. Pick L2TP. The host name is how you refer to your server in your local network, could be anything. The Shared Secret is important, and you will be handing this out to your users later. After filling out all these, switch on the VPN server on the top right.

osx_vpn_service_setup2

Your VPN server is now setup.

Now on the client side. Open up System Preferences, then click Network. Create a VPN, L2TP over IPSec connection profile. Then start filling in the fields. The Service Address is, for example, the one I get from no-ip.org. The Account Name is a valid username on your server.

osx_vpn_client1

Then click on the Authentication Settings… button. This is where you put in the user’s password, and the Shared Secret that we put in earlier on the server. When done, click ok.

osx_vpn_client2

Then click the Connect button. Cross your fingers…

Hopefully you will see the green light for your VPN connection! If you want to double check whether you are on your private network, you can try ssh to one of your machines in the 192.168.1.x address range and see how it goes.

osx_vpn_client3

Happy VPN’ing!

splitting and joining huge files

Today I wanted to sftp a 10Gb file over the wire. Knowing that the transfer would fail in the middle of it, I decided to split this file into small pieces beforehand. Below I think is the easiest way on osx.

// to split my huge file into 100mb files, having the prefix "bak_"
split -b 100m my_huge_file.ext bak_

// to re-construct my huge file
cat `ls bak_*` > my_huge_file2.ext

After running the split command, you will see some files named bak_aa, bak_ab, etc in your directory. These are the chucks of your original huge file. So just transfer these small bak_* files over the wire.

sftp doesn’t have mput… sigh…

Also the original file name is gone missing in the process… sigh… That’s unfortunate. What I decided to do is to write up a small readme file to store the above two commands, so later on I know what output filename to use when running the second command.

DNS flush

Sometimes you may need to do a DNS flush, say after a mysterious DNS network issue. Here are the commands.

Mountain Lion / Lion
sudo killall -HUP mDNSResponder

Leopard
sudo dscacheutil -flushcache

Tiger
lookupd -flushcache

Windows
ipconfig /flushdns

Run a nslookup or ping to check if you are indeed getting the correct IP(s) from the DNS server(s).

Installing mysql on osx

I often need to do this so I figure I will write it down to remind myself later.

First, download the mysql installation dmg file (version 5.5.20 as of now). osx already comes with mysql but I like the extra System Pref icon after the manual install. You can download at http://dev.mysql.com/downloads/mysql/. Pick Mac OS X 10.6 (x86, 64-bit), DMG Archive. Once you have the dmg file, open and install all three files (mysql-5.5.20-osx10.6-x86_64.pkg, MySQL.prefPane, MySQLStartupItem.pkg) in there.

Then in your ~/.bash_rc file, put in the following to save typing.

alias mysql=/usr/local/mysql-5.5.20-osx10.6-x86_64/bin/mysql
alias mysqladmin=/usr/local/mysql-5.5.20-osx10.6-x86_64/bin/mysqladmin

You can surely add /usr/local/mysql-5.5.20-osx10.6-x86_64/bin/ to the $PATH variable in your ~/.bash_profile file. Yea, whatever works.

PATH=/usr/local/mysql-5.5.20-osx10.6-x86_64/bin/:$PATH

Then turn on your MySQL daemon. An easy way is to open up System Preferences, then find the MySQL icon, open it. Then start the server and check the auto-start option below. If everything goes well, you will see the green running text.

Then set the root password for this database. The following set the root password to my_secret_password

$ mysqladmin -u root password my_secret_password

To change your root password, say from my_secret_password to abc123, do the following.

$ mysqladmin -u root -p 'my_secret_password' password 'abc123'

Alright, mysql is now up and running!

Bonus: To make your life a little easier, download Sequel Pro. It’s a free osx mysql gui.

Installing memcache on osx for php

Finally got memcache working!

I am compiling the steps here in case I need to do it again.

First install libevent. This is a dependency to memcached, so need to get it.

cd /tmp
curl -OL https://github.com/downloads/libevent/libevent/libevent-2.0.17-stable.tar.gz
tar -xvzf libevent-2.0.17-stable.tar.gz
cd libevent-2.0.17-stable*
./configure
make
sudo make install

Then install memcached.

# Compile memcached utility
cd /tmp
curl -O http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
tar -xvzf memcached-1.4.13.tar.gz
cd memcached-1.4.13*
./configure
make 
sudo make install

At this point, if everything goes well, the memcache daemon should be ready to run. You can try the following to see if memcached returns anything to you.

memcached -d -m 24 -p 11211
telnet localhost 11211
stats
quit

When you run memcached -d -m 24 -p 11211, you are assigning 24Mb ram for memcache to use, and using the port 11211, which is the default port for memcache. The -d runs memcache as a daemon.

After you run stats, you should see some stats returned on your screen. If so, that means memcache is running fine now.

Next step is to make sure php can talk to memcache.

Download the php extension to memcached from this link: http://pecl.php.net/package/memcache. I recommend getting the stable version, 2.2.6 as of June 2012.

After uncompressing it, do phpize. If you get an error on not having autoconf, install it with brew. See my other tutorial on how to do that.

gzip -d < memcache-2.2.6.tgz | tar -xvf -
cd memcache-2.2.6
phpize

After phpize gives you your php version info, do the usual compile and install:

./configure
make
sudo make install

Double check that the memcache.so file is in your php include directory.

ls /usr/lib/php/extensions/no-debug-non-zts-20090626/

It should be there… if not, you can manually copy the file yourself. It’s located under the “modules” folder.

Now, modify your /etc/php.ini file to include this extension.

extension = memcache.so

Then finally, restart apache.

sudo apachectl restart

If everything goes well, your phpinfo() should give you a section on memcached, indicating memcached is loaded properly.

Congratulation! At this point php is ready to interact with memcache. But just how to do that in code? Let’s wait for my part 2 of this tutorial. 😉

Ref link:
http://readystate4.com/2012/03/15/installing-memcached-on-os-x-10-7-3-lion-on-mamp/
http://www.glenscott.co.uk/blog/2009/08/30/install-memcached-php-extension-on-os-x-snow-leopard/
http://jamiecurle.co.uk/blog/memcached-on-osx-without-macports/

“You have new mail” on osx

Sometimes when I open up a new terminal I see the following, notifying me there are new mails in my system mail box.

Last login: Tue Feb 28 18:57:43 on ttys006
You have new mail.

Just how to read those emails?

You can use “mailx”.

my_osx_box:~ birdchan$ mailx

This is a very linux like mail program, so don’t expect fancy controls. Below are some commands that are enough to get you through.

  • h: lists the current emails in your mailbox, notice there is an ID before each email.
  • NUMBER: Just type in the email ID and press enter, you will enter the reading mode. Then ENTER gets you to the next line, SPACE next page. Just like in MORE.
  • d <num>: deletes the corresponding email.
  • ?: Shows you all the commands

installing osx from an external hard drive

For whatever reasons if you need to install osx from an external hard drive, here are the steps I have tried and verified. In my case I installed snow leopard onto a mac mini from an external usb hard drive.

First of all, go get the snow leopard image file in iso or dmg.

Then we will need to prepare for your external hard drive. We are going to erase its content so please make a backup first.

  1. Open up Disk Utility, select your external hard drive.
  2. Choose the Partition tab, then pick two partitions. For the first one, click Options then pick Guid Partition Table to make it bootable. Pick Mac OS Extended format. Then set it to 8Gb for your CD image. Name it “Snow_Leopard_Drive”.
  3. For the second partition, pick Free Space.
  4. Click OK to commit the setup.
  5. Now, click on the Snow_Leopard_Drive, the choose the Restore tab.
  6. Double click on your snow leopard CD image, a white drive icon should appear on your desktop. Drag that icon onto the Source field.
  7. Drag your Snow_Leopard_Drive icon onto the Destination field.
  8. Click OK to proceed.
  9. Put in your root password, then the CD image will be burned onto your Snow_Leopard_Drive.

osx random freezes

I have had my macbook pro 13-inch for about 2 years now. It’s been running very well until recently, it randomly freezes. The keyboard and mouse would not respond and I would be forced to do a hard restart. After a few incidences, I noticed that this often happens when I am watching a youtube video or any long videos on VLC. At first I thought it was some corrupt libraries in the kernel and so I re-installed osx, but the problem remains. Then I clicked around, trying to investigate… to my horror, it was actually that my cpu fan stopped spinning!

I have attached a picture here. This software is called “iStat” btw. The cpu fan is spinning at 2000 rpm, which is good. This is after I replaced the fan. Before the replacement, it was at 0 rpm. The cpu temperature was going up to 90 degrees in fahrenheit.

I did try to clean up my old fan, but it still didn’t spin. See pic below. So it could be that it just died. Well too bad, I got another fan on ebay for about $15.

Replacing the cpu fan is fairly simple. All it takes is to unscrew the bottom screws from the case, then 3 more screws to remove the fan. Be careful with the small socket on the motherboard, coz it’s very easy to drag that along with the fan cable/plug. I recommend to use something small enough to press on the small outer socket, then use a needle tip to lift up the plug. After the removal, just reverse the process to install the new fan.

Now my macbook pro is working perfectly fine again! My cpu temperature is now at 45 degrees.

To limit bandwidth in osx

There may come a time when you want to limit your http download bandwidth coz it’s taking up all your bandwidth. I came across two apps that try to accomplish this task but I am afraid to say they are too hard to understand. So let’s just do this in the terminal.

1. First, create a pipe/rule that allows 100KB/s
sudo ipfw pipe 1 config bw 100KByte/s

2. Then assign that pipe to port 80 which is the remote web server’s http port #. It’s src-port coz the data is coming into your computer.
sudo ipfw add 1 pipe 1 src-port 80

3. At this point, all your http download bandwidth will max out at 100KByte/s. You could then do other things like ssh’ing to your work place or whatever.

4. To clear the bandwidth limit, do the following to remove pipe 1.
sudo ipfw delete 1

All these can be done on the fly. No reboot nonsense.

For more info, google for “ipfw bandwidth throttle” or do a “man ipfw”.