Tuesday, July 17, 2012

A simple example of developing C program, patching and applying it to original source file

Purpose : Create a C program, compile it, apply patch to original file
OS Environemnt : Linus
Required Software : gcc
Implementation Steps : 

1. Create a test.c file and put below codes in it :


int main()
 printf("\n I'm test\n");
return 0;

2. Compile above file and check its output :

$ gcc -o test test.c

$ ./test

I'm test

3. Applying patches or change codes and recompile it : 
Modify test.c and add new lines or codes in it. Lets say new file is test_modifed.c. This file contains following codes :


int main()
 printf("\n I'm kamal \n");
 printf("\nI have added one more line. This comes from modifed code\n" );
return 0;

Compile & execute above program now :

$  gcc -o test_modified test_modified.c
$  ./test_modified

 I'm test

I have added one more line. This comes from modifed code

4. Create a patch file : 

Execute following command to create a patch in the same directory.  :

 $ diff -u test.c test_modified.c > test.patch.1

Here test.path.1 will contain following :

$ cat test.patch.1
--- test.c      2012-07-17 07:52:39.000000000 +0530
+++ test_modified.c     2012-07-17 07:54:41.000000000 +0530
@@ -3,5 +3,6 @@
 int main()
  printf("\n I'm kamal \n");
+ printf("\nI have added one more line. This comes from modifed code\n" );
 return 0;

5. Applying above patch to original file :

$ patch -u test.c < test.patch.1
patching file test.c

$ cat test.c
int main()
 printf("\n I'm kamal \n");
 printf("\nI have added one more line. This comes from modifed code\n" );
return 0;

6. Reverting back to previous version :

$ patch -R test.c < test.patch.1
patching file test.c

$ cat test.c


int main()
 printf("\n I'm kamal \n");
return 0;

7. Dry run : You can do dry-run (test prior to be originally changing codes in test.c)

$ patch -p0 --dry-run test.c < test.patch.1

Sunday, June 3, 2012

How to redirect tomcat log to syslog server?

■ Purpose : Redirecting tomcat log messages to central syslog server
OS Environment : Linux[RHEL, Centos]
■  Required Application : tomcat, syslog
■  Assumption :  Assuming that we are going to use log4j tool to log the tomcat messages.
■  Implementation Steps :

1. Modify log4j.properties file :

In order to redirect these messages in syslog file, we should modify4j$CATALINA_BASE/lib/log4j.properties and make sure following entries are present :

log4j.rootLogger=INFO, CATALINA

# Define all the appenders
# Roll-over the log once per day
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayout
log4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

log4j.appender.MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

log4j.appender.HOST-MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.HOST-MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

# Configure which loggers log to which appenders
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=INFO, LOCALHOST

 Note : It's added SYSLOG logger and appender along with other properties in above file. For more information about log4j, you can log on tomcat vendor site and check it from there.

2. Restart tomcat application or instance :
3. Redirect client log messages to syslog :

vi  syslog.conf and put below entry :

 *.* @IP_OF_syslog server

Note : Please replace IP address with appropriate IP. Make sure syslog ports (514) are open in sysog server.

Monday, April 16, 2012

How to configure apache-tomcat on linux box?

■  Purpose : Configure apache-tomcat 
■  OS Environment : Linux (RHEL, Centos)
■  Required Applications: JDk 1.7.0_03, Apache tomcat 6.0.35
■ Implementation Steps :

1.  JDK setup :

$ pwd
$ export JAVA_HOME=/jdk1.7.0_03
$ export PATH=$JAVA_HOME/bin:$PATH

$ which java

$  java -version
java version "1.7.0_03"
Java(TM) SE Runtime Environment (build 1.7.0_03-b04)
Java HotSpot(TM) 64-Bit Server VM (build 22.1-b02, mixed mode)

2. Apache Tomcat setup :

$ groupadd tomcat
$ useradd -g tomcat -s /usr/sbin/nologin -m -d /home/tomcat tomcat
$ cd /var/lib
$ tar zxvf /usr/local/src/apache-tomcat-6.0.35.tar.gz
$ chown -R tomcat.tomcat /var/lib/apache-tomcat-6.0.35

3. Verify tomcat version  :

$ /var/lib/apache-tomcat-6.0.35/bin/version.sh

Output will look like below :
Using CATALINA_BASE: /var/lib/apache-tomcat-6.0.35
Using CATALINA_HOME: /var/lib/apache-tomcat-6.0.35
Using CATALINA_TMPDIR: /var/lib/apache-tomcat-6.0.35/temp
Using JRE_HOME: /jdk1.7.0_03
Using CLASSPATH: /var/lib/apache-tomcat-6.0.35/bin/bootstrap.jar
Server version: Apache Tomcat/6.0.35
Server built: Nov 28 2011 11:20:06
Server number:
OS Name: Linux
OS Version: 2.6.18-308.el5
Architecture: amd64
JVM Version: 1.7.0_03-b04
JVM Vendor: Oracle Corporation

