Posts Tagged ‘linux’

Autorun Screen Sessions on an SSH Session

06 May

Source: Automatic ‘screen’ on remote logins

It annoys me whenever an SSH session I have to a server drops, and I can’t reconnect to the session, especially when there’s a long running tasks that I need to know the status of.

So anyway, the solution to this one is a project called GNU Screen. To summarize, it creates a virtual terminal session for you that you can actually resume on if ever your connection (or whatever) was terminated.

The problem with it is that you have to manually invoke the screen command when you successfully login, like so:

screen -D -RR

To run it on every SSH session, you can a script it to your bashrc profile. Either with your own bashrc located in ~/.bashrc or a global profile in /etc/bash.bashrc for all users. I normally put it for all users.

The script to add is found below, and is taken from

# Auto-screen invocation. see:
# if we're coming from a remote SSH connection, in an interactive session
# then automatically put us into a screen(1) session.   Only try once
# -- if $STARTED_SCREEN is set, don't try it again, to avoid looping
# if screen fails for some reason.
if [ "$PS1" != "" -a "${STARTED_SCREEN:-x}" = x -a "${SSH_TTY:-x}" != x ]
  [ -d $HOME/lib/screen-logs ] || mkdir -p $HOME/lib/screen-logs
  sleep 1
  screen -RR && exit 0
  # normally, execution of this rc script ends here...
  echo "Screen failed! continuing with normal bash startup"
# [end of auto-screen snippet]
No Comments

Posted in HowTo


Installing Oracle 10g on Ubuntu 9.10 (Karmic Koala) Server 64-bit

27 Apr

Source: Installing Oracle 10g On Ubuntu Karmic 64 Bit or OtherwiseInstalling Oracle 10g On Ubuntu Karmic 64 Bit Or Otherwise (Part 2)

So apparently I needed to install Oracle 10g to Ubuntu, and ran through a lot of hassles. I found a guide that’s well written but had some trouble with 64-bit server, and am writing for this for others who will encounter some trouble as well.

I’m breaking this down the following sections:

  1. Installing Required Libraries
  2. Setting Up Users and Groups
  3. Setting Kernel Parameters
  4. Emulating SUSE and Red Hat
  5. Setting Up the Oracle Environment
  6. Installing Oracle
  7. Creating the Listener
  8. Creating a Database
  9. Creating Startup/Shutdown Scripts

Installing Required Libraries

Since Ubuntu is not an officially supported OS, we need to emulate those that are, and since Oracle mixes 32-bit and 64-bit libraries, we need to install i386, and even ia32 libraries. Run the following:

sudo apt-get install libaio1 rpm gcc-multilib libc6-dev-i386 libc6-dev-i386 libc6-i386 ia32-libs

In addition, doesn’t come with LIBSTDC++5, so you have to install manually installing the package. Run the following:

wget ""
ar vx libstdc++5_3.3.6-17ubuntu1_amd64.deb
tar zxvf data.tar.gz
file usr/lib/
sudo install usr/lib/ /usr/lib32/
cd /usr/lib32
sudo ln -s
cd /usr/lib
sudo ln -s /usr/lib32/

Setting Up Users and Groups

We need to create the group oinstall, dba, and nobody, and create a user named oracle. Run the following:

sudo groupadd oinstall
sudo groupadd dba
sudo groupadd nobody
sudo useradd -m oracle -g oinstall -G dba -s /bin/bash

And don’t forget to change the password.

sudo passwd oracle

Take note of the oinstall group ID as you will need this in setting the Kernel Parameters later. Do this by running the code below:

cat /etc/group | grep oinstall

The ID should be in the format 10XX. If it’s a newly installed system, it should be 1002.

Setting Kernel Parameters

We need to override the kernel parameters by running the following:

sudo vi /etc/sysctl.conf

And add the settings below at the bottom of the file. Take note that the [oinstall_group_id] should be replaced with what you got in the steps above (1002).

