Tuesday, October 22, 2013

How to add port to firewall in centos

You can just open /etc/sysconfig/iptables file and add the below line and change the port which port you want to allow from firewall.

-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT

Save the file and restart iptables service.

Open the terminal and run the following command /etc/init.d/iptables restart.

Linux Terminal Command Reference

System Info

date – Show the current date and time
cal – Show this month's calendar
uptime – Show current uptime
w – Display who is online
whoami – Who you are logged in as
finger user – Display information about user
uname -a – Show kernel information
cat /proc/cpuinfo – CPU information
cat /proc/meminfo – Memory information
df – Show disk usage
du – Show directory space usage
free – Show memory and swap usage

Keyboard Shortcuts

Enter – Run the command
Up Arrow – Show the previous command
Ctrl + R – Allows you to type a part of the command you're looking for and finds it
Ctrl + Z – Stops the current command, resume with fg in the foreground or bg in the background
Ctrl + C – Halts the current command, cancel the current operation and/or start with a fresh new line
Ctrl + L – Clear the screen
command | less – Allows the scrolling of the bash command window using Shift + Up Arrow and Shift + Down Arrow
!! – Repeats the last command
command  !$ – Repeats the last argument of the previous command
Esc + . (a period) – Insert the last argument of the previous command on the fly, which enables you to edit it before executing the command
Ctrl + A – Return to the start of the command you're typing
Ctrl + E – Go to the end of the command you're typing
Ctrl + U – Cut everything before the cursor to a special clipboard, erases the whole line
Ctrl + K – Cut everything after the cursor to a special clipboard
Ctrl + Y – Paste from the special clipboard that Ctrl + U and Ctrl + K save their data to
Ctrl + T – Swap the two characters before the cursor (you can actually use this to transport a character from the left to the right, try it!)
Ctrl + W – Delete the word / argument left of the cursor in the current line
Ctrl + D – Log out of current session, similar to exit

Learn the Commands

apropos subject – List manual pages for subject
man -k keyword – Display man pages containing keyword
man command – Show the manual for command
man -t man | ps2pdf - > man.pdf  – Make a pdf of a manual page
which command – Show full path name of command
time command – See how long a command takes
whereis app – Show possible locations of app
which app – Show which app will be run by default; it shows the full path

Searching

grep pattern files – Search for pattern in files
grep -r pattern dir – Search recursively for pattern in dir
command | grep pattern – Search for pattern in the output of command
locate file – Find all instances of file
find / -name filename – Starting with the root directory, look for the file called filename
find / -name ”*filename*” – Starting with the root directory, look for the file containing the stringfilename
locate filename – Find a file called filename using the locate command; this assumes you have already used the command updatedb (see next)
updatedb – Create or update the database of files on all file systems attached to the Linux root directory
which filename – Show the subdirectory containing the executable file  called filename
grep TextStringToFind /dir – Starting with the directory called dir, look for and list all files containingTextStringToFind

File Permissions

chmod octal file – Change the permissions of file to octal, which can be found separately for user, group, and world by adding: 4 – read (r), 2 – write (w), 1 – execute (x)
Examples:
chmod 777 – read, write, execute for all
chmod 755 – rwx for owner, rx for group and world
For more options, see man chmod.

File Commands

ls – Directory listing
ls -l – List files in current directory using long format
ls -laC – List all files in current directory in long format and display in columns
ls -F – List files in current directory and indicate the file type
ls -al – Formatted listing with hidden files
cd dir – Change directory to dir
cd – Change to home
mkdir dir – Create a directory dir
pwd – Show current directory
rm name – Remove a file or directory called name
rm -r dir – Delete directory dir
rm -f file – Force remove file
rm -rf dir – Force remove an entire directory dir and all it’s included files and subdirectories (use with extreme caution)
cp file1 file2 – Copy file1 to file2
cp -r dir1 dir2 – Copy dir1 to dir2; create dir2 if it doesn't exist
cp file /home/dirname – Copy the file called filename to the /home/dirname directory
mv file /home/dirname – Move the file called filename to the /home/dirname directory
mv file1 file2 – Rename or move file1 to file2; if file2 is an existing directory, moves file1 into directoryfile2
ln -s file link – Create symbolic link link to file
touch file – Create or update file
cat > file – Places standard input into file
cat file – Display the file called file
more file – Display the file called file one page at a time, proceed to next page using the spacebar
head file – Output the first 10 lines of file
head -20 file – Display the first 20 lines of the file called file
tail file – Output the last 10 lines of file
tail -20 file – Display the last 20 lines of the file called file
tail -f file – Output the contents of file as it grows, starting with the last 10 lines

Compression

tar cf file.tar files – Create a tar named file.tar containing files
tar xf file.tar – Extract the files from file.tar
tar czf file.tar.gz files – Create a tar with Gzip compression
tar xzf file.tar.gz – Extract a tar using Gzip
tar cjf file.tar.bz2 – Create a tar with Bzip2 compression
tar xjf file.tar.bz2 – Extract a tar using Bzip2
gzip file – Compresses file and renames it to file.gz
gzip -d file.gz – Decompresses file.gz back to file

