Tuesday, January 31, 2012

Startup services and run levels

The /etc directory has (among other) the following directory entries.


drwxr-xr-x  2 root root     4096 2012-01-29 12:20 rc0.d
drwxr-xr-x  2 root root     4096 2012-01-29 12:20 rc1.d
drwxr-xr-x  2 root root     4096 2012-01-29 12:20 rc2.d
drwxr-xr-x  2 root root     4096 2012-01-29 12:20 rc3.d
drwxr-xr-x  2 root root     4096 2012-01-29 12:20 rc4.d
drwxr-xr-x  2 root root     4096 2012-01-29 12:20 rc5.d
drwxr-xr-x  2 root root     4096 2012-01-29 12:20 rc6.d


Each of these corresponds to a particular runlevel.


In Ubuntu, runlevel 0 is for powerdown, runlevel 6 is for reboot, runlevel 1 is for single user mode and runlevels 2-5 represent regular multi-user mode with GUI and are all the same. (Other Linux OSes may make a distinction but Ubuntu does not).


Let's take a look at the run level 5 directory.


suraj@suraj-Satellite-T135D:/etc/rc5.d$ ls -l
total 4
-rw-r--r-- 1 root root 677 2011-12-15 01:40 README
lrwxrwxrwx 1 root root  26 2012-01-02 08:00 S20clamav-freshclam -> ../init.d/clamav-freshclam
lrwxrwxrwx 1 root root  20 2011-10-30 22:30 S20kerneloops -> ../init.d/kerneloops
lrwxrwxrwx 1 root root  27 2011-10-30 22:30 S20speech-dispatcher -> ../init.d/speech-dispatcher
lrwxrwxrwx 1 root root  19 2012-01-29 08:14 S20tinyproxy -> ../init.d/tinyproxy
lrwxrwxrwx 1 root root  29 2011-10-30 22:30 S20unattended-upgrades -> ../init.d/unattended-upgrades
lrwxrwxrwx 1 root root  19 2011-10-30 22:30 S25bluetooth -> ../init.d/bluetooth
lrwxrwxrwx 1 root root  20 2011-10-30 22:30 S50pulseaudio -> ../init.d/pulseaudio
lrwxrwxrwx 1 root root  15 2011-10-30 22:30 S50rsync -> ../init.d/rsync
lrwxrwxrwx 1 root root  15 2011-10-30 22:30 S50saned -> ../init.d/saned
lrwxrwxrwx 1 root root  19 2011-10-30 22:30 S70dns-clean -> ../init.d/dns-clean
lrwxrwxrwx 1 root root  18 2011-10-30 22:30 S70pppd-dns -> ../init.d/pppd-dns
lrwxrwxrwx 1 root root  14 2011-10-30 22:30 S75sudo -> ../init.d/sudo
lrwxrwxrwx 1 root root  17 2012-01-29 12:20 S92tomcat6 -> ../init.d/tomcat6
lrwxrwxrwx 1 root root  22 2011-10-30 22:30 S99acpi-support -> ../init.d/acpi-support
lrwxrwxrwx 1 root root  21 2011-10-30 22:30 S99grub-common -> ../init.d/grub-common
lrwxrwxrwx 1 root root  18 2011-10-30 22:30 S99ondemand -> ../init.d/ondemand
lrwxrwxrwx 1 root root  18 2011-10-30 22:30 S99rc.local -> ../init.d/rc.local
 

Let's take a look at the README file in this directory.


suraj@suraj-Satellite-T135D:/etc/rc5.d$ more README
The scripts in this directory are executed each time the system enters
this runlevel.

The scripts are all symbolic links whose targets are located in
/etc/init.d/ .

To disable a service in this runlevel, rename its script in this
directory so that the new name begins with a 'K' and a two-digit
number, and run 'update-rc.d script defaults' to reorder the scripts
according to dependencies.  A warning about the current runlevels
being enabled not matching the LSB header in the init.d script will be
printed.  To re-enable the service, rename the script back to its
original name beginning with 'S' and run update-rc.d again.

For a more information see /etc/init.d/README.

Monday, January 30, 2012

Tomcat installation



Note
Tomcat6 and Tomcat7 cannot be installed at the same time as a Ubuntu service. You have to make a choice. Otherwise the apt-get command is going to give you a package dependency error on the second tomcat installation (whichever one it is). If you want to install both of them at the same time, the only option is to extract the compressed tar and not install - with either one or both the versions of Tomcat.

Installation

Tomcat 6 is available from the Ubuntu repository, so we can use apt-get to download and install the package for us.
cgirl@cgirl-Satellite-T135D:~$ sudo apt-get install tomcat6
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following extra packages will be installed:
  authbind libcommons-dbcp-java libcommons-pool-java libecj-java libtomcat6-java tomcat6-common