4. Starting/Stopping Tomcat:

$ export CATALINA_HOME=/var/lib/apache-tomcat-6.0.35
$ export CATALINA_BASE=/var/lib/apache-tomcat-6.0.35
$ su -p -s /bin/sh tomcat $CATALINA_HOME/bin/startup.sh

You can make posix compliant script to run above script during booting. Export can be mentioned in root user's profile.

Output will look like  :

Using CATALINA_BASE: /var/lib/apache-tomcat-6.0.35
Using CATALINA_HOME: /var/lib/apache-tomcat-6.0.35
Using CATALINA_TMPDIR: /var/lib/apache-tomcat-6.0.35/temp
Using JRE_HOME: /jdk1.7.0_03
Using CLASSPATH: /var/lib/apache-tomcat-6.0.35/bin/bootstrap.jar

5. Check if tomcat process is running or not :

$ ps aux |grep java
tomcat 10711 9.4 10.1 463404 51696 pts/1 Sl 18:58 0:02 /jdk1.7.0_03/bin/java -Djava.util.logging.config.file=/var/lib/apache-tomcat-6.0.35/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/var/lib/apache-tomcat-6.0.35/endorsed -classpath /var/lib/apache-tomcat-6.0.35/bin/bootstrap.jar -Dcatalina.base=/var/lib/apache-tomcat-6.0.35 -Dcatalina.home=/var/lib/apache-tomcat-6.0.35 -Djava.io.tmpdir=/var/lib/apache-tomcat-6.0.35/temp org.apache.catalina.startup.Bootstrap start
root 10730 0.0 0.1 61232 724 pts/1 R+ 18:58 0:00 grep java

6. Shutdown tomcat application :

$ su -p -s /bin/sh tomcat $CATALINA_HOME/bin/shutdown.sh

Output will look like :

Using CATALINA_BASE: /var/lib/apache-tomcat-6.0.35
Using CATALINA_HOME: /var/lib/apache-tomcat-6.0.35
Using CATALINA_TMPDIR: /var/lib/apache-tomcat-6.0.35/temp
Using JRE_HOME: /jdk1.7.0_03
Using CLASSPATH: /var/lib/apache-tomcat-6.0.35/bin/bootstrap.jar

$ ps aux |grep java
root 10763 0.0 0.1 61232 724 pts/1 R+ 18:59 0:00 grep java

7. Switching to Tomcat User Account :

$ su - -s /bin/sh tomcat
$ id
uid=502(tomcat) gid=503(tomcat) groups=503(tomcat) context=root:system_r:unconfined_t:SystemLow-SystemHigh

Control+D to exit from tomcat shell

8. Setting Up First Tomcat JVM Instance :

$ mkdir -p /opt/tomcat-instance/sales.example.com
$ cd /opt/tomcat-instance/sales.example.com
$ cp -a /var/lib/apache-tomcat-6.0.35/conf .
$ mkdir common logs temp server shared webapps work
$ chown -R tomcat.tomcat /opt/tomcat-instance

9. Environment setup for instance :

$ su - -s /bin/sh tomcat
$ id
uid=1001(tomcat) gid=1001(tomcat) groups=1001(tomcat)
$ pwd
$ whoami

$ cat /opt/tomcat-instance/sales.env 

export JAVA_HOME=/jdk1.7.0_03
export PATH=\$JAVA_HOME/bin:\$PATH
export CATALINA_HOME=/var/lib/apache-tomcat-6.0.35
export CATALINA_BASE=/opt/tomcat-instance/sales.example.com

10. Configuring Tomcat Network Ports : Since this is the first Tomcat instance that's being created here, the default port numbers can be left unchanged in $CATALINA_BASE/conf/server.xml (/opt/tomcat-instance/sales.example.com/conf/server.xml):

11. Starting First Tomcat Instance :

$ source /opt/tomcat-instance/sales.env
$ $CATALINA_HOME/bin/startup.sh

Output will look like :

Using CATALINA_BASE: /opt/tomcat-instance/sales.example.com
Using CATALINA_HOME: /var/lib/apache-tomcat-6.0.35
Using CATALINA_TMPDIR: /opt/tomcat-instance/sales.example.com/temp
Using JRE_HOME: /jdk1.7.0_03
Using CLASSPATH: /var/lib/apache-tomcat-6.0.35/bin/bootstrap.jar

12. Setting Up a Web Application for First Tomcat JVM Instance :

