Thursday, August 25, 2011

Replacing a string recurcively in all files of a folder in Linux

Today I have come to a situation in which I have to search for a particular string in a Directory/Directories recurcively and if found, then have to replace with other string.
                  So here is simple one line command using for loop or you can also make a good script using the below command.

Go to directory where you have to search the particular and fire below command.

# for file in $(grep -rli *string to search* *);           
          sed -i 's/*string to search*/*String to be replaced*/g' $file; 

Note : * at end of grep is compulsory, but don't put with string you want to search. eg if you want search linux then write linux not *linux*. The * at end of grep command is astrerisk for searching all files.

Option with grep do following tasks..
-r : For recurcively search
-i : For all matches(whether small or capital)
-l : Stop after fisrt occurence

Option with sed do following tasks..
-i : edit the orginal file permanently
Note : if -e option with sed shows you result on screen and do not change the contents of file permanetly.

Tuesday, August 23, 2011

Secure WEb-Console and JMX-Console in Jboss

Hi All,
            Here I am posting my first video on How to Secure web-console and jmx-console in JBOSS 5.1GA.

Feel free to comment...

Setting PXE Network Boot Server in Linux

Dear All,
               Here I am showing you another way to configure PXE Network Boot Server on Linux. I have shown this in my previous post PXE(Preboot eXecution Environment) Installation and Configuration . This will be really helpful when you have no CD/DVD Drive in your Client System.

Requirements : You will need following thing to be configure for this...
  • A DHCP server
  • A TFTP server (tftpd-hpa)
  • vmlinuz and initrd.img from your distribution of choice
  • The PXELINUX.0 boot loader and a suitable config file
  • Network installation Server that may be NFS, FTP, or HTTP. Here I am using ftp server. 
  • system-config-netboot package for pxeos commands.
Note : My server IP --

1.) Install the TFTP server:
Ensure you use the tftpd-hpa package, as PXELINUX requires that the boot server has a TFTP server which supports the “tsize” TFTP option. If you don’t use tftpd-hpa you will most likely see an error such as ‘TFTP server does not support the tsize option’.
              You can make use of yum or can download it from net. I am showing you comtents of "/etc/xinetd.d/tftp" here
[root@server199 ~]# cat /etc/xinetd.d/tftp
# default: off
# description: The tftp server serves files using the trivial file transfer \
#       protocol.  The tftp protocol is often used to boot diskless \
#       workstations, download configuration files to network-aware printers, \
#       and to start the installation process for some operating systems.
service tftp
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args        = -s /tftpboot   [-- my Dir where I have configure ftp Server]
        disable                 = no
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4

2.) Install and configure dhcp server:
                        I am not going in detail of dhcp server just showing you contents of dhcpd.conf file.

[root@server199 ~]# cat /etc/dhcpd.conf
ddns-update-style interim;
ignore client-updates;

allow booting;
allow bootp;
class "pxeclients" {
      match if substring(option vendor-class-identifier, 0, 9) = "PXEClient";
      filename "linux-install/pxelinux.0";

subnet netmask {

# --- default gateway
        option routers        ;
        option subnet-mask    ;

#       option nis-domain               "";
#        option domain-name             "";
        option domain-name-servers;

        option time-offset              -18000; # Eastern Standard Time
#       option ntp-servers    ;
#       option netbios-name-servers;
# --- Selects point-to-point node (default is hybrid). Don't change this unless
# -- you understand Netbios very well
#       option netbios-node-type 2;

        range dynamic-bootp;
        default-lease-time 21600;
        max-lease-time 43200;


3.) Setting up PXELINUX:
PXELINUX is a SYSLINUX derivative, for booting Linux off a network server. Essentially, it is used to load a linux kernel of your choice on to your machine. You specify which kernel to load in a config file. You can download the latest pxelinux.0 file from You will need to extract it from the syslinux archive .
You are going to be creating a folder structure that will eventually look like this:

[root@server199 /]# mkdir -p /tftpboot/linux-install/
[root@server199 /]# mkdir -p /tftpboot/linux-install/pxelinux.cfg
[root@server199 /]# touch /tftpboot/linux-install/pxelinux.cfg/default
[root@server199 /]# mkdir -p /tftpboot/linux-install/distros
[root@server199 /]# mkdir -p /tftpboot/linux-install/distros/centos5.6
[root@server199 /]# vi /tftpboot/linux-install/pxelinux.cfg/default
[root@server199 pxelinux.cfg]# cat default
default local
timeout 100
prompt 1
display msgs/boot.msg
F1 msgs/boot.msg
F2 msgs/general.msg
F3 msgs/expert.msg
F4 msgs/param.msg
F5 msgs/rescue.msg
F7 msgs/snake.msg

label 0
  localboot 1

label 1
  kernel centos/vmlinuz
  append initrd=centos/initrd.img ramdisk_size=8419 method= ip=dhcp
[root@server199 pxelinux.cfg]#
[root@server199 pxelinux.cfg]#
[root@server199 pxelinux.cfg]# cat /tftpboot/linux-install/menu.msg