Printing

/etc/rc.d/init.d/lpd start – Start the print daemon
/etc/rc.d/init.d/lpd stop – Stop the print daemon
/etc/rc.d/init.d/lpd status – Display status of the print daemon
lpq – Display jobs in print queue
lprm – Remove jobs from queue
lpr – Print a file
lpc – Printer control tool
man subject | lpr – Print the manual page called subject as plain text
man -t subject | lpr – Print the manual page called subject as Postscript output
printtool – Start X printer setup interface

Network

ifconfig – List IP addresses for all devices on the local machine
ping host – Ping host and output results
whois domain – Get whois information for domain
dig domain – Get DNS information for domain
dig -x host – Reverse lookup host
wget file – Download file
wget -c file – Continue a stopped download

SSH

ssh user@host – Connect to host as user
ssh -p port user@host – Connect to host on port port as user
ssh-copy-id user@host – Add your key to host for user to enable a keyed or passwordless login

User Administration

adduser accountname – Create a new user call accountname
passwd accountname – Give accountname a new password
su – Log in as superuser from current login
exit – Stop being superuser and revert to normal user

Process Management

ps – Display your currently active processes
top – Display all running processes
kill pid – Kill process id pid
killall proc – Kill all processes named proc (use with extreme caution)
bg – Lists stopped or background jobs; resume a stopped job in the background
fg – Brings the most recent job to foreground
fg n – Brings job n to the foreground

Installation from source

./configure
make
make install
dpkg -i pkg.deb – install a DEB package (Debian / Ubuntu / Linux Mint)
rpm -Uvh pkg.rpm – install a RPM package (Red Hat / Fedora)

Stopping & Starting

shutdown -h now – Shutdown the system now and do not reboot
halt – Stop all processes - same as above
shutdown -r 5 – Shutdown the system in 5 minutes and reboot
shutdown -r now – Shutdown the system now and reboot
reboot – Stop all processes and then reboot - same as above
startx – Start the X system

Monday, October 14, 2013

How to install MySQL on Ubuntu/Debian

It may seem easy for some, but for others, installing MySQL on Ubuntu or Debian Linux is not an easy task. This article explains to you how to install the MySQL Server and Client packages on a Ubuntu/Debian system.

First of all, make sure your package management tools are up-to-date. Also make sure you install all the latest software available.

sudo apt-get update
sudo apt-get dist-upgrade
After a few moments (or minutes, depending on the state of your system), you’re ready to install MySQL. ~ By default, recent Ubuntu/Debian systems install a MySQL Server from the 5-branch. This is a good thing, so don’t worry.

First, install the MySQL server and client packages:

sudo apt-get install mysql-server mysql-client
When done, you have a MySQL database read to rock ‘n roll. However, there’s more to do.

You need to set a root password, for starters. MySQL has it’s own user accounts, which are not related to the user accounts on your Linux machine. By default, the root account of the MySQL Server is empty. You need to set it. Please replace ‘mypassword’ with your actual password and myhostname with your actual hostname.

sudo mysqladmin -u root -h localhost password 'mypassword'
sudo mysqladmin -u root -h myhostname password 'mypassword'
Now, you probably don’t want just the MySQL Server. Most likely you have Apache+PHP already installed, and want MySQL to go with that. Here are some libraries you need to install to make MySQL available to PHP:

sudo apt-get install php5-mysql
Or for Ruby:

sudo apt-get install libmysql-ruby
You can now access your MySQL server like this:

mysql -u root -p
Have fun using MySQL Server.

How to install Oracle Java in Linux

Check to see if your Ubuntu Linux operating system architecture is 32-bit or 64-bit, open up a terminal and run the following command below.
getconf LONG_BIT

It will print the exact number 32 or 64.

Download the Oracle Java JDK/JRE for Linux. Make sure you select the correct compressed binaries for your system architecture 32-bit or 64-bit (which end in tar.gz).
For example, if you are on Ubuntu Linux 32-bit operating system download 32-bit Oracle Java binaries.
For example, if you are on Ubuntu Linux 64-bit operating system download 64-bit Oracle Java binaries.

Important Information: 64-bit Oracle Java binaries do not work on 32-bit Ubuntu Linux operating systems, you will receive multiple system error messages, if you attempt to install 64-bit Oracle Java on 32-bit Ubuntu Linux.

Copy the Oracle Java binaries into the /usr/local/java directory. In most cases, the Oracle Java binaries are downloaded to: /home/"your_user_name"/Downloads.

32-bit Oracle Java on 32-bit Ubuntu Linux installation instructions:
Type/Copy/Paste: cd /home/"your_user_name"/Downloads
Type/Copy/Paste: sudo cp -r jdk-7u40-linux-i586.tar.gz /usr/local/java
Type/Copy/Paste: sudo cp -r jre-7u40-linux-i586.tar.gz /usr/local/java
Type/Copy/Paste: cd /usr/local/java