$ vi $CATALINA_BASE/conf/server.xml :

 docBase attribute is set to mysales which stands for the application name within the URL, i.e. "http://serverip/mysales" or "http://serverip:8080/mysales".

13. Setup home page for web Application :

$ cat $CATALINA_BASE/webapps/sales/index.html 


14. Restart First Tomcat Instance :

$ source /opt/tomcat-instance/sales.env
$ $CATALINA_HOME/bin/shutdown.sh

$ $CATALINA_HOME/bin/startup.sh

14. Test :
Access http://serverip/mysales/ , you'll see "Apache Tomcat Sales Home Page".

15. Deploying Java Servlet for Web Application in First Tomcat JVM Instance :

a. Setting up Java Servlet Layout :

$ mkdir -p $CATALINA_BASE/webapps/sales/WEB-INF/classes

$ mkdir $CATALINA_BASE/webapps/sales/WEB-INF/lib
b. Create a Java Servlet :

vi $CATALINA_BASE/webapps/sales/WEB-INF/classes/Sales.java  and put following entries in this file :

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Sales extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
PrintWriter out = response.getWriter();
out.println("Sales Page");

Executing Sales ...


c. Compile above java program :

$ cd $CATALINA_BASE/webapps/sales/WEB-INF/classes
$ javac -classpath "$CATALINA_HOME/lib/*" Sales.java
$ ls
Sales.class Sales.java

d. Configuring the Java Servlet :

$ cat $CATALINA_BASE/webapps/sales/WEB-INF/web.xml

e. Modify index.html &  put some entries :

$ vi $CATALINA_BASE/webapps/sales/index.html

16. Testing and Executing the Java Servlet :

 $ source /opt/tomcat-instance/sales.env
$ $CATALINA_HOME/bin/shutdown.sh
$ $CATALINA_HOME/bin/startup.sh

17. Out of  http://serverip/mysales/  will be 

Apache Tomcat Sales Home Page
Execute Sales

Now click on "Execute Sales", you'll see following :

Executing Sales ...

Wednesday, March 21, 2012

How to enable php with apache on linux?

Purpose : Enable php engine in apache
OS Environement : Linux [RHEL, Centos]
Application : php, apache httpd
Prerequisites : Make sure php is installed in the system first.
Implementation Steps : 

1. Locate libphp5.so

In most of cases, libphp5.so will be present inside /etc/httpd/modules

2. Add following in httpd.conf :

# Use for PHP 5.x:
LoadModule php5_module modules/libphp5.so
AddHandler php5-script .php

# Add index.php to your DirectoryIndex line:
DirectoryIndex index.html index.php

AddType text/html .php

# PHP Syntax Coloring
# (optional but useful for reading PHP source for debugging):

AddType application/x-httpd-php-source phps

3. Restart apache web server :

$ /etc/init.d/httpd restart

4. Verification :

$ httpd -t|grep -i php

5. Create a php file inside web directory. Put the following content in that file like :

vi info.php


6. Access the file through browser like http://localhost/info.php. If it shows all the enables php directives or modules then php works fine.

Wednesday, March 14, 2012

How to store sftp log messages in custom file on Linux ?

■  Purpose : store sftp log messages in custom file
■ OS Environment : RHEL 6
■  Applications: sftp, syslog
■  Prerequisites : Assuming chrooted environment is setup at /chroot, disable SELinux
■  Implementation Steps :

1. Modify sshd config file. 

Make sure "AUTHPRIV" facility is used in sshd_config file. Sample file looks like :

$ cat /etc/ssh/sshd_config :

Subsystem sftp internal-sftp -f AUTHPRIV -l VERBOSE

2. Use proper additional socket file in /etc/sysconfig/rsyslog like :

vi /etc/sysconfig/rsyslog

SYSLOGD_OPTIONS="-m 0 -a /chroots/dev/log"

3. Following lines should be present in /etc/rsyslog.conf :

Add following lines :

$InputUnixListenSocketHostname internal-sftp
$AddUnixListenSocket /chroots/dev/log

# Log internal-sftp activity in a separate file

:programname, isequal, "internal-sftp" -/var/log/sftp.log
:programname, isequal, "internal-sftp" ~
:programname, isequal, "sshd" -/var/log/sftp.log
:programname, isequal, "sshd" ~

4. Make sure that a hard link to socket file like :

$mkdir -p /chroots/dev
$ln /chroots/dev/log /chroots/

5. Restart rsyslog and sshd service :

$ service rsyslog restart
$ service sshd restart

6. Testing :

Open a terminal and execute following command on it like :

$tailf /var/log/sftp.log

Try to access SFTP account like :

$sftp username@IP

Enter password You'll see that all sftp logs messages are available in /var/log/sftp.log. 

