Friday, June 22, 2012

My customizations to an Ubuntu Server 12.04 LTS

I have been toying with the idea of setting up an Ubuntu Server to run my media server at home.  Each time I go down this route there are several tweaks I do until I am happy.  The following is a list of the things I have to search the web to find most often.
I have also found some awesome suggestions for tools for xfce at their wiki.
Table of Contents
  1. Screen Resolution
  2. Console Font
  3. Set line numbers to always appear in vi
  4. Change command prompt to include colour
  5. Some additional aliases
  6. cp with a wget style progress bar
  7. Lightweight xfce4 gui
  8. phpMyAdmin for the mysql db
  9. phpPgAdmin for the PostgreSQL db
  10. Install Oracle JDK 6
  11. Webmin

  1. Screen resolution.
  2. This is simply done by editing the /etc/defaults/grub file like so
    sudo vi /etc/defaults/grub

    find the line that says GRUB_CMDLINE_LINUX="" and change it to look as follows
    GRUB_CMDLINE_LINUX="vga=773"

    save the file and REMEMBER to run
    sudo update-grub

    This will give you a console resolution of 1024x768
    You can chose other resolutions based on the following table

    VGA Resolution Codes for GRUB & Lilo<
    --- Depth --

    Colorsbits640x480800×6001024×7681152×8641280×10241600×1200
    2568vga=769vga=771vga=773vga=353vga=775vga=796
    32000?vga=784vga=787vga=790vga= ? vga=793vga= ?
    6500016vga=785vga=788vga=791vga=355vga=794vga=798
    16.7M24vga=786vga=789vga=792vga=795vga=799vga= ?

    All you need do then is reboot and you should see your server with the new resolution.

  3. Console Font
  4. Still trying to find a permanent solution to this - The closest I have come is here but this setting gets lost after a reboot.

  5. Set line numbers to always appear in vi
  6. In you home directory create a file called .vimrc and add the following line to it
    set number

    Save the file and the next time you open vi each line will automatically be numbered.

  7. Change command prompt to include colour
  8. I use this in case I login as root or if I have su'ed to root so I won't forget who I am running as at the moment. If I am running as root I want my user to appear in bright red otherwise in bright green. I also want the directories to be displayed after the username in dull green and if I am root in dull red.
    For my normal user (The green one) I edit my .bashrc file in my HOME dir
    vi ~/.bashrc

    and add the following at the end of it.
    # Set prompt: " username@hostname/directory/tree $ " (with colors)
    export PS1="\[\e[32;1m\]\u\[\e[0m\]\[\e[32m\]@\h\[\e[36m\]\w \[\e[33m\]\$ \[\e[0m\]"

    For the root user I add the following to the end of roots .bashrc
    # Set prompt: " username@hostname/directory/tree $ " (with colors)
    export PS1="\[\e[31;1m\]\u\[\e[0m\]\[\e[31m\]@\h\[\e[31m\]\w \[\e[33m\]\$ \[\e[0m\]"


  9. Some additional aliases
  10. I have some other aliases that I make use of also that I add to the end of my .bashrc file
    alias ver="cat /etc/lsb-release"
    alias free="free -m"
    alias update="sudo apt-get update"
    alias install="sudo apt-get install"
    alias upgrade="sudo apt-get upgrade"
    alias remove="sudo apt-get remove"


  11. cp with a wget style progress bar
  12. When I am copying files I like to see the speed and progress of the copy. To do this I create a file in my home dir called cppv which look as follows

    #!/bin/bash

    set -e

    if [ $# -lt 2 ]; then
    echo "cppv - copy files with progress bar and rate limiting ability"
    echo "Usage: cppv source_file[s] destination_file_or_directory"
    echo "No other non-positional command line arguments can be given"
    echo "Always recurses like find"
    echo "You can change copying speed limit on the fly with \"pv -R\" if you find out pv's PID"
    echo "Use FIND_OPTS, PV_OPTS, CPIO_O_OPTS, CPIO_I_OPTS to override arguments to the pipeline parts"
    echo "Examples:"
    echo " cppv a b # Copy file a to b. Just calls \"pv a > b\""
    echo " cppv a d/ # Copy file a to d/a. Calls \"find a | cpio -o | pv | (cd d && cpio -i)\""
    echo " cppv *{01..26}*.mkv /mnt/usb/ # Copy all matching files to /mnt/usb/."
    echo " cppv dir1 dir2 # duplicate dir1"
    echo " PV_OPTS=\"-L 1M\" cppv . /tmp/ # Limit copying rate to 1M"
    echo " cppv /home/vi/bin /tmp/ # Warning: Copy /home/vi/bin to /tmp/home/vi/bin"
    exit 1
    fi;

    true ${CPIO_O_OPTS:="-H newc -0o"}
    true ${CPIO_I_OPTS:="-0diu --no-absolute-filenames"}
    true ${FIND_OPTS:="-depth -print0"}

    ARGS=( "$@" );

    DEST="${ARGS[$#-1]}"
    unset ARGS[$#-1];

    if [[ ( "${1:0:1}" == "-" && ! -e "$1" ) || ( "${DEST:0:1}" == "-" && ! -e "$DEST" ) ]]; then
    echo "There should not be any command line options. Only file names." >&2
    exit 1;
    fi

    DIRMODE=0

    if [[ $# > 2 || "${DEST:${#DEST}-1:1}" == "/" || -d $DEST ]]; then
    DIRMODE=1
    elif [[ -d "$1" && ! -d "$2" ]]; then
    DIRMODE=1
    mkdir "$DEST";
    DEST=`readlink -f "$DEST"`;
    cd "$1";
    ARGS=(".")
    fi

    if [ $DIRMODE == 0 ]; then
    pv "$1" > "$2" && exit 0;
    fi;

    if [ ! -d "$DEST" ]; then
    echo Not a directory: "$DEST" >&2
    exit 1
    fi

    if [ "${1:0:1}" == "/" ]; then
    echo "Warning: it will do a bit different thing than usual cp" >&2
    echo " For example, copying $1 to $DEST$1, not to $DEST/`basename $1`" >&2
    fi

    SIZE=`du -sb "${ARGS[@]}" | perl -ne '/^(\d+)/ and $q+=$1; END{print $q}'`

    find "${ARGS[@]}" $FIND_OPTS | cpio $CPIO_O_OPTS | pv -s $SIZE $PV_OPTS | (cd "$DEST" && cpio $CPIO_I_OPTS)


    I also create a static link in the /usr/bin dir to this file so I don't have to keep adding the path before the command. The command to create a static link looks like so:
    sudo ln -s ~/cppv /usr/bin/cppv


  13. Lightweight xfce4 gui

  14. From the CLI terminal, we will install a few software packages to make our life easy
    1. Install the Xfce desktop environment (a GUI for using the server) :

    2. sudo apt-get install xfce4 xfce4-terminal xauth xorg

    3. Install the Gnome Display Manager (which includes a GUI login manager) :

    4. sudo apt-get install gdm

      Alternately (what I prefer to do is) install lightdm

      sudo apt-get install lightdm-gtk-greeter

    5. If you find you have some icons missing try this to fix the problem. Also check under
      "Applications Menu->Settings->Appearance" under the icon tab and make sure you are using an icon theme.

    6. sudo apt-get install tango-icon-theme tango-icon-theme-extras gnome-icon-theme

    7. I change the font of my terminal to use "Monospace" size 10
      "Edit->Preferences->Appearance"

    8. I want my terminal window to use more real estate so I edit the "~/.config/Terminal/terminalrc" file and change the MiscDefaultGeometry setting to look as follows.

    9. MiscDefaultGeometry=100x40

    10. OPTIONAL: Install the Synaptic package manager (a GUI for installing software) :

    11. sudo apt-get install synaptic

    12. OPTIONAL: Install the Update manager (a GUI for updating software) :

    13. sudo apt-get install update-manager

    14. OPTIONAL: Install the Gnome System Tools (which includes a GUI for user administration) :

    15. sudo apt-get install gnome-system-tools

    16. Now reboot the server, so that you can login to the Xfce GUI environment :

    17. sudo shutdown -r now

    When the server reboots, there will now be a GUI login screen
    Select the xfce-session, the username and enter the password
    The Xfce desktop environment will be displayed with the menu panel on top and a few icons on the desktop screen

  15. phpMyAdmin for the mysql db

  16. If I have installed mysql I normally install phpMyAdmin to make manipulation and admin of the database easier. This is easily done by following the setup found here. But basically you need to run the following with some configuration.

    sudo apt-get install libapache2-mod-auth-mysql phpmyadmin

  17. phpPgAdmin for the PostgreSQL db

  18. If I have installed PostgreSQL I normally install phpPgAdmin to make manipulation and admin of the database easier.

    The following instructions will get PostgreSQL accessible from a remote server on your lan

    If PostgreSQL is not installed already:

    sudo apt-get install postgresql

    Log into PostgreSQL and create an account and a database for the user:

    sudo -u postgres psql template1
    psql> create user acme with createdb password 'abcd';
    psql> create database acme with owner=acme; psql> \q


    Leave out the createdb option in the CREATE USER statement if you do not want to grant the user the privilege to create databases.

    By default PostgreSQL server does not accept remote connections. To enable remote connections (which is necessary for servers like CS3 but not for servers like CSNS), modify /etc/postgresql/9.1/main/postgresql.conf so:

    listen_addresses = '*'


    And modify /etc/postgresql/9.1/main/pg_hba.conf so that:

    host all all 0.0.0.0/0 md5
    host all all ::0/0 md5


    To install phpPgAdmin:

    sudo apt-get install phppgadmin

    By default phpPgAdmin only allows local access. To allow remote access, edit /etc/phppgadmin/apache.conf so that

    #deny from all
    #allow from 127.0.0.0/255.0.0.0 ::1/128
    allow from all

    Then restart Apache2. After that phpPgAdmin can be accessed at http://host/phpmyadmin/ .

    NOTE:I was looking for the quickest way to manage PostgreSQL I was not testing the security of the admin tool.

  19. Install Oracle JDK 6

  20. I followed this link to install the Oracle JDK6 onto my Ubuntu Server.
    I also add the following to my .bashrc
    # Java environment variables
    export JAVA_HOME=/usr/lib/jvm/java-6-oracle
    export PATH=$PATH:$JAVA_HOME/bin

  21. Webmin

  22. I can't believe I didn't remember to talk about installing Webmin. Firstly you'll need to have some dependancies installed. You can do this by running the following from the command line.
    apt-get install perl libnet-ssleay-perl openssl libauthen-pam-perl libpam-runtime libio-pty-perl apt-show-versions python

    Official instructions can be found here, but basically there are 2 ways to install webmin.
    1. Via APT
    As root edit /etc/apt/sources.list and add the following lines
    deb http://download.webmin.com/download/repository sarge contrib
    deb http://webmin.mirror.somersettechsolutions.co.uk/repository sarge contrib

    You will then need to install the GPG key which you can do using the following as rootcommand
    wget http://www.webmin.com/jcameron-key.asc && sudo apt-key add jcameron-key.asc
    Once you have update the sources file you can install webmin using the following commands.
    sudo apt-get update
    sudo apt-get install webmin

    2. Manually Download the latest webmin archive.
    wget http://www.webmin.com/download/deb/webmin-current.deb

    Install webmin with following command:
    sudo dpkg --install webmin-current

    You can connect to webmin by pointing your browser to:
    http://server-host-ip-address:10000/