Wednesday, August 28, 2013

DELETE FILES OLDER THAN x DAYS IN LINUX.



Linux has find utility to delete the files in the specified folder older than x days where value of x is entered by the user.
The example below illustrates the implementation of this utility:

# find /path/to/files* -mtime +x -exec rm {} \;

In the example above ,

The first argument /path/to/files is the location of the folder from where the files are deleted.

The second -mtime specifies number of days old that the file is. For example, +x will find files older than x days.

The third argument – exec allows user  to pass in a command such as rm while {} \; at the end is required to end the command.
 
 
 
http://cbitss.com/6-months-industrial-training.html
 

Wednesday, August 21, 2013

RETRIVING PACKAGE INFORMATION


YUM (Yellow Updater,Modified) and rpm  (Red Hat Package Manager) are used as package managment tool. These are frequently 
used for management but comprise of less frequently  used options as follows which save the time of system administrator to 
retrieve system related information :

=> rpm can be used to find all the packages which are installed outside CentOS repositories , that is , third party repositiories as 
follows:

[root@PC1 ~]# rpm -qa --qf '%{NAME} %{VENDOR}\n' | grep -v CentOS
The sample output is:
flash-plugin Adobe Systems Inc.
crash-trace-command Fujitsu Limited
adobe-release-x86_64 Adobe Systems Inc.
gpg-pubkey (none)
gpg-pubkey (none)

=> We can quickly view the documentation related to a package using either of the two commands as below:
rpm -qd <packagename>
 OR
rpm -qdf /path/to/file
The    first command gives the documentation present in  the rpm  while second option gives the documentation in the package 
which owns the file.For example:
 [root@PC1 ~]# rpm -qd pinentry-0.7.6-6.el6.x86_64
/usr/share/doc/pinentry-0.7.6/AUTHORS
/usr/share/doc/pinentry-0.7.6/COPYING
/usr/share/doc/pinentry-0.7.6/ChangeLog
/usr/share/doc/pinentry-0.7.6/NEWS
/usr/share/doc/pinentry-0.7.6/README
/usr/share/doc/pinentry-0.7.6/THANKS
/usr/share/doc/pinentry-0.7.6/TODO
/usr/share/info/pinentry.info.gz

=> The       following command is used to list the file stored inside the rpm file
# rpm -qpl file.rpm
For        example,
  [root@PC1 ~]# rpm -qpl Downloads/adobe-release-x86_64-1.0-1.noarch.rpm 
/etc/pki/rpm-gpg/RPM-GPG-KEY-adobe-linux
/etc/yum.repos.d/adobe-linux-x86_64.repo
=> We can also list the files in the installed package using following command:
# rpm -ql packageName
For example,
 [root@PC1 ~]#rpm -ql httpd
/etc/httpd
/etc/httpd/conf
/etc/httpd/conf.d
/etc/httpd/conf.d/README
/etc/httpd/conf.d/welcome.conf


Friday, August 16, 2013

NETSTAT COMMAND FOR QUICK NETWORK RELATED INFORMATION


Netstat is a command which displays detailed information about system's mode of communication in a network. This 
command has advantage of displaying very  detailed information about network including network connections , 
protocol-specific networking statics, and overall network which is helpful in troubleshooting network related issues.



Nestat command when used with -a flag displays the state of all sockets used by system processes.

[root@cbitss ~]# netstat -a  | more 
Active Internet connections (servers and established) 
Proto Recv-Q Send-Q Local Address               Foreign Address  State      
tcp        0      0 *:rquotad                   *:*                         LISTEN      
tcp        0      0 *:38924                     *:*                         LISTEN      
tcp        0      0 *:59853                     *:*                         LISTEN      
tcp        0      0 *:sunrpc                    *:*                         LISTEN      
tcp        0      0 *:37589                     *:*                         LISTEN      

In the above example netstat command is piped with more pagination to display result page by page.
All tcp ports can be listed as follows:


User can also find the Kernel interface table which is similar to ifconfig command by using -ie flag with netstat as follows:

[root@dhcppc15 ~]# netstat -ie 

The sample output of above command is as follows:
Kernel Interface table 
eth0      Link encap:Ethernet  HWaddr 00:E0:E8:66:44:11  
          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:20 Base address:0xc000 

eth1      Link encap:Ethernet  HWaddr 00:1D:7D:80:18:1B  
          inet addr:192.168.1.17  Bcast:192.168.1.255  Mask:255.255.255.0 
          inet6 addr: fe80::21d:7dff:fe80:181b/64 Scope:Link 
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1 
          RX packets:34628 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:26093 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1000 
          RX bytes:37288056 (35.5 MiB)  TX bytes:2928416 (2.7 MiB) 

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:16 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:16 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:0 
          RX bytes:960 (960.0 b)  TX bytes:960 (960.0 b)
 
http://cbitss.in/
 

Thursday, August 15, 2013

NETSTAT COMMAND FOR QUICK NETWORK RELATED INFORMATION


All ports can be listed using following command:
[root@cbitss ~]# netstat -a  | more 
Active Internet connections (servers and established) 
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 *:rquotad                   *:*                         LISTEN      
tcp        0      0 *:38924                     *:*                         LISTEN      
tcp        0      0 *:59853                     *:*                         LISTEN      
tcp        0      0 *:sunrpc                    *:*                         LISTEN      
tcp        0      0 *:37589                     *:*                         LISTEN      

All tcp ports can be listed as follows:
[root@cbitss ~]# netstat -at 
Active Internet connections (servers and established) 
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 *:rquotad                   *:*                         LISTEN      
tcp        0      0 *:38924                     *:*                         LISTEN      
tcp        0      0 *:59853                     *:*                         LISTEN      
tcp        0      0 *:sunrpc                    *:*                         LISTEN      
tcp        0      0 *:37589                     *:*                         LISTEN      
tcp        0      0 *:ssh                       *:*                         LISTEN      
tcp        0      0 localhost:ipp               *:*                         LISTEN      
tcp        0      0 localhost:smtp              *:*                         LISTEN      
tcp        0      0 *:34394                     *:*                         LISTEN      
tcp        0      0 *:nfs                       *:*                         LISTEN      
tcp        0      0 *:962                       *:*                         LISTEN      
tcp        0      0 *:36034                     *:*                         LISTEN      
tcp        0      0 *:36103                     *:*                         LISTEN      
tcp        0      0 192.168.1.28:45021          del01s06-in-f22.1e100:https ESTABLISHED 
tcp        1      0 192.168.1.28:38722          a96-17-181-51.deploy.a:http CLOSE_WAIT  
tcp        0      0 *:40170                     *:*                         LISTEN      
tcp        0      0 *:54990                     *:*                         LISTEN      
tcp        0      0 *:sunrpc                    *:*                         LISTEN
http://cbitss.in/

Wednesday, August 14, 2013

SHELL SCRIPT TO REDIECT ALL STDOUT FROM SUBSHELL.


Here is a simple interactive shell script to redirect all stdout from the subshell.For example if the user wants to 
redirect contents of files entered to the file in temporary folder /tmp/sh, it can be done as follows :


Vim Script_Name.sh
:i
#!bin/bash
echo -n "Enter the file whose content is to be redirected using subshell: "

read file_path 
#enter the path of the file whose data is to be redirected.Multiple files can be given.

(

cat $file_path

)>/tmp/sh 
#The data is redirected to temporary folder.
:wq

The above script can be executed directly as follows:
# bash Script_Name.sh
  OR
#  sh Script_Name.sh

The script can also be executed  by giving execute permission as follows:
# chmod +x Script_Name
# ./Script_Name

Tuesday, August 13, 2013

USER SPECIFIC BASH PROMPT CONFIGURATION


The bash prompt can be customized in numberous ways depending on user specific customization or sytem wide configuration.