64-bit Oracle Java on 64-bit Ubuntu Linux installation instructions:
Type/Copy/Paste: cd /home/"your_user_name"/Downloads
Type/Copy/Paste: sudo cp -r jdk-7u40-linux-x64.tar.gz /usr/local/java
Type/Copy/Paste: sudo cp -r jre-7u40-linux-x64.tar.gz /usr/local/java
Type/Copy/Paste: cd /usr/local/java

Unpack the compressed Java binaries, in the directory /usr/local/java
32-bit Oracle Java on 32-bit Ubuntu Linux installation instructions:
Type/Copy/Paste: sudo tar xvzf jdk-7u40-linux-i586.tar.gz
Type/Copy/Paste: sudo tar xvzf jre-7u40-linux-i586.tar.gz

64-bit Oracle Java on 64-bit Ubuntu Linux installation instructions:
Type/Copy/Paste: sudo tar xvzf jdk-7u40-linux-x64.tar.gz
Type/Copy/Paste: sudo tar xvzf jre-7u40-linux-x64.tar.gz

Double-check your directories. At this point, you should have two uncompressed binary directories in /usr/local/java for the Java JDK/JRE listed as:
Type/Copy/Paste: ls -a
jdk1.7.0_40
jre1.7.0_40

Edit the system PATH file /etc/profile and add the following system variables to your system path. Use vi, gedit or any other text editor, as root, open up /etc/profile.
Type/Copy/Paste: sudo gedit /etc/profile
or
Type/Copy/Paste: sudo vi /etc/profile

Scroll down to the end of the file using your arrow keys and add the following lines below to the end of your /etc/profile file:
Type/Copy/Paste:

export JAVA_HOME=/usr/local/java/jdk1.7.0_40
export PATH=$PATH:$JAVA_HOME/bin

Save the /etc/profile file and exit.

Reload your system wide PATH /etc/profile by typing the following command:
Type/Copy/Paste: . /etc/profile
Note your system-wide PATH /etc/profile file will reload after reboot of your Ubuntu Linux system

Test to see if Oracle Java was installed correctly on your system. Run the following commands and note the version of Java:

A successful installation of 32-bit Oracle Java will display:
Type/Copy/Paste: java -version
This command displays the version of java running on your system
You should receive a message which displays:
java version "1.7.0_40"
Java(TM) SE Runtime Environment (build 1.7.0_40-b40)
Java HotSpot(TM) Server VM (build 23.1-b03, mixed mode)
Type/Copy/Paste: javac -version
This command lets you know that you are now able to compile Java programs from the terminal.
You should receive a message which displays:
javac 1.7.0_40

A successful installation of Oracle Java 64-bit will display:
Type/Copy/Paste: java -version
This command displays the version of java running on your system
You should receive a message which displays:
java version "1.7.0_40"
Java(TM) SE Runtime Environment (build 1.7.0_40-b40)
Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)
Type/Copy/Paste: javac -version
This command lets you know that you are now able to compile Java programs from the terminal.
You should receive a message which displays:
javac 1.7.0_40

Congratulations, you just installed Oracle Java on your Linux system. Now reboot your Ubuntu Linux system. Afterwards, your system will be fully configured for running and developing Java programs. Later on you may want to try compiling and running your own Java programs by following this article How to Create your First Java Program on Ubuntu Linux


Quickly Enable Root Login in Ubuntu 12.10

Here’s a quick way to login as root in Ubuntu 12.10. This is not recommended as the root user account is disabled for a reason.
In a perfect world, no one should login with total administrator rights (root user) in Ubuntu. But since we’re not in a perfect world, this brief tutorial is going to show you how to quickly enable root login in Ubuntu 12.10
Objectives:
  • Login as Root (Administrator) in Ubuntu 12.10
  • Enjoy!
To get started, press Ctrl – Alt – T on your keyboard to open the terminal. When it opens, run the commands below to create a root password.
sudo passwd root
Next, run the commands below to enable root login in Ubuntu 12.10
sudo vi /etc/lightdm/lightdm.conf
add the below line end of the file.
greeter-show-manual-login=true
save the file and restart the system. In the next boot you can see login label in the login screen.

Friday, June 7, 2013

How many ways to create object in java

How many ways to create object in java?
There are different ways to create object in java as follows:--

1. Using new keyword
 we can create the instance normally using new operator which is called as static instance creation.
        Hello hello =  new Hello();

2.using Class.forName()
we can create the instance dynamically without using new operator as follow
Hello hello=(Hello)Class.forName("com.bikash.Hello").newInstance();
                                   or
 Class cls = Class.forName("com.bikash.Hello");
Hello hello = (Hello)cls.newInstance();

3.using clone().
clone() method can be used to copy of the existing object.
Hello hello=new Hello();
Hello hello1=(Hello)hello.clone();

4.using object deserialization.
deserializion is the process of creating the new object on the remote mechine from its serialize form.
ObjectInputStream ois =new ObjectInputStream();
Hello hello = (Hello)ois.readObject();

Donot forget to give your comments /suggestions and refer to your friends.

Monday, June 3, 2013

How to create and execute JAR file in Java – Command line Eclipse Netbeans