vm.hugetlb_shm_group = [oinstall_group_id]
kernel.shmall = 2097152
kernel.shmmax = 2147483648
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
fs.file-max = 65536
net.ipv4.ip_local_port_range = 1024 65000

Reload the parameters by running the following:

sudo /sbin/sysctl -p

We also need to edit limits of the system. Edit the limits.conf file by running:

sudo vi /etc/security/limits.conf

Add add the settings below to the bottom of the file.

* soft nproc 2047
* hard nproc 16384
* soft nofile 1024
* hard nofile 65536

Emulating SUSE and Red Hat

We emulate SUSE by creating symbolic links of the following:

sudo ln -s /usr/bin/awk /bin/awk
sudo ln -s /usr/bin/rpm /bin/rpm
sudo ln -s /lib/ /lib/
sudo ln -s /usr/bin/basename /bin/basename

We emulate Red Hat by creating the redhat-release file to trick the Oracle Installer that we are using Red Hat. =) Edit the file:

sudo vi /etc/redhat-release

And add the following:

Red Hat Enterprise Linux AS release 3 (Taroon)

Setting Up the Oracle Environment

I normally install my oracle database inside /opt so from hereon, you have to take note that /opt/oracle/10g will be the oracle home, and /opt/oracle will be the oracle base. The SID I will be using here will be orcl. Run the following:

sudo mkdir -p /opt/oracle/10g
sudo chown -R oracle:oinstall /opt/oracle
sudo chmod -R 775 /opt/oracle

Update environment profile by editing the global profile.

sudo vi /etc/profile

And add the following settings at the bottom.

export ORACLE_BASE=/opt/oracle
export ORACLE_HOME=/opt/oracle/10g
export ORACLE_SID=orcl

Installing Oracle

Proceed with installation. My install directory is in /home/installers Unpack the downloaded oracle installer:

gunzip 10201_database_linux_x86_64.cpio.gz
cpio -id < 10201_database_linux_x86_64.cpio
sudo chown -R oracle:oinstall .

Normally, Ubuntu Server doesn’t have X server installed, so you will have problems running the installer as it actuallly has GUI. The easiest way to install this is to access a desktop with X server, and create a connection with SSH forwarding.

ssh -X oracle@oracle-server

From there, fire up the installer:

cd /home/installers/database

The GUI should come out on your client machine. Once the installer is up, uncheck the “Create Starter Database” option, and keep clicking on Next to accept the defaults.

If you encounter some error about the “collector” you can just ignore this as Ubuntu is not supported.

After the installation, the installer will ask you to run two scripts as root. Run them as is.

Oracle is Installed! X_X

Creating the Listener

We create the listener by running the Network Configuration Assistant (needs X server). As with the installer, we need to have X forwarding enabled. The command should be in the oracle path, if not, there might have been a problem with setting up the oracle environment. Run the following as oracle user:


Just use the defaults and create the listener.

Creating the Database

We create a database by running the Database Configuration Assistant (needs X). We also need X forwarding enabled here. Run the assistant with the following:


Create the database as you please. Although you have to take note of the Database Name/SID, and it should match the ORACLE_SID environment variable you set on the /etc/profile file.

Creating Startup/Shutdown Scripts

First, we need to edit the /etc/oratab file.

sudo vi /etc/oratab

My oratab file contains the following:


Change the last character, N, to Y.

Next, we edit dbstart as oracle user. Run the following:

vi $ORACLE_HOME/bin/dbstart

Look for the following line:


Just change that to your ORACLE_HOME (/opt/oracle/10g) and save the file.

Create a startup script found in /etc/init.d/ named dbora. The following script is taken here. Take note of the ORACLE_HOME and ORACLE_SID variables.

# /etc/init.d/dbora
# Startup script for Oracle databases

export ORACLE_HOME=/opt/oracle/10g
export ORACLE_SID=orcl

case "$1" in
echo -n "Starting Oracle: "
su oracle -c $ORACLE_HOME/bin/dbstart
touch /var/lock/oracle

