Wednesday, November 24, 2010

Ubuntu desktop as a nas server

I had a spare Desktop in my basement with Ubuntu installed on it. So had, in past added a spare hard drive to it.So I decided to use it as my own Nas server. Use it to back up pictures, mp3s etc.

Here's a list of steps that I had to follow in order to finally set it up:


Step 1: Upgraded ubuntu to 10.4.

Step 2: By default ubuntu will be configured with DHCP. That's no good. Got to fix he ip address so the network drive mounts do not break everytime the server starts up.
  • The easiest way to do this is to edit /etc/network/interfaces (don't mess with the gui it was kinda flaky).
    --------------------------------
    auto eth0
    iface eth0 inet static
            address 192.168.1.100
            netmask 255.255.255.0
            network 192.168.1.0
            broadcast 192.168.1.255
            gateway 192.168.1.1
    -----------------------------------
    • edit /etc/resolv.conf. Add your DNS name servers here. Or just add 8.8.8.8 ( gooogle's free dns service)
    • now just restart networking: /etc/init.d/networking restart ( or sudo service networking restart)
    • >ifconfig should now tell you that the ipaddress has changed. ping google.com to make sure you are up and running. 
    Step 3:  Install samba.
    • The best resource that I found was this : http://ubuntuforums.org/showthread.php?t=202605.
    • Note that in the link above the setting "guest = ok". This will allow any user to access your samba share. It should be set to no.
    • Make sure that you set "security = user"  and "users = winuser, otheruser" . This will restrict the access to users who have password.
    Step 4: Map network drive in a windows machine.
    • You might have to change the windows firewall setting for it to be able to connect to your shared network via smb.
    • Use map network drive option in Windows explorer to map your machine. Use the following for folder:
    //192.168.1.100/media/
    • Click on the "Connect using a different user name". link. Enter usename and password.

    Tuesday, November 23, 2010

    Date in java difference anyone ?

    Turns out it not simple function call to get the difference between two dates and display the result is a nicely formatted output. I ended up doing this (With liberal help from google )



    import java.util.*;
    import java.text.*;


    public class Test1 {

    private static String getDateDifference(String format, Date d1, Date d2 ){
    long dl1 = d1.getTime();
    long dl2 = d2.getTime();

    long sec2Millisec = 1000;
    long minute2Millisec = sec2Millisec*60;
    long hour2Millisec = minute2Millisec*60;
    long day2Millisec = hour2Millisec*24;

    long diff = (dl2-dl1);
    long days = (diff)/(day2Millisec);
    diff = diff%day2Millisec;
    long hours = (diff)/(hour2Millisec);
    diff = diff%hour2Millisec;
    long mins = (diff)/(minute2Millisec);
    diff = diff%minute2Millisec;
    long secs = diff/sec2Millisec ;
    return String.format( format, days, hours, mins, secs);
    }

    public static void main(String[] args){

    try {
    Date d1 = new Date();
    Date d2 = new Date(d1.getTime() + 1113660*1000);
    System.out.println (Test1.getDateDifference(" %d days, %d hours, %d mins, %d secs", d1, d2));

    }catch (Exception e){
    System.out.println("err = " + e);
    e.printStackTrace();
    }

    }
    }