Creating JAR file in java from command prompt is always been little tricky for many of us even if IDE like Netbeans and Eclipse provide support to export java program as JAR file simply because we don’t create jar often and not familiar with manifest file or jar command as whole. JAR file in Java is a kind of zip file which holds all contents of a Java application including Class files, resources such as images, sound files and optional Manifest file. JAR stands for Java Archive and provides a platform independent deliverable for java programs, libraries and framework. you can execute same jar file in any operating system e.g. Windows 7, windows 8, Macintosh or Linux. Apart from platform independence and standard delivery method jar file also provides compression of contents which results in faster download if you are downloading java program from internet specially in case of mobile devices where you install Java program by OTA. In this article we will some JAR command examples and learn how to create and execute jar file, how to view contents of jar file from command prompt and Eclipse and Netbeans.

How to create jar file in Java form command prompt
jar command in Java allows you to create jar file from command prompt, what is required is that you must have jar command included in System PATH variable. you can check this by typing "jar" in command prompt if it doesn't throw error as "jar is not recognized as an internal or external command" they you are ready to go. When you create jar file in Java, command also creates Manifest file which is optional and you can control whether to create it or not by jar command line options, but if you want to create executable jar file they you must need Manifest file which we will discuss in further sections. Now here is jar command example to create jar file from command prompt, this will work both in windows and Linux operating system.

JAR command Examples in Java
C:\Users\SREE> jar -cvf HelloWorld.jar HelloWorld.class
added manifest
adding: HelloWorld.class(in = 450) (out= 311)(deflated 30%)

This command will crate Helloworld. jar which contains Helloworld.class file. this will also create manifest file but without Main-Class entry as shown below:

C:\Users\SREE> cat MANIFEST.MF
Manifest-Version: 1.0
Created-By: 1.6.0-beta2 (Sun Microsystems Inc.)

This jar can not be executed and you will get error when you try to run this jar file:

C:\Users\SREE> java -jar HelloWorld.jar
Failed to load Main-Class manifest attribute from HelloWorld.jar

You just need to provide Main-Class entry to get rid of this error which we will see in coming Section.

How to Create an executable JAR file in Java

To create an executable JAR in Java, you need to provide a manifest file and include your Main Class in Manifest. When you create jar file , jar command also creates manifest file inside META-INF as MANIFEST.MF but doesn't create Main-Class entry which is required for executable jar file. You can create executable jar file in Java by two ways either provide a self created Manifest file or specify entry point using "-e" jar option. If you provide external Manifest file than you need to use jar -m option to include that manifest file inside jar. Let's see example of both ways to create executable jar file in Java.

Executable JAR File Example with External Manifest

1.Create MANIFEST.MF file by using any text editor e.g. notepad in windows or Vim in Unix and add following entry in file, remember last line must end with either new line or carriage return:

Manifest-version: 1.0
Main-Class: HelloWorld

Important thing to remember is that we need to specified full classified class name here. suppose if our main class was inside com/example/HelloWorld than we should have to specify com.example.HelloWorld here, don't put .class extension here its not required. Apart from specifying Main-Class you can also specify Java Classpath in Manifest file which is important if your application is depended on external library jars. "Classpath" entry supersede both -cp and CLASSPATH environment variable. to learn more see How ClassPath works in Java.

2.Execute following jar command to create executable jar

C:\Users\SREE> jar -cvfm HelloWorld.jar MANIFEST.MF HelloWorld.class
added manifest
adding: HelloWorld.class(in = 450) (out= 311)(deflated 30%)

here -m is used for including manifest file and remember specify name of manifest file after jar name. now you have an executable jar file in java which you run by command specified earlier.

Creating Executable JAR File By entry point

This seems to me an easy way to create executable jars in Java, as you need not have to create manifest file explicitly and it will be create by jar command itself along with Main-Class entry. What you need to provide is a new jar option "-e" and you main class name while running jar command. here is example of jar command with entry option:

C:\Users\SREE> jar -cvfe HelloWorld.jar HelloWorld HelloWorld.class
added manifest
adding: HelloWorld.class(in = 450) (out= 311)(deflated 30%)

jar -e for entry point and entry point or main class name should come after jar file name and before directory or file needs to be included in JAR. You can now run your executable jar file by issuing "java -jar" command as shown in following example:

C:\Users\SREE> java -jar HelloWorld.jar
Executing Java Program from JAR file

How to execute Java Program from Jar file

Executing jar program from jar archive is very easy one thing required is jar must be executable and must have Main-Class entry in MANIFEST.MF file. here is a Java command example for running java program from jar file:

C:\Users\SREE> java -jar HelloWorld.jar
Executing Java Program from JAR file

here we have specified jar file name with -jar option and it will run main class declared as “Main-Class” attribute in manifest file.

How to view contents of a JAR file in Java

jar command in Java allows you to view files and directories inside of a jar file without extracting or unzipping original jar. "-t" jar option is used to list files from jar archive as shown in jar command example below:

C:\Users\SREE>jar -tvf Hello.jar
     0 Thu May 30 20:24:42 IST 2013 META-INF/
    95 Thu May 30 20:24:42 IST 2013 META-INF/MANIFEST.MF
   604 Thu May 30 20:20:10 IST 2013 HelloWorld.class