                          .-=-.          .--.
              __        .'     '.       /  " )
      _     .'  '.     /   .-.   \     /  .-'0c\0a
     ( \   / .-.  \   /   /   \   \   /  /    0c^0a
      \ `-` /   \  `-'   /     \   `-`  /
       `-.-`     '.____.'       `.____.'
  __ _ _ __   __ _  ___ ___  _ __   __| | __ _
 / _` | '_ \ / _` |/ __/ _ \| '_ \ / _` |/ _` |
| (_| | | | | (_| | (_| (_) | | | | (_| | (_| |
 \__,_|_| |_|\__,_|\___\___/|_| |_|\__,_|\__,_|


Choose one of the following labels in order to boot:
- linux (localboot)
- centos5.6

[root@server199 ~]# tree -d /tftpboot/
/tftpboot/  -----------Manual Created
`-- linux-install----------Manual Created
    |-- centos-----------
    |-- distros----------        Manual Created
    |   `-- centos5.6  ....Manual Created
    |       |-- CentOS
    |       |-- NOTES
    |       |-- images
    |       |   |-- pxeboot
    |       |   `-- xen
    |       |-- isolinux
    |       `-- repodata
    |-- msgs  ------------ Manual Created
    `-- pxelinux.cfg   --------  Manual Created

13 directories
[root@server199 linux-install]# tree msgs/
msgs/   --- Manual Created
`-- boot.msg   ---- Manual Created

0 directories, 1 file
[root@server199 linux-install]# pwd
[root@server199 linux-install]# ll
total 48
drwxr-xr-x 2 root root  4096 Aug 22 12:42 centos
drwxr-xr-x 3 root root  4096 Aug 22 11:36 distros
-rw-r--r-- 1 root root   620 Aug 22 11:41 menu.msg   
drwxr-xr-x 2 root root  4096 Aug 22 12:45 msgs
-rw-r--r-- 1 root root 26442 Aug 22 11:45 pxelinux.0
drwxr-xr-x 2 root root  4096 Aug 23 12:44 pxelinux.cfg
[root@server199 linux-install]#

4.) Setting up ftp Server :
                             Finally Setup ftp server. You can install vsftpd using yum or can download from net. I am just showing you my vsftpd.conf file.

[root@server199 ~]# cat /etc/vsftpd/vsftpd.conf
# Allow anonymous FTP?

Now copy the Centos5.6 DVD to location "/tftpboot/linux-install/distros".
[root@server199 distros]# ll
total 4 drwxr-xr-x 7 root root 4096 Aug 22 12:03 centos5.6
[root@server199 distros]# cd centos5.6/
[root@server199 centos5.6]# ls
CentOS      NOTES                  RELEASE-NOTES-en.html     RELEASE-NOTES-fr.html  RELEASE-NOTES-pt_BR.html  TRANS.TBL
EULA        RELEASE-NOTES-cs       RELEASE-NOTES-en_US       RELEASE-NOTES-ja       RELEASE-NOTES-ro          vmlinuz
images      RELEASE-NOTES-de       RELEASE-NOTES-es          RELEASE-NOTES-nl       repodata
initrd.img  RELEASE-NOTES-de.html  RELEASE-NOTES-es.html     RELEASE-NOTES-nl.html  RPM-GPG-KEY-beta
isolinux    RELEASE-NOTES-en       RELEASE-NOTES-fr          RELEASE-NOTES-pt_BR    RPM-GPG-KEY-CentOS-5

Finally restart the following services:

[root@server199 linux-install]# /etc/init.d/xinetd restart
Stopping xinetd:                                           [  OK  ]
Starting xinetd:                                           [  OK  ]
[root@server199 linux-install]# /etc/init.d/vsftpd restart
Shutting down vsftpd:                                      [  OK  ]
Starting vsftpd for vsftpd:                                [  OK  ]
[root@server199 linux-install]# /etc/init.d/dhcpd restart
Shutting down dhcpd:                                       [  OK  ]
Starting dhcpd:                                            [  OK  ]
[root@server199 linux-install]#

Last Step add "pxeos"  entry by issuing following command:

[root@server199 linux-install]# pxeos -a -i "Centos5.6" -p FTP -D 0 -s -L /centos5.6/ centos
[root@server199 linux-install]#

and then test it from client by changing boot order of client system and make PXE Boot as First boot device.

Feel free to comment....

Thursday, August 18, 2011

Basic Linux Commands Intervewer may Ask

1)Give me 15 commands which you use frequently
Depends on the environment you work. Some examples are
mkdir — For creating folders( use -p option to create multiple folders at a time)
ls –List folders/files( check what ls -1 do)
top — To monitor system activities
lsof –To check whats happening on the server and which process open which file.
netstat -tcp –Gives you complete picture about network connection details.
vnstat –Gives you Network band width statics
sh –For running shell scripts
history –For monitoring the commands executed by users
cd –For changing directories
vi --For editing configuration files.
chmod –To change permissions of folders and files.
mount –For mounting formated partitions.
service –For start/restart/stop a service.
chkconfig –For permanent on/off a service.
fdisk -l –To list all the partitions
This is my own list, you can have your list.
2)Give me some commands for user management.
last, chage, chsh, lsof, chown, chmod, useradd, userdel, newusers.
3)Give me syntax checking commands for following services
DNS, SAMBA, Apache etc
4)What is the command to do password less logins to other machines.
expect and ssh-keygen
5)Give me some security monitoring related commands.
lsof, netstat, top, ps -ef, tail, last, tcpdump, sestatus, history, w.
6)What is the difference between man, info, whatis commands and a –help option for a command?
whatis gives you one line answer.
–help option for a command gives you one line answers for each option supported by a command
man command gives you medium size info.
info command gives full details about a commands, lots and lots of information about a command.
Please share your interview questions with us, we post them here so that others can get help from this.