USER SPECIFIC CUSTOMIZATION :
For user specific configuration we edit ~/.bashrc and add customization below the line “# User specific aliases and functions”. 
For example the sample `/.bashrc for user named test is as follows:

# .bashrc


# Source global definitions

if [ -f /etc/bashrc ]; then

        . /etc/bashrc

fi


# User specific aliases and functions

PS1='\u@\H:\w\$ '




In the above example the prompt will have user_name@hostname.domain.tld:/working/directory$ sequence yielding following 
output:

test@cbitss:~$


Users can also implement colourful prompt using following syntax :
 
PS1='\[\033[02;32m\]\u@\H:\[\033[02;34m\]\w\$\[\033[00m\] '

Above Prompt Setter will give colorful prompt as :
test@cbitss:~$ 


Bash supports a range of colour as listed below:
Black             0;30     Dark Gray     1;30
Blue               0;34     Light Blue    1;34
Green             0;32     Light Green   1;32
Cyan              0;36     Light Cyan    1;36
Red                0;31     Light Red     1;31
Purple            0;35     Light Purple  1;35
Brown            0;33     Yellow        1;33
Light Gray      0;37     White         1;37

'h' is used instead of 'H' to display hostname only and not complete url.

System-wide Configuration

To implement prompt setting system-wide the changes are made in /etc/bashrc file as follows :
36 #[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "

 
Comment 36 line number.

For example we can add terminal in which user is logged in bash prompt by uncommenting following lines in /etc/bashrc file :
40               if [ "$PS1" ]; then

41                        PS1="[\u@\h:\l \W]\\$ "

42               fi


The above gives bash prompt as follows:
[root@cbitss:tty1 Desktop]#

Thus, user can change bash prompt as per the requirement.

Monday, August 12, 2013

Cloud Computing

Cloud computing is Internet based computing.In cloud computing virtual shared servers provide software, infrastructure, platform,devices,hosting and other resources to customers on a pay-as-you-use basis.
In cloud computing all information that digitized system has to offer is provided as a service.User can access these services on Internet cloud.
There are four different kind of deployment models of cloud  computing:-
1.Public Cloud
2.Community Cloud
3.Hybrid Cloud
4.Private Cloud
Benefits of Cloud computing:-

*Allows the enterprise to focus on its core business
*Increased competitive advantage
*Easy to maintain as they don't have to be installed on each user's computer

Sunday, August 11, 2013

Post of load balancing

 Load balancing is especially important for networks where it's difficult to predict the number of requests that will be issued to a server.
Distributing processing and communications activity evenly across a computer network so that no single device is overwhelmed..Busy web sites typically employ two or more web servers in a load balancing scheme. If one server starts to get swamped, requests are forwarded to another server with more capacity. Load balancing can also refer to the communications channels themselves.
Load balancing is usually provided by dedicated software or hardware, such as a multilayer switch/domain name system server process.Lately, some load balancers have evolved to support databases; these are called database load balancers.
For Internet services, the load balancer is usually a software program that is listening on the port where external clients connect to access services.
The term load balancing refers to distributing incoming HTTP requests across Web servers in a server farm, to avoid overloading any one server because load balancing distributes the requests based on the actual load at each server, it is excellent for ensuring availability and defending against denial of service attacks.

Friday, August 9, 2013

SHELL SCRIPT TO CONCATENATE TWO STRINGS AND DISPLAY LENGHT OF NEW STRING.



Here is a simple interactive shell script to display concatenation of two strings entered by the user and display  the length the        resultant string.

Vim Script_Name.sh
:i
#!bin/bash
echo -n "Enter the file path : "
#Display user the message to enter file path.
read file_path
#Input the path.
echo "The fourth and fifth line of file are as follows:"
     
cat $file_path | head -5 | tail -2
#Display fourth and fifth line.
:wq

The above script can be executed directly as follows:
# bash Script_Name.sh
  OR
#  sh Script_Name.sh

The script can also be executed  by giving execute permission as follows:
# chmod +x Script_Name
# ./Script_Name

Thursday, August 8, 2013

SHELL SCRIPT TO CONCATENATE TWO STRINGS AND DISPLAY LENGTH OF NEW STRING.

Here is a simple interactive shell script to display concatenation of two strings entered by the user and display the length the resultant string.

Vim Script_Name.sh
:i
#!bin/bash
echo -n "Enter  the first string: "                                              
read  str1                                                                   # input the first string.
echo -n "Enter the second string: "
read str2                                                                    # input the second string
str3=$str1$str2                                                         # concatenate   the above two input strings.
len=`echo $str1 | wc -c`                                         # find the length of first  input string.
len1=`echo $str2 |wc -c`                                       # find the length of second input string.
len2=`echo $str3 | wc -c`                                     # find the length of concatenated string.
echo "The length of first string is : `expr $len – 1`"            # display the length of first string.
echo "The length of second string is : `expr $len1 – 1`"   # display the length of second string.
echo "THe lenght of concatenated string is : `expr $len2 – 1`" # display the length of concatenated string.
:wq

The above script can be executed directly as follows:
# bash Script_Name.sh
  OR
#  sh Script_Name.sh

The script can also be executed  by giving execute permission as follows:
# chmod +x Script_Name
# ./Script_Name

Tuesday, August 6, 2013

HOW TO GIVE USER-BASED AUTHENTICATION TO WEBSITE IN APACHE SERVER



The htpasswd is the command used to give password to the various users who try to access a particular webpage in Apache 
Web Server.The following commands are executed to provide the user-based authentication:

 # htpasswd -cm /etc/httpd/conf/.htpasswd User_Name

Change the directory to http configuration file where the above file (.htpasswd) is created.
 # cd /etc/httpd/conf

Check the content of the file created using following command which will  show the encrpyted password:
 # ls -a

the above command shows the hidden files.

Next create the access file in the directory where webpages are  placed as follows:
 # vim /var/www/html/WebPage_Dir/.htaccess

Make the following entries in the above file :
 :i
 AuthType Basic  #AuthType directive selects that method that is used to authenticate the user. The most common method is 
Basic AuthName “Server Authentication”    #This directive sets the Realm to be used in authentication which is presentd to user 
as part of password dialog box as well as client uses it to determine what password to send for given authenticated area.
 AuthUserFile  /etc/httpd/conf/.htpasswd       #AuthUserFile directive sets the name of a textual file containing the list of users 
and passwords for user authentication.
 Require valid-user           #Grant access to all successfully authenticated users.
 :wq

Finally, to implement the changes make the following changes in th configuration file of apache server which is 
/etc/http/conf/httpd.conf:
 # vim /etc/httpd/conf/httpd.conf
 :i
 <directory /var/www/html/WePage_Dir>
  AllowOverride Authconfig
 </directory>

Now restart the service daemon and access the site from the client-end.
 # service httpd restart

HOW TO SET YUM-PLUGIN-PRIORITY IN CENTOS 6

The yum-plugin-priority associate a priority to the repositories so that packagesfrom repositories with lower priority will not be used to upgrade packages which were initially installed from higher priority repository. The users can also set the priorities to the repositories so that the packages are installed from higher priority repository if present in multiple repository.This plugin help user to update sytem from repositories which help in maintaining the functioning of CentOS.

To enable the priority-plugin the option of plugin should be enabled in /etc/yum.conf file as follows:

# vim /etc/yum.conf
     [main]

     cachedir=/var/cache/yum/$basearch/$releasever

     keepcache=0

     debuglevel=2

     logfile=/var/log/yum.log

     exactarch=1

     obsoletes=1

     gpgcheck=1

     plugins=1

     installonly_limit=5

     distroverpkg=centos-release
  


The priorities plugin is available at two locations:
By default enabled CentOS Extras Repository where yum-plugin-priority can be installed using following command :

  # yum install yum-plugin-priorities
 
yum-utils sources which has README file with instructions to install the plugin in the archieve directory.


Once the package is installed it should also be enabled in the /etc/yum/pluginconf.d/priorities.conf :

  [main]

  enabled = 1




Following the above steps enables the plugin.Now we can add priorities to the repositories by appending following line
  
 priority=N

to the targeted repository,where N is integer ranging from 1 to 99. Repositories with the lowest numerical value have the highest 
priority. It is best practice to give CentOS base and update repositories very high priority.
The default priority for repositories is 99.

Monday, August 5, 2013

HOW TO REVERSE A NUMBER


Here is a simple shell script to reverse the digits of the number entered by the user.

echo "Enter the number to be reversed where the first digit should have range from 1-9"   #prompt the message to enter the 
number such that first digit is not zero
read number                                                       #enter the number
var=${#number}                                               #find the number of the digits in the  number                                                           
for((i=1;i<=$var;i++))                                       # initiate the loop
do
var1=$((number%10))                                        #find the last digit of the number
echo -n "$var1"                                                 #display the last digit such that output should not trail to the new line.
number=$((number/10))                                  #remove the last digit     
done                                                                    #terminate the loop
echo                                                                  #change to new line


The above script is simple technique to reverse any number entered by the user.
Note : The script can be executed by giving execute permission as below:
                     # chmod +x Script_Name
        # ./Script_Name
                         OR 
Executing the script directly as follows:
                      # bash Script_Name
                             OR
                     #  sh Script_Name

HOW TO READ FILE WORD BY WORD


Here is a simple script which reads and display each word of the file separated by a blank space whose path is entered by 
the user. The IFS(Internal File Separator) of the file is blank space in other case it should be changed accordingly.

echo -n "Enter the path to file whose content is to be read line by line :  "
#ask user to enter the path of the file whose data is to be read word by word
read file_path
#input the path
for i in `cat $file_path`
#start the loop 
do
echo $i
#display each word of file separated by blank space
done
#terminate the loop


Note: The above script can be executed by giving it execute permission as follows:
 #chmod +x Script_Name
 #./Script_Name
   OR
Directly executing the script as follows:
 #bash Script_name
 #sh Script_Name

Friday, August 2, 2013

HOW TO MOUNT NTFS PARTITIONS IN LINUX(CENTOS6.4)

Ntfs-3g is GPL licensed,cross-palteform and read/write driver used in Linux which  is provides safe handling of files in 
NTFS format. 
The basic requisite is to install the package which facilitate the mounting of nts partition in linux.This utility is provided by ntfs-3g 
package which are available    in EPEL Repository.
 # yum -y install ntfs-3g
The EPEL Repository for CentOS6.4 can be downloaded using following steps.
#wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
#rpm -ivh epel-release-6*.rpm
# yum install fuse
With FUSE it is possible to implement a fully functional filesystem in a userspace program.  Features include:
  • Simple library API
  • Simple installation (no need to patch or recompile the kernel)
  • Secure implementation
  • Userspace - kernel interface is very efficient
  • Usable by non privileged users
  • Runs on Linux kernels 2.4.X and 2.6.X
  • Has proven very stable over time


Install and load FUSE driver to mount detected devices
# modprobe fuse
Next,make the directory where the partition is to be mounted.
# mkdir /mnt/ntfs_partition
Temporary mounting can be done by following command.
# mount -t ntfs-3g /dev/sda1 /mnt/ntfs_partition
For permanent mounting make the following entry in /etc/fstab file:
/dev/sdx    /mnt/usb    ntfs-3g        defaults    0    0

where /dev/sdx is hard disk partition with NTFS file system.
Thus, the data can now be accessed by changing directory where partition is mounted.

Thursday, August 1, 2013

HOW TO READ A FILE LINE BY LINE USING SHELL SCRIPT


A simlpe shell script to read any file line by line 
where the name of file is passed as the argument:
~]# touch navneet.sh
~]# chmod 755 navneet.sh
~]# vi navneet.sh
 
 file_name=$1                                         #positional parameter $1 is passed as the argument to the script
flag=0                                                       #initialize the variable read the file
cat $file_name | while read LINE       #pipe the content of file to while loop
do  
let flag++                                                #post increment of flag counter
echo "$flag $LINE"                              #display each line with line number
done                                                      #termination of loop 
esc:wq                                                  #save file

The above script displays the content of the file name entered by line.