here "-t" for listing and "-v" and "-f" for verbose and jar file name.

How to extract contents of JAR File

use jar option "-v" for extracting files form JAR files as shown in jar command example below:

C:\Users\SREE> jar -xvf HelloWorld.jar
created: META-INF/
inflated: META-INF/MANIFEST.MF
inflated: HelloWorld.class

here -x for extracting , -v is for verbose and -f specify jar file name.

How to create jar file in Eclipse

Creating JAR file in  Eclipse IDE is a cakewalk once you know the process. here is step by step guide of creating JAR file from Eclipse IDE: In Jar file main class is specified as “Main-Class” attribute inside manifest file and used as program entry point if you double click on JAR or run jar from java command.

1) Select Project for which you want to create jar file.
2) Go to File Menu and select Export
3) Expand Java folder and select JAR file

Now you just need to click next and follow instruction as displayed. you can select what contents you want to export to jar file and specify Main Class entry as well. If you like Eclipse IDE then you may like my earlier post on eclipse as well e.g. Java debugging tips in Eclipse and  How to setup java remote debugging in Eclipse.


How to create jar file in Netbeans

In Netbeans to create jar file you need to build the project which execute project ant file and creates JAR file inside dist folder. You can go on properties of project and specify main class there which will be run when you run the project and same will be used to create “Main-Class” attribute in JAR file.

jar is not recognized as an internal or external command

if you get this error while executing jar command from command prompt in Windows or Unix it means your Java Path is not set properly. JAR command is a binary which resides in JDK_HOME/bin folder where JDK_HOME is JDK installation directory. In order to use jar command from command prompt this bin folder must be in your System's PATH variable. Don't worry if its not there in PATH you can check this link to Set PATH for Java in Windows and Unix.It shows how you can do it in both Windows and Unix. Once your PATH is property set, you will see following output when you execute jar command from command  line:

C:\Users\SREE> jar
Usage: jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
Options:
-c  create new archive
-t  list table of contents for archive

and now you are ready to use jar from command prompt.

WAR and EAR -  related JAR like fies in Java

WAR file
WAR file in Java stands for Web application archive and it is used to package a Java application together you can package all your Servlet, JSP, CSS, images, html in one WAR file and then deploy it to any Java web or application server like Tomcat, Weblogic or webshere. WAR files provide a clean and faster way to package and deploy Java web application just like JAR file provides for core java apps. Since WAR file also compacts resources inside it is comparatively download faster than downloading individual components.

EAR file
EAR file stands for Enterprise Java Archive and used to package an Enterprise Java application, like earlier WAR and JAR file. What separates EAR archive to WAR file is inclusion of Enterprise Java Beans(EJB). EAR file contains all web resources including Servlet, JSP, html, javascript, css, images along-with EJB. You can not deploy EAR files into web servers like Tomcat because it doesn't support EJB and can only be deploy-able in Application servers like WebSphere or Weblogic.

JAR File format in Java

Few words about java jar file format, its similar to zip format and use .jar extension. you can open JAR file in windows by using either winzip or winrar zip utilities.


That’s all on how to create jar file from command line, Eclipse, Netbeans. How to extract contents, how to run Java program from jar file etc. Let me know if you face any issue while creating JAR file in java.


Donot forget to give your comments /suggestions and refer to your friends.

Friday, May 3, 2013

How to create and extract a .tar.gz archive using command line


One of the most common compression formats used in GNU/Linux and variants is tar.gz. A tar.gz file is nothing but a gzipped tar archive. These days users of GNU/Linux system seldom have to use the command line to create or extract tar.gz archives. But it is a useful command to keep in your arsenal if you are a system administrator.

To create a tar.gz archive from a given folder you can use the following command

tar -zcvf tar-archive-name.tar.gz source-folder-name

This will compress the contents of source-folder-name to a tar.gz archive named tar-archive-name.tar.gz

To extract a tar.gz compressed archive you can use the following command

tar -zxvf tar-archive-name.tar.gz

This will extract the archive to the folder tar-archive-name.


Donot forget to give your comments /suggestions and refer to your friends.

Thursday, May 2, 2013

Resolve Applying iptables firewall rules failed


Some time we might be get error like Applying iptables firewall rules: iptables-restore: line 1 failed [FAILED].The line Number will be different.That is nothing  while editing editing iptables you may wrongly enter some extra attributes.
For example iptable rule will be like that

-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 21 -j ACCEPT

but you editing rule like

ss-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 21 -j ACCEPT

means you can get like Applying iptables firewall rules: iptables-restore: line 1[this the exact line failed] failed [FAILED].

For that you simply correct the rule and restart your iptables. And you away from that error.

If the iptable rule is correct and you getting same error means add the following line above the rules and add COMMIT statement end of the rule.Go to /etc/init.d/iptables and add

*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT

-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 21 -j ACCEPT----->rule
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 25 -j ACCEPT----->rule

COMMIT


and restart your iptables .That  working fine


Donot forget to give your comments /suggestions and refer to your friends.

Linux Basic Commands

1.  Everything in Linux is a file including the hardware and even the directories.