Friday, August 12, 2011

Removing Files in Linux having Name starting with special characters

Since on  Linux everything is to be considered as file, So a file may have any name that start with anything. So in case you got some file that has name starting with some unusual characters or you can say with special characters.

                         This post will show you about "How to Delete files whose name start with special characters in Linux".
e.g. files like --test2.

There are two ways to delete such files.
1.) Find inode of that particular file to be deleted and then delete using find command.

        Syntax:  ~]# find . -inum -exec rm -rf {} \;

2.) Second way is use rm command in following manner.

       Syntax: ~]# rm -rf ./

Now let me show you with Example. Here suppose I have a file having name ~test1. So for deleting this file....
Using find command :
                           [root@server199 ks]# ll -i 
                           total 0
                           16154646 -rw-r--r-- 1 root root 0 Aug 12 13:44--test
                           [root@server199 ks]# find . -inum 16154646 -exec rm -rf {} \;
                           [root@server199 ks]# ll -i
                           total 0

Using rm Command:
                            [root@server199 ks]# ll -i
                            total 0
                            16154646 -rw-r--r-- 1 root root 0 Aug 12 13:47 --test
                            [root@server199 ks]# rm -rf ./--test
                            [root@server199 ks]# ll -i
                            total 0

!Hope This will help You

Kuldeep Sharma

Tuesday, August 9, 2011

Network Bonding or Link Aggregation in Linux

Link aggregation or trunking or link bundling or Ethernet/network/NIC bonding or NIC teaming are computer networking umbrella terms to describe various methods of combining (aggregating) multiple network connections in parallel to increase throughput beyond what a single connection could sustain, and to provide redundancy in case one of the links fails.
 The Linux bonding driver provides a method for aggregating multiple network interfaces into a single logical bonded interface. The behavior of the bonded interfaces depends upon the mode; generally speaking, modes provide either hot standby or load balancing services. Additionally, link integrity monitoring may be performed.

Here I am showing you configuration for Centos, it will be same for others, may be some change in file location(depends on distro you are using).

[root@server199 ~]# cd /etc/sysconfig/network-scripts
[root@server199 network-scripts]#
[root@server199 network-scripts]# cat ifcfg-eth0
#Bonding eth0 to bond0
[root@server199 network-scripts]# cat ifcfg-eth1
# Bonding eth1 to bond0
[root@server199 network-scripts]#
Create a new file called ifcfg-bond0 and below line to it. I am showing contents of file.