Suggested packages:
  libgeronimo-jta-1.0.1b-spec-java ecj libecj-java-gcj tomcat6-docs tomcat6-admin tomcat6-examples
  tomcat6-user libtcnative-1
The following NEW packages will be installed:
  authbind libcommons-dbcp-java libcommons-pool-java libecj-java libtomcat6-java tomcat6 tomcat6-common
0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.
Need to get 0 B/4,633 kB of archives.
After this operation, 5,984 kB of additional disk space will be used.
Do you want to continue [Y/n]? y
Preconfiguring packages ...
Selecting previously deselected package libcommons-pool-java.
(Reading database ... 252142 files and directories currently installed.)
Unpacking libcommons-pool-java (from .../libcommons-pool-java_1.5.6-1_all.deb) ...
Selecting previously deselected package libcommons-dbcp-java.
Unpacking libcommons-dbcp-java (from .../libcommons-dbcp-java_1.4-1ubuntu1_all.deb) ...
Selecting previously deselected package libecj-java.
Unpacking libecj-java (from .../libecj-java_3.5.1-3_all.deb) ...
Selecting previously deselected package libtomcat6-java.
Unpacking libtomcat6-java (from .../libtomcat6-java_6.0.32-5ubuntu1.1_all.deb) ...
Selecting previously deselected package tomcat6-common.
Unpacking tomcat6-common (from .../tomcat6-common_6.0.32-5ubuntu1.1_all.deb) ...
Selecting previously deselected package tomcat6.
Unpacking tomcat6 (from .../tomcat6_6.0.32-5ubuntu1.1_all.deb) ...
Selecting previously deselected package authbind.
Unpacking authbind (from .../authbind_1.2.0build3_amd64.deb) ...
Processing triggers for ureadahead ...
Processing triggers for man-db ...
Setting up libcommons-pool-java (1.5.6-1) ...
Setting up libcommons-dbcp-java (1.4-1ubuntu1) ...
Setting up libecj-java (3.5.1-3) ...
Setting up libtomcat6-java (6.0.32-5ubuntu1.1) ...
Setting up tomcat6-common (6.0.32-5ubuntu1.1) ...
Setting up tomcat6 (6.0.32-5ubuntu1.1) ...