2. # : Denotes the super(root) user

3.  $ : Denotes the normal user

4.  /root: Denotes the super user’s directory

/home: Denotes the normal user’s directory.

5.  Switching between Terminals

§  Ctrl + Alt + F1-F6: Console login

§  Ctrl + Alt + F7: GUI login

6.  The Magic Tab: Instead of typing the whole filename if the unique pattern for a particular file is given then the remaining characters need not be typed and can be obtained automatically using the Tab button.

7.   ~(Tilde): Denotes the current user’s home directory

8.   Ctrl + Z: To stop a command that is working interactively without terminating it.

9.  Ctrl + C: To stop a command that is not responding. (Cancellation).

10.  Ctrl + D: To send the EOF( End of File) signal to a command normally when you see ‘>’.

11.  Ctrl + W: To erase the text you have entered a word at a time.

12.  Up arrow key: To redisplay the last executed command. The Down arrow key can be used to print the next command used after using the Up arrow key previously.

13.  The history command can be cleared using a simple option –c (clear).

14.  cd :   The cd command can be used trickily in the following ways:

cd : To switch to the home user

cd * : To change directory to the first file in the directory (only if the first file is a directory)

cd .. : To move back a folder

cd - : To return to the last directory you were in

15.  Files starting with a dot (.) are a hidden file.

16.   To view hidden files: ls -a

17.   ls: The ls command can be use trickily in the following ways:

ls -lR : To view a long list of all the files (which includes directories) and their subdirectories recursively .

ls *.* : To view a list of all the files with extensions only.

18.   ls -ll: Gives a long list in the following format

drwxr-xr-x 2 root root 4096 2010-04-29 05:17 bin where

drwxr-xr-x : permission where d stands for directory, rwx stands for owner privilege, r-x stands for the group privilege and r-x stands for others permission respectively.

Here r stands for read, w for write and x for executable.

2=> link count

root=>owner

root=>group

4096=> directory size

2010-04-29=>date of creation

05:17=> time of creation

bin=>directory file(in blue)



The color code of the files is as follows:

Blue: Directory file

White: Normal file

Green: Executable file

Yellow: Device file

Magenta: Picture file

Cyan: link file

Red: Compressed file

File Symbol

-(Hyphen) : Normal file

d=directory

l=link file

b=Block device file

c=character device file

19.  Using the rm command: When used without any option the rm command deletes the file or directory ( option -rf) without any warning. A simple mistake like rm / somedir instead of rm /somedir can cause major chaos and delete the entire content of the /(root) directory. Hence it is always advisable to use rm command with the -i(which prompts before removal) option. Also there is no undelete option in Linux.

20.  Copying hidden files: cp .* (copies hidden files only to a new destination)

21. dpkg -l : To get a list of all the installed packages.

23. Use of ‘ > ‘ and ‘ >> ‘ : The ‘ > ‘ symbol ( input redirector sign) can be used to add content to a file when used with the cat command. Whereas ‘ >> ‘ can be used to append to a file. If the ‘ >> ‘ symbol is not used and content is added to a file using only the ‘>’ symbol the previous content of the file is deleted and replaced with the new content.

e.g: $ touch text (creates an empty file)

$ cat >text

This is text’s text. ( Save the changes to the file using Ctrl +D)

$cat >> text

This is a new text. (Ctrl + D)

Output of the file:

This is text’s text.

This is a new text.



23.  To count the number of users logged in : who |wc –l



24.  cat:  The cat command can be used to trickly in the following way:

- To count no. of lines from a file : cat <filename> |wc -l

- To count no. of words from a file : cat <filename> |wc -w

- To count no. of characters from a file : cat <filename> |wc –c



25.  To search a term that returns a pattern: cat <filename> |grep [pattern]



26.  The ‘tr’ command: Used to translate the characters of a file.

tr ‘a-z’ ‘A-Z’ <text >text1 : The command for example is used to translate all the characters from lower case to upper case of the ‘text’ file and save the changes to a new file ‘text1'.

27.  File permission using chmod: ‘chmod’ can be used directly to change the file permission of files in a simple way by giving the permission for root, user and others in a numeric form where the numeric value are as follows:

r(read-only)=>4

w(write)=>2

x(executable)=>1

e.g. chmod 754 text will change the ownership of owner to read, write and executable, that of group to read and executable and that of others to read only of the text file.

28.  more: It is a filter for paging through text one screenful at a time.

Use it with any of the commands after the pipe symbol to increase readability.

e.g. ls -ll |more

29.  cron : Daemon to execute scheduled commands. Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates.

1 * * * * echo “hi” >/dev/tty1 displays the text “hi” after every 1 minute in tty1

.—————- minute (0 – 59)

| .————- hour (0 – 23)

| | .———- day of month (1 – 31)

| | | .——- month (1 – 12) OR jan,feb,mar,apr …

| | | | .—– day of week (0 – 7) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

* * * * * command to be executed

Source of example: Wikipedia

30.  fsck: Used for file system checking. On a non-journaling file system the fsck command can take a very long time to complete. Using it with the option -c displays a progress bar which doesn’t increase the speed but lets you know how long you still have to wait for the process to complete.