Wednesday, March 7, 2012

How to setup multiple gateways for multiple Ethernets?

■ Purpose : Setup multiple gateways in linux
■ OS Environment : Linux [RHEL 5, 6]
■  Application : iproute package
■  Implementation Steps :
■  Assumption : eth0 has gateway 10.1XX.69.1 and eth3's gateway is 10.1XX.66.1.

Concept : Defining the gateway in two tables in conjunction with each interface.

1. Put entries in routing table :

echo "1 ISP1" >> /etc/iproute2/rt_tables
echo "2 ISP2 " >> /etc/iproute2/rt_tables

2.  Setup routing rules for  ISP1 table:

$ ip route add default via 10.1XX.69.1 dev eth0 table ISP1
$ ip rule add from 10.1XX.69.0/24 table ISP1

For the ISP2 table:

$ ip route add default via 10.1XX.66.1 dev eth3 table ISP2
$ ip rule add from 10.1XX.66.0/24 table ISP2

3. Make above rules persistent:

Put below entries in rc.local file  :

ip route add default via 10.1XX.69.1 dev eth0 table ISP1
ip rule add from 10.1XX.69.0/24 table ISP1
ip route add default via 10.1XX.66.1 dev eth3 table ISP2
ip rule add from 10.1XX.66.0/24 table ISP2

Note : You should replace the IP addresses in above commands.

Wednesday, February 29, 2012

How to create bridge on top of bonding in linux?

■ Purpose: create bridge on top of bonding
■ OS Environment: Linux [RHEL, Centose]
■ Application: bonding
■ Practical Usage : Used in KVM environment
■ Implementation Steps : 

1. Create network script :
vi /etc/sysconfig/network-scripts/ifcfg-eth0 & put following entries :


2. Create another network script : 
vi /etc/sysconfig/network-scripts/ifcfg-eth1 & put following entries :


3. Create a network bonding script :

vi /etc/sysconfig/network-scripts/ifcfg-bond0 & put following lines :

BONDING_OPTS="mode=1 miimon=100 updelay=200 downdelay=200 primary=eth0"

4. Create network bridge script :
vi /etc/sysconfig/network-scripts/ifcfg-br0 & put below entries with valid value


5. Add alias of bonding in modprobe.conf like :

# vi /etc/modprobe.conf

alias bond0 bonding

6. Restart network service :

# service network restart

Friday, February 24, 2012

How to start apache uisng worker MPM?

■ Purpose : Start Apache using worker MPM
■ OS Environment : Linux[RHEL, Centos]
■ Application: httpd
■ Implementation Steps:

1. Check if apache is running with MPM :

$ /usr/sbin/apachectl -l

If it's seen that worker.c in the list of loaded modules, then Apache is running with worker MPM. If  it's seen prefork.c, then it is running as prefork.

2. To determine if apache has worker MPM compiled in:

$ /usr/sbin/httpd.worker -l

If it's seen worker.c in the list of compiled-in modules, Apache can run Worker MPM.

3. Un-Comment following line in /etc/sysconfig/httpd : 

HTTPD=/usr/sbin/httpd.worker in 

4. Restart httpd :

$ service httpd restart

Note : Please recompile php with thread-safe option(php still doesn't support worker MPM. It's required mod_fcgid ), else it will throw error.

Wednesday, February 22, 2012

How to create reverse DNS record in bind?

■ Purpose: Create reverse DNS record 
■ OS Environment: Linux
■ Application : bind
■ Assumption: Used C class address
■ Implementation Steps :

1. Add following lines in /etc/named.conf :

zone "0.168.192.in-addr.arpa" IN {
type master;
file "0.168.192.in-addr.arpa";
allow-update { none; };

2. Create zone for this reverse record :

$ vi  /var/named/0.168.192.in-addr.arpa  & put below entries

$TTL 600
@ IN SOA ns1.example.com. host.example.com. (
2012013001 ;Serial Number
86400 ;refresh
7200 ;retry
3600000 ;expire
86400 ;minimum


0.168.192.in-addr.arpa. IN NS ns1.example.com.
0.168.192.in-addr.arpa. IN NS ns2.example.com.
201 IN PTR example.com.

Monday, January 2, 2012

Which command will provide IO details of all processes in the system?

■ Requirement : Command which provides IO details of process
■ OS Environment : Linux
■ Solution : 

$ for i in {1..65353}; do if [ -f /proc/$i/io ] ; then echo "---------------------------------------------------"; echo "Process name :" ; cat /proc/$i/cmdline; echo "PID : $i" ; echo "IO Details:" ; cat /proc/$i/io ; fi; done

NOTE : To dig the IO issue, one can use "iostat, sar, nfsstat -x" commands.