[root@server199 network-scripts]# cat ifcfg-bond0

Now do some changes in /etc/modprobe.conf file by addition of following two lines.

alias bond0 bonding
options bond0 mode=balance-alb miimon=100

Next Restart network service

[root@server199 network-scripts]# service network reload
Shutting down interface bond0:                             [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Disabling IPv4 packet forwarding:  net.ipv4.ip_forward = 0
                                                           [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface bond0:                               [  OK  ]

[root@server199 network-scripts]# ifconfig
bond0     Link encap:Ethernet  HWaddr 00:1C:C0:F8:3E:A9 
          inet addr:  Bcast:  Mask:
          inet6 addr: fe80::21c:c0ff:fef8:3ea9/64 Scope:Link
          RX packets:19257 errors:0 dropped:0 overruns:0 frame:0
          TX packets:21223 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:11049157 (10.5 MiB)  TX bytes:2416427 (2.3 MiB)

eth0      Link encap:Ethernet  HWaddr 00:1C:C0:F8:3E:A9 
          RX packets:14580 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10688 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          RX bytes:9979659 (9.5 MiB)  TX bytes:1232395 (1.1 MiB)

eth1      Link encap:Ethernet  HWaddr 00:06:29:AF:C2:6C 
          RX packets:4677 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10535 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1069498 (1.0 MiB)  TX bytes:1184032 (1.1 MiB)

lo        Link encap:Local Loopback 
          inet addr:  Mask:
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:19508 errors:0 dropped:0 overruns:0 frame:0
          TX packets:19508 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:6175688 (5.8 MiB)  TX bytes:6175688 (5.8 MiB)

Hope it will Help
!Feel free to comment

Kuldeep Sharma

Friday, August 5, 2011

Linux Top Command

Linux top Command
                                                            In most Unix-like operating systems, the top command is a system monitor tool that produces a frequently-updated list of processes. By default, the processes are ordered by percentage of CPU usage, with only the "top" CPU consumers shown. top shows how much processing power and memory are being used, as well as other information about the running processes. Some versions of top allow extensive customization of the display, such as choice of columns or sorting method.
                                     top is useful for system administrators, as it shows which users and processes are consuming the most system resources at any given time.
The ps command is similar to top, but instead produces a one-time "snapshot" list of processes

top - 12:59:35 up  3:25,  8 users,  load average: 0.07, 0.17, 0.24
Tasks: 169 total,   2 running, 166 sleeping,   0 stopped,   1 zombie
Cpu(s):  0.7%us,  1.3%sy,  0.0%ni, 98.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   2037228k total,  1970468k used,    66760k free,     2572k buffers
Swap:  8193108k total,      188k used,  8192920k free,   176744k cached

 2579 root      18   0 1607m 627m 8716 S  1.3 31.6  17:10.36 java
 5732 root      15   0  650m  63m  16m S  1.0  3.2   2:57.50 VirtualBox
    1 root      15   0  2160  628  544 S  0.0  0.0   0:00.21 init
    2 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 migration/0
    3 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0
    4 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/0
    5 root      10  -5     0    0    0 S  0.0  0.0   0:00.01 events/0
    6 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 khelper
    7 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 kthread
   10 root      10  -5     0    0    0 S  0.0  0.0   0:00.05 kblockd/0
   11 root      20  -5     0    0    0 S  0.0  0.0   0:00.00 kacpid
  141 root      20  -5     0    0    0 S  0.0  0.0   0:00.00 cqueue/0
  144 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 khubd
  146 root      12  -5     0    0    0 S  0.0  0.0   0:00.00 kseriod
  207 root      22   0     0    0    0 S  0.0  0.0   0:00.00 khungtaskd
  208 root      15   0     0    0    0 S  0.0  0.0   0:00.06 pdflush
  209 root      15   0     0    0    0 S  0.0  0.0   0:00.03 pdflush
  210 root      10  -5     0    0    0 S  0.0  0.0   0:00.64 kswapd0
  211 root      20  -5     0    0    0 S  0.0  0.0   0:00.00 aio/0
  368 root      11  -5     0    0    0 S  0.0  0.0   0:00.00 kpsmoused
  391 root      10  -5     0    0    0 S  0.0  0.0   0:00.42 ata/0
  392 root      16  -5     0    0    0 S  0.0  0.0   0:00.00 ata_aux
  395 root      13  -5     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_0
  396 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_1
  397 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_2
  398 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_3
  401 root      10  -5     0    0    0 S  0.0  0.0   0:00.49 scsi_eh_4
  402 root      11  -5     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_5
  405 root      12  -5     0    0    0 S  0.0  0.0   0:00.00 kstriped
  414 root      16  -5     0    0    0 S  0.0  0.0   0:00.00 ksnapd
  423 root      10  -5     0    0    0 S  0.0  0.0   0:00.26 kjournald
  449 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 kauditd
  486 root      16  -4  2568  940  392 S  0.0  0.0   0:00.23 udevd
 1420 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 hd-audio0
 1953 root      20  -5     0    0    0 S  0.0  0.0   0:00.00 kmpathd/0
top command options:

Line1: Gives System present time, up time of the machine, number of users logged in, Load average on system at 5, 10, 15 min interval.
Line2: Gives total number of process on the machine, number of running process, number of sleeping process, number of stopped process, number of Zambie process.
Line3: Gives you CPU details
Line4 &  5: Gives RAM and SWAP details.
Line6: To execute top command shortcuts(See below for the list of top command shortcuts ).
From Line7: dynamically displayed top process results.

Linux top Command Shortcut
l --To display or to hide load average line

t --To display or to hide task/cpu line

1 --To display or hide all other CPU's

m --to display or to hide RAM and SWAP details

s --To change the time interval for updating top results(value is in sec's)

R --To sort by PID number

u -- Press u then username to get only that user process details

P --To sort by CPU utilization 

M --To sort by RAM utilization 

c --To display or hide command full path

r --To renice a process, press r then the PID no then the renice value to renice a process.

k --To kill a process, press k then PID number then enter to kill a process
W --To save the modified configuration permanently.
q --To quit the top command. 
h --for getting help on top command
space -- immediately refresh output

Kuldeep Sharma

Thursday, August 4, 2011

How to know java bit version(32bit or 64bit) in Linux

To know about the JVM bit version i.e. currently running on Linux Server, use following command:
#java -d64 -version
#java -d32 -version

If you have 32 bit JVM and you run above command i.e. java -d64 -version, then you'll get some error message on the console like ....

[root@localhost default]$  java -d64 -version
Running a 64-bit JVM is not supported on this platform.

And if you run command "java -d32 -version" , then you'll get....

[root@localhost default]$  java -d32 -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) Server VM (build 20.1-b02, mixed mode)

!Hope this will help you
Kuldeep Sharma

Getting status of all services in Linux

Below is the script to find out status of all services on linux system.

#Created on:04-08-2011
#Last modified:04-08-2011
#Purpose:To Check the status of services
#The below for loop will take inputs from chkconfig command to check the services which are running/stopped on the machine.
for i in `chkconfig --list | awk '{print $1}' | grep -v :`
/sbin/service $i status
there is one another way of doing this task is just use single command.

#service --status-all
[root@localhost ~]$  service --status-all
acpid (pid 2467) is running...
amd is stopped
anacron is stopped
arpwatch is stopped
atd (pid  2992) is running...
auditd (pid  2253) is running...
automount (pid 2660) is running...
Avahi daemon is running
Avahi DNS daemon is not running
bgpd is stopped
hcid (pid 2390) is running...
sdpd (pid 2396) is running...
capi not installed - No such file or directory (2)
clamav-milter (pid 2834) is running...


Kuldeep Sharma

Wednesday, August 3, 2011

Switching Back to yahoo Classic from Beta Version

Hello All,
         You know this is linux blog, But I am also adding some misc posts which I am finding some thing interesting. So Enjoy..

                  Recently I was facing some issue with yahoomail, As I don't want any more Yahoo Beta Version and want to switch back to yahoo classic version. But now they don't have keep any option to switch back to yahoo Classic, they have made Beta version mendatory :(.
    So after doing some R&D at last I was able to switch back. So here I am sharing the trick...
Note : All done on firefox.
1.) Firstly just disable your java script as
                                *Click on Tools then option and finally on Contents*, their uncheck Enable Java Script.
2.) Now restart firefox.
3.) Now when you open yahoomail, it will display message asking for enabling java or switching back to older version. So now you can switch back to yahoo older version :).
4.) After that again you can enable Java for firefox in same way as you have disabled.


Kuldeep Sharma