e.g. fsck -C

31.  To find the path of the command: which command

e.g. which clear

32. Setting up alias: Enables a replacement of a word with another string. It is mainly used for abbreviating a system command, or for adding default arguments to a regularly used command

e.g. alias cls=’clear’ => For buffer alias of clear

33.  The du (disk usage) command can be used with the option -h to print the space occupied in human readable form. More specifically it can be used with the summation option (-s).

e.g. du -sh /home summarizes the total disk usage by the home directory in human readable form.

34.  Two or more commands can be combined with the && operator. However the succeeding command is executed if and only if the previous one is true.

e.g. ls && date lists the contents of the directory first and then gives the system date.

35.  Surfing the net in text only mode from the terminal: elinks [URL]

e.g: elinks www.google.com

Note that the elinks package has to be installed in the system.

36.  The ps command displays a great more deal of information than the kill command does.

37.  To extract a no. of lines from a file:

e.g head -n 4 abc.c is used to extract the first 4 lines of the file abc.c

e.g tail -n 4 abc.c is used to extract the last 4 lines of the file abc.c

38.  Any changes to a file might cause loss of important data unknowingly. Hence    Linux creates a file with the same name followed by ~ (Tilde) sign without the recent changes. This comes in really handy when playing with the configuration files as some sort of a backup is created.

39.   A variable can be defined with an ‘=’ operator. Now a long block of text can be assigned to the variable and brought into use repeatedly by just typing the variable name preceded by a $ sign instead of writing the whole chunk of text again and again.

e.g ldir=/home/my/Desktop/abc

cp abcd $ldir copies the file abcd to /home/my/Desktop/abc.

40. To find all the files in your home directory modified or created today:

e.g. find ~ -type f -mtime 0

41. How to know 32-bit or 64-bit in linux?
A. getconf LONG_BIT
You can type the above command in terminal  press enter it will return 32 or 64 based on OS bit.


Donot forget to give your comments /suggestions and refer to your friends.

Saturday, April 27, 2013

In addition to the ability to execute Manager commands via HTTP requests. Tomcat includes a convenient set of Task definitions for the Ant (version 1.4 or later) build tool. In order to use these commands, you must perform the following setup operations:

  • Download the binary distribution of Ant from http://ant.apache.org. You must use version 1.4 or later.
  • Install the Ant distribution in a convenient directory (called ANT_HOME in the remainder of these instructions).
  • Copy the file server/lib/catalina-ant.jar from your Tomcat installation into Ant's library directory ($ANT_HOME/lib).
  • Add the $ANT_HOME/bin directory to your PATH environment variable.
  • Configure at least one username/password combination in your Tomcat user database that includes the manager-script role.
To use custom tasks within Ant, you must declare them first with a <taskdef> element. Therefore, your build.xml file might look something like this:
build.xml

<project name="MyApp" default="deploy" basedir=".">
<!-- loading properties file. -->
<property file="tomcatdeploy.properties"/>
<property name="src" location="${basedir}/src"/>
<property name="webcontent" location="${basedir}/WebContent"/>
<property name="lib" location="${webcontent}/WEB-INF/lib"/>
<!-- Configure the directory into which the web application is built -->
<property name="build" value="${basedir}/build"/>
<property name="war" location="${basedir}/war"/>
<!-- Configure the context path for this application -->
<property name="path" value="/myapp"/>
<!-- Configure properties to access the Manager application -->
<property name="url" value="${manager.url}"/>
<property name="username" value="${manager.username}"/>
<property name="password" value="${manager.password}"/>

<!-- Configure the custom Ant tasks for the Manager application -->
<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask"/>
<taskdef name="list" classname="org.apache.catalina.ant.ListTask"/>
<taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask"/>
<taskdef name="findleaks" classname="org.apache.catalina.ant.FindLeaksTask"/>
<taskdef name="resources" classname="org.apache.catalina.ant.ResourcesTask"/>
<taskdef name="start" classname="org.apache.catalina.ant.StartTask"/>
<taskdef name="stop" classname="org.apache.catalina.ant.StopTask"/>
<taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask"/>

<path id="libpath">
<fileset dir="${lib}">
<include name="*.jar"/>
</fileset>
</path>

<target name="clean" description="Deleting build and war directories">
<delete dir="${build}"/>
<delete dir="${war}"/>
</target>

<target name="init" depends="clean" description="Creating build and war directories">
<mkdir dir="${build}"/>
<mkdir dir="${war}"/>
</target>

<target name="compile" depends="init" description="Compile web application">
<copy todir="${build}/">
<fileset dir="${webcontent}"/>
</copy>
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}/WEB-INF/classes" classpathref="libpath" includeantruntime="true"/>
</target>

<target name="war" depends="compile" description="generate the distribution war" >
<jar jarfile="${war}${path}.war" basedir="${build}"/>
</target>

<target name="deploy" description="Install web application" depends="war">
<deploy url="${url}" username="${username}" password="${password}" path="${path}" war="file:${war}${path}.war"/>
</target>