su oracle -c "$ORACLE_HOME/bin/emctl start dbconsole"
echo "OK"
echo -n "Shutdown Oracle: "
su oracle -c $ORACLE_HOME/bin/dbshut
rm -f /var/lock/oracle
echo "OK"
echo "Usage: 'basename $0' start|stop"
exit 1
exit 0

Modify the file so it is executable

sudo chmod 775 /etc/init.d/dbora

Register it in Ubuntu’s startup script by running the following:

sudo update-rc.d dbora defaults 99

Everything should be working as is. Try to access Enterprise Manager by accessing port 1158 (default) through HTTP.


Installing PostgreSQL 8.4, PostGIS 1.4.1, and pgRouting 1.0.3 on Ubuntu 9.10 (Karmic Koala)

01 Feb

Go to the bathroom before we start this one! hehe

I’m breaking this into the following 3 sections:

  1. Installing PostgreSQL 8.4
  2. Installing PostGIS 1.4.1
  3. Installing pgRouting 1.0.3

Installing PostgreSQL 8.4

Get a working copy of PostgreSQL running. At the time of this writing, Ubuntu has deprecated PostgreSQL 8.3, and has 8.4 as stable. So that’s what aptitude will install here.

sudo apt-get install postgresql

Normally, you wouldn’t have access to the database right now. You need to use the postgres user to set up. I’d recommend not changing the password of the postgres user. Just create a PostgreSQL super user.

sudo su -l postgres
createuser -E -P -s dbsuperuser

Now, try to login to the database with the super user you just created.

psql -U dbsuperuser -W -h postgres

You should come out with the following prompt:


Installing PostGIS 1.4.1

We’re compiling PostGIS, so we gotta get compilers. Normally you’d have them, unless you made a minimal install. I usually just get the build-essential package to get it over with.

sudo apt-get install build-essential
sudo apt-get install libgeos-dev
sudo apt-get install proj
sudo apt-get install postgresql-server-dev-8.4

You need the last one to be able to compile PostGIS. Had a lot of trouble without it, so I just added it. =)

Get the latest version of PostGIS here. At this time, PostGIS is at 1.4.1.

tar zxvf postgis-1.4.1.tar.gz
cd postgis-1.4.1
sudo make install

Everything should be compiled. Now we need to create the PostGIS database template. I normally have the naming scheme of template_XXX, so I’m naming my database as template_postgis.

For the next few statements, you can either switch to the postgres user, so you don’t have to enter the password everytime. I normally use the super user I create to do this.

createdb -U dbsuperuser -h -W -E UNICODE template_postgis
createlang -U dbsuperuser -h -W -d template_postgis plpgsql
psql -U dbsuperuser -h -W template_postgis &lt; /usr/share/postgresql/8.4/contrib/postgis.sql
psql -U dbsuperuser -h -W template_postgis &lt; /usr/share/postgresql/8.4/contrib/spatial_ref_sys.sql

Check your PostGIS installation with the following command:

psql -U dbsuperuser -h -W -c "SELECT postgis_lib_version();" template_postgis

Should give you the version of your PostGIS.

Installing pgRouting 1.0.3

You can follow the official guide here.

Now it gets complicated. Stable version 1.0.3 of pgRouting as of the moment isn’t compatible to compile with 8.4. You have to edit some code… not really. Just add a few include headers in the C files. =)

Let’s begin by installing dependencies. First up, libboost… this is a huge library.

sudo apt-get install libboost-graph-dev
sudo apt-get install cmake

Next, gaul. Check out the latest ones here. Right now, mine’s 0.1850-0.

tar -zxvf gaul-devel-0.1850-0.tar.gz
cd gaul-devel-0.1850-0
./configure --disable-slang
sudo make install
sudo ldconfig

Get pgRouting here. Now, I don’t use DD so I skipped installing CGAL and DD.

tar -zxvf pgRouting-1.03.tgz
cd pgrouting/
cmake -DWITH_TSP=on

At this point, if cmake spits out an error like this:

core/src/dijkstra.c: In function ‘fetch_edge_columns’:
core/src/dijkstra.c:98: error: ‘INT4OID’ undeclared (first use in this function)
core/src/dijkstra.c:98: error: (Each undeclared identifier is reported only once
core/src/dijkstra.c:98: error: for each function it appears in.)
core/src/dijkstra.c:100: error: ‘FLOAT8OID’ undeclared (first use in this function)

There’s an issue with PostgreSQL 8.4. v1.0.3 still doesn’t support 8.4 at the moment (pgRouting Ticket #160), so I have to edit the following files (Hat tip to thge on this one):


Just add the line:

#include "catalog/pg_type.h"

That’ll make pgRouting see PostgreSQL 8.4 data types.

cmake -DWITH_TSP=on
sudo make install

pgRouting should now be installed in /usr/share/postlbs. As I said before, I like my database naming schemes, so I’m creating another template for pgRouting called template_pgrouting. Create the database with template_postgis as your base template.

createdb -U dbsuperuser -h -W -E UNICODE -T template_postgis template_routing
psql -U dbsuperuser -h -W template_routing < /usr/share/postlbs/routing_core.sql
psql -U dbsuperuser -h -W template_routing < /usr/share/postlbs/routing_core_wrappers.sql
psql -U dbsuperuser -h -W template_routing < /usr/share/postlbs/routing_topology.sql
psql -U dbsuperuser -h -W template_routing < /usr/share/postlbs/routing_tsp.sql
psql -U dbsuperuser -h -W template_routing < /usr/share/postlbs/routing_tsp_wrappers.sql

We’re done!! Grab a coke and go pee!

Update: Fixed package name from build-essentials to build-essential; Added postgres to the psql command to connect to database; Thanks Thera


Setting Up an Ubuntu 9.10 (Karmic Koala) Linode VPS

21 Jan

So I was shopping for a VPS over the past week and decided that Linode would be a good choice. My deciding factor? Check out the benchmarks here by Eivind Uggedal (Thanks btw).

So anyway, here’s a summary:

  1. Getting Started with Linode
  2. Setup SSH
  3. Setup Firewall

Getting Started with Linode

Now, basically, you’ll want to follow the Getting Started with Linode guide… for me, just setup your default settings, and connect to the server through SSH.

Once in, you’ll want to secure your system. We’ll do an upgrade.

apt-get update
apt-get upgrade

Now, create your user, change the password, and add it as a sudoer.

adduser -m john
passwd john
# visudo uses nano or vi as the editor depending on your environment, normally it's nano on the first try
# In case you aren't familiar with these, just use your favorite text editor and edit /etc/sudoers

Look for the line:

root    ALL=(ALL) ALL

And add the following line below it:

john    ALL=(ALL) ALL

Oh and btw, change john to whatever username you wish.

Setup SSH

This is the most important. Brute force attackers will hit the port 22 first and run common usernames and passwords against. If they detect that port 22 is not open, they’ll just give up on your IP, and move on to the next. So this is very, VERY important. I make mine listen to… just kidding. I’m not telling you of course. Anyway, to change the port, edit /etc/ssh/sshd_config (Use whatever text editor you are comfortable with, mine’s VI):

sudo vi /etc/ssh/sshd_config

Edit the following configurations:

Port 22
PermitRootLogin yes

Change listening port to whatever port you might want to listen to (E.G. 22123), and disallow root login (change it to no). Save your file, and restart your SSH service by running:

sudo /etc/init.d/ssh restart

You just made your server 10-fold more secure.

Setup Firewall

So Ubuntu recommends ufw (Uncomplicated Firewall). This isn’t uncomplicated at all if you are used to iptables as you have to relearn the commands. No fear, it’s pretty simple. First up, let’s get it working:

sudo apt-get install ufw
# Next line is depending on the SSH port that you allowed, very, Very, VERY important! =)
sudo ufw allow 22123
# Next 2 lines is if you want HTTP/HTTPS, which you most probably will
sudo ufw allow 80
sudo ufw allow 443

That’s it. If you want to delete the rule again, just run:

sudo ufw delete deny 80

And it’ll remove block the HTTP service.