Creating config file /etc/default/tomcat6 with new version
Adding system user `tomcat6' (UID 117) ...
Adding new user `tomcat6' (UID 117) with group `tomcat6' ...
Not creating home directory `/usr/share/tomcat6'.
 * Starting Tomcat servlet engine tomcat6                                                          [ OK ]
Setting up authbind (1.2.0build3) ...

Tomcat is installed as a service.
cgirl@cgirl-Satellite-T135D:/etc/init.d$ ls -l tomcat6
-rwxr-xr-x 1 root root 7149 2011-09-15 05:46 tomcat6
cgirl@cgirl-Satellite-T135D:/etc/init.d$

Here are a few lines from the tomcat6 script shown above.
PATH=/bin:/usr/bin:/sbin:/usr/sbin
NAME=tomcat6
DESC="Tomcat servlet engine"
DEFAULT=/etc/default/$NAME
JVM_TMP=/tmp/tomcat6-tmp

# Run Tomcat 6 as this user ID and group ID
TOMCAT6_USER=tomcat6
TOMCAT6_GROUP=tomcat6
# The first existing directory is used for JAVA_HOME (if JAVA_HOME is not
# defined in $DEFAULT)
JDK_DIRS="/usr/lib/jvm/java-6-openjdk /usr/lib/jvm/java-6-sun /usr/lib/jvm/java-1.5.0-sun /usr/lib/j2sdk1.5-sun /u
sr/lib/j2sdk1.5-ibm"

# Directory where the Tomcat 6 binary distribution resides
CATALINA_HOME=/usr/share/$NAME
# Directory for per-instance configuration files and webapps
CATALINA_BASE=/var/lib/$NAME
# Define other required variables
CATALINA_PID="/var/run/$NAME.pid"
CATALINA_SH="$CATALINA_HOME/bin/catalina.sh"


From the above lines, we can conclude this is where Tomcat 6 is actually installed.
cgirl@cgirl-Satellite-T135D:/usr/share/tomcat6$ ls -l
total 16
drwxr-xr-x 2 root root 4096 2012-01-29 12:20 bin
-rw-r--r-- 1 root root   39 2011-09-15 05:46 defaults.md5sum
-rw-r--r-- 1 root root 1960 2011-09-15 05:46 defaults.template
drwxr-xr-x 2 root root 4096 2012-01-29 12:20 lib


We can also conclude that the following directory is where the configuration files and webapps reside.
cgirl@cgirl-Satellite-T135D:/var/lib/tomcat6$ ls -l
total 16
drwxr-xr-x 3 tomcat6 tomcat6 4096 2012-01-29 12:20 common
lrwxrwxrwx 1 root    root      12 2011-10-13 19:07 conf -> /etc/tomcat6
lrwxrwxrwx 1 root    root      17 2011-10-13 19:07 logs -> ../../log/tomcat6
drwxr-xr-x 3 tomcat6 tomcat6 4096 2012-01-29 12:20 server
drwxr-xr-x 3 tomcat6 tomcat6 4096 2012-01-29 12:20 shared
drwxrwxr-x 3 tomcat6 tomcat6 4096 2012-01-29 12:20 webapps
lrwxrwxrwx 1 root    root      19 2011-10-13 19:07 work -> ../../cache/tomcat6



The log file for the server is in this directory (the catalina.out file).
cgirl@cgirl-Satellite-T135D:/var/log/tomcat6$ ls -l
total 16
-rw-r--r-- 1 tomcat6 tomcat6 2253 2012-01-29 14:15 catalina.2012-01-29.log
-rw-r--r-- 1 tomcat6 tomcat6 1927 2012-01-30 19:13 catalina.2012-01-30.log
-rw-r--r-- 1 tomcat6 root    4180 2012-01-30 19:13 catalina.out
-rw-r--r-- 1 tomcat6 tomcat6    0 2012-01-29 12:20 localhost.2012-01-29.log
-rw-r--r-- 1 tomcat6 tomcat6    0 2012-01-30 00:52 localhost.2012-01-30.log

cgirl@cgirl-Satellite-T135D:/var/log/tomcat6$


Here's how a successful startup looks.
INFO: Starting Servlet Engine: Apache Tomcat/6.0.32
Jan 29, 2012 2:10:17 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor ROOT.xml
Jan 29, 2012 2:10:18 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Jan 29, 2012 2:10:18 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 665 ms
Jan 29, 2012 2:15:49 PM org.apache.coyote.http11.Http11Protocol pause
INFO: Pausing Coyote HTTP/1.1 on http-8080
Jan 29, 2012 2:15:50 PM org.apache.catalina.core.StandardService stop
INFO: Stopping service Catalina
Jan 29, 2012 2:15:50 PM org.apache.coyote.http11.Http11Protocol destroy
INFO: Stopping Coyote HTTP/1.1 on http-8080
Jan 30, 2012 12:52:57 AM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
Jan 30, 2012 12:52:57 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1367 ms
Jan 30, 2012 12:52:57 AM org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
Jan 30, 2012 12:52:57 AM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.32
Jan 30, 2012 12:52:57 AM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor ROOT.xml
Jan 30, 2012 12:52:57 AM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Jan 30, 2012 12:52:57 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 544 ms

 

Here's how a shutdown looks.
Jan 30, 2012 12:59:57 AM org.apache.coyote.http11.Http11Protocol pause
INFO: Pausing Coyote HTTP/1.1 on http-8080
Jan 30, 2012 12:59:58 AM org.apache.catalina.core.StandardService stop
INFO: Stopping service Catalina
Jan 30, 2012 12:59:58 AM org.apache.coyote.http11.Http11Protocol destroy
INFO: Stopping Coyote HTTP/1.1 on http-8080


Tomcat 6 configuration - server.xml, context.xml and web.xml

 In $CATALINA_BASE/conf, you can find these 3 files.

Sunday, January 29, 2012

tinyproxy

tinyproxy is a lightweight proxy for Linux based operating systems like Ubuntu. In this blog post, I discuss the steps I took for installing and configuring the proxy on my Ubuntu 11.10 installation. I primarily found it to be an effective whitelist (explained below) based internet filter, which is what I wanted for my kids at this time. I did not find it to be a good blacklist based internet filter - the downloadable blacklists are massive and brings tinyproxy to its knees. If you want a blacklist based internet filter, look elsewhere. I'll try that someday too - squid/squidguard, dansguardian seem to be promising but I haven't tested those yet.



Installation


You need to be system administrator in order to perform this task. tinyproxy is available from the Ubuntu repository, so we can use apt-get to download and install the package for us.


cgirl@cgirl-Satellite-T135D:~$ sudo apt-get install tinyproxy
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following NEW packages will be installed:
  tinyproxy
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 0 B/61.6 kB of archives.
After this operation, 270 kB of additional disk space will be used.
Selecting previously deselected package tinyproxy.
(Reading database ... 252120 files and directories currently installed.)
Unpacking tinyproxy (from .../tinyproxy_1.8.2-2_amd64.deb) ...
Processing triggers for man-db ...
Processing triggers for ureadahead ...
Setting up tinyproxy (1.8.2-2) ...
Starting tinyproxy: tinyproxy.

Technical observations on the installed package


Feel free to skip this section(and jump to the Configuring tinyproxy section below) if you are looking to install and not worry about any technical details. Being somewhat new to the Ubuntu world, these were my observations that I found informative and interesting, which is why I am including them here.


From the last line of the installation log, it is clear that tinyproxy is installed as a service and has been started up right after installation.


cgirl@cgirl-Satellite-T135D:/etc/init.d$ cd /etc/init.d 
cgirl@cgirl-Satellite-T135D:/etc/init.d$ ls -l tinyproxy
-rwxr-xr-x 1 root root 2041 2010-03-22 10:46 tinyproxy
 
cgirl@cgirl-Satellite-T135D:/etc/init.d$


The installation seems to make no entries in the /etc/init and /etc/default folders.
According to Ubuntu documentation,


/etc/init.d is where all the traditional sysvinit scripts and the backward compatible scripts for upstart live. The backward compatible scripts basically run service myservice start instead of doing anything themselves. Some just show a notice to use the "service" command

Here are a few selected lines from the tinyproxy script.

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
CONFIG=/etc/tinyproxy.conf
DAEMON=/usr/sbin/tinyproxy
DESC=tinyproxy
FLAGS=
NAME=tinyproxy

The DAEMON line tells us where the executable actually sits.

cgirl@cgirl-Satellite-T135D:/usr/sbin$ ls -l tinyproxy
-rwxr-xr-x 1 root root 77632 2011-04-30 17:27 tinyproxy

The CONFIG line points us to the configuration file that drives how tinyproxy behaves. See below for details about configuring tinyproxy to meet your needs. 

Configuring tinyproxy

The file /etc/tinyproxy.conf is the tinyproxy configuration file that needs to be edited to suit your needs. Here are a few selected lines from the configuration file.

Port 8888 - This is the port that tinyproxy will listen on for incoming requests. Any browser or program that needs to use the proxy needs to refer to this port.
Logfile "/var/log/tinyproxy/tinyproxy.log" - The log file that you can use to check if tinyproxy is working correctly.

#Filter "/etc/filter" - The filter file location. This line is commented out by default, so nothing is filtered if left like that. Also read the FilterDefaultDeny line explained below.

#FilterDefaultDeny Yes - This line actually commented out by default and if left like that, defaults to No. This means that tinyproxy functions in "blacklist mode" ie., any request will be allowed as long as the URL/domain is not on the filtered list. If you uncomment this line (set the option to Yes), then tinyproxy will function in "whitelist mode" ie., any domain/URL that will be allowed needs to be specified explicitly in the filter file.

There are, of course, several other settings in the configuration file, these only list the ones that I specifically found relevant and/or changed for my needs.


Restarting tinyproxy


Once the configuration file and filter files have been setup, tinyproxy needs to be restarted as follows for the changes to take effect.


cgirl@cgirl-Satellite-T135D:~$ sudo service tinyproxy restart
Restarting tinyproxy: tinyproxy.
cgirl@cgirl-Satellite-T135D:~$

MAC Address in Ubuntu

Each Ethernet adapter on your computer has a corresponding MAC address. On Ubuntu, use the following command to display all Ethernet adapters on your computer along with a bunch of details.


$ ifconfig

eth0      Link encap:Ethernet  HWaddr c8:0a:a9:55:a5:44 
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Interrupt:42

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:46879 errors:0 dropped:0 overruns:0 frame:0
          TX packets:46879 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:9499263 (9.4 MB)  TX bytes:9499263 (9.4 MB)

wlan0     Link encap:Ethernet  HWaddr 70:f1:a1:5f:e4:1f 
          inet addr:10.0.0.7  Bcast:10.0.0.255  Mask:255.255.255.0
          inet6 addr: fe80::72f1:a1ff:fe5f:e41f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1430  Metric:1
          RX packets:91695 errors:0 dropped:0 overruns:0 frame:0
          TX packets:79413 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:75188528 (75.1 MB)  TX bytes:12304230 (12.3 MB)


Buried in those details is the MAC address. Or you could simply use


$ ifconfig | grep HWAddr


to simply list all the MAC Addresses on your computer.


Here's a sample output from ifconfig | grep HWAddr


eth0      Link encap:Ethernet  HWaddr c8:0a:a9:55:a5:44 
wlan0     Link encap:Ethernet  HWaddr 70:f1:a1:5f:e4:1f