<target name="reload" description="Reload web application">
<reload  url="${url}" username="${username}" password="${password}" path="${path}"/>
</target>

<target name="undeploy" description="Remove web application">
<undeploy url="${url}" username="${username}" password="${password}" path="${path}"/>
</target>

</project>

tomcatdeploy.properties


manager.url = http://localhost:8080/manager/text
manager.username = tomcatusername
manager.password = tomcatpassword



Donot forget to give your comments /suggestions and refer to your friends.

Sunday, April 21, 2013

Setting Global Environment Variables in CentOS


The easiest way to set an environment variable in CentOS is to use export as in

$> export JAVA_HOME=/usr/java/jdk.1.5.0_12

$> export PATH=$PATH:$JAVA_HOME

However, variables set in such a manner are transient i.e. they will disappear the moment you exit the shell. Obviously this is not helpful when setting environment variables that need to persist even when the system reboots.

In such cases, you need to set the variables within the system wide profile. In CentOS (I’m using v5.2), the folder /etc/profile.d/ is the recommended place to add customizations to the system profile.

For example, when installing the Sun JDK, you might need to set the JAVA_HOME and JRE_HOME environment variables. In this case:

Create a new file called java.sh
vim /etc/profile.d/java.sh

Within this file, initialize the necessary environment variables
export JRE_HOME=/usr/java/jdk1.5.0_12/jre
export PATH=$PATH:$JRE_HOME/bin

export JAVA_HOME=/usr/java/jdk1.5.0_12
export JAVA_PATH=$JAVA_HOME

export PATH=$PATH:$JAVA_HOME/bin

Now when you restart your machine, the environment variables within java.sh will be automatically initialized (checkout /etc/profile if you are curious how the files in /etc/profile.d/ are loaded) .

PS: If you want to load the environment variables within java.sh without having to restart the machine, you can use the source command as in:

$> source java.sh

Friday, March 15, 2013

VMPlayer The File is To Large


I recently bought a Buffalo LinkStation NAS drive to be used on my LAN. The idea being that all shared files to go there as also the VMs (I need lots of them for the work that I do!).
Installed the NAS drive, created shared areas, configured a DLNA server etc etc. All went fine. Now the moment of truth – would I be able to successfully run VMs that reside on NAS?
Setup was as follows:
  • Windows 7 Prof
  • VMWare Player 7.0.1
  • Buffalo LinkStation NAS
  • All connected through LAN
  • Launched one of the VMs by double clicking the .vmx file. All fine so far, VM Player kicks in and starts to load, but then the following error was thrown:
  • Here’s how to fix it:
  • Edit your .vmx file with your favorite text editor
  • Add a line to the end of this file:
  • diskLib.sparseMaxFileSizeCheck="FALSE"
  • Now fire up the VM again and hopefully it should work as normal!

    Donot forget to give your comments /suggestions and refer to your friends.

Monday, March 11, 2013

Sending SMTP mail to gmail using PHP

Sending mail from your web server is a very common thing that many of us need to do. To do this, the most common approach is to use the following PHP code:mail($to, $subject, $message, $headers);

      This method sends a copy of the email, directly to the recipients mail server from the web server. However generally speaking, the web server is not the real mail server for the domain you’re trying to send from which can cause major issues.For example, lets say I have a website at www.myweb.com and when someone registers it sends them an email from the address website@myweb.com. The recipients mail servers spam filtering facility may do an MX lookup on the myweb.com domain and notice that the real mail server for the myweb.com has a different IP address to the address where the email is originating from (the web servers IP address). For this reason web server emails are often flagged as spam and thus never received by the intended recipient.To avoid this, we can relay the mail through the domains real mail server using SMTP. Using this method the mail will come to the recipients mail server from the real mail server for the senders domain as seen in its MX records. This then avoids the problematic spam filter rule.To achieve this I use the Mail.php class as found in the PEAR::Mail library located at http://pear.php.net/package/Mail. If you’re using CentOS like me, you can easily install this package using the following 

command#yum install php-pear-Mail

In this example I’m connecting to a gmail server which requires the connection to be over SSL: 

<?php 

require_once('Mail.php');

$host = 'ssl://smtp.gmail.com';

$port = '465';

$username = '<your email address>';

$password = '<yourPassword>';

$subject = 'my subject';

$to = '<to email address>';

$from = $username;$message = 'test';

$headers = array ('From' => $from,'To' => $to,'Subject' =>subject);

$smtp = Mail::factory('smtp',

array ('host' => $host,'port' =>; $port,'auth' => true,'username' => $username,'password' => $password));

$mail = $smtp->send($to, $headers, $message);

if (PEAR::isError($mail)) {

  echo("<p>" . $mail->getMessage() . "</p>");

} else {

  echo("<p>Message successfully sent!</p>");

}

?>

If SSL wasn’t required I’d just need to remove the ‘ssl://’ from in front of the host declaration and change the port to 25 like this:

$host = 'mail.myweb.com';

$port = '25';

Obviously you’ll need to ensure that your firewall allows your web server to make connections to your smtp host over port 25 or 465 for SSL.


Donot forget to give your comments /suggestions and refer to your friends.