Archive for the ‘Linux And Unix’ Category

Some useful Linux Command for DBA

August 24, 2010 Leave a comment
ls: List files
cp: Copy files
mv: Move and rename files
mkdir: Make a directory
alias: Define command macros
rm: Remove files and directories
more: Page through output
head: Show beginning of file contents
tail: Show end of file contents
df: Display filesystem space usage
du: Display directory disk space usage
cat: Show and concatenate files
grep: Search for patterns in files
chmod: Change permissions of files
chown: Change owner of files
zip: Compress and package files together
gedit: A WYSIWYG text editor
export: Make environment settings global
ps: List running processes
touch: Change file time stamps
id: Show information about the current user
sudo: Execute commands as another user

Standard Measurement Tools

•    Top Resource Consumers: top
•    System Activity Reporter: sar
•    Virtual Memory Statistics: vmstat
•    I/O Statistics: iostat
•    System Log files: /var/log/messages

Linux Tools

•    X-based tools: xosview
•    The /proc virtual file system
•    Free and used memory: free

Tools for monitoring and tuning CPU include:

•    top
•    pstree and free
•    vmstat
•    Syntax: vmstat <interval> <count>
•    Example : # vmstat 2 5
•    mpstat –p All
•    sar –u
•    Syntax: #sar -B <frequency> <count>
#sar -R <frequency> <count>
•    Example : #sar -B 2 3
#sar -R 2 3
•    xosview
•    xload
•    System Monitor

Measuring Total Memory

•    top
•    free
•    cat /proc/meminfo

Monitoring and Tuning I/O

•    /proc file system
•    sar -d
•    I/O statistics by device [iostat –d]
Syntax : iostat -d <interval> <count>
Eample : #iostat -d 2 2
•    I/O activity by partition
iostat –d -p <interval> <count>
•    vmstat
•    xosview


scp – Linux command line tool to copy files over ssh

December 24, 2009 Leave a comment

scp stands for secure cp (copy), which means that you can copy files across an ssh connection that will be encrypted, and therefore secured.

You can this way copy files from or to a remote server, you can even copy files from one remote server to another remote server, without passing through your PC.

scp [[user@]from-host:]source-file [[user@]to-host:][destination-file]

Description of options

from-host: Is the name or IP of the host where the source file is, this can be omitted if the from-host is the host where you are actually issuing the command

user: Is the user which have the right to access the file and directory that is supposed to be copied in the cas of the from-host and the user who has the rights to write in the to-host

source-file:Is the file or files that are going to be copied to the destination host, it can be a directory but in that case you need to specify the -r option to copy the contents of the directory

destination-file:Is the name that the copied file is going to take in the to-host, if none is given all copied files are going to maintain its names


-p  Preserves the modification and access times,
    as well as the permissions of the source-file in the destination-file
-q  Do not display the progress bar
-r  Recursive, so it copies the contents of the
    source-file (directory in this case) recursively
-v  Displays debugging messages


[oracle@testdb]$ scp java.tar.gz tamim@
The authenticity of host ' (' can't be established.
RSA key fingerprint is 23:b9:a4:b9:93:99:28:1f:4c:08:fa:8a:5f:d7:10:d0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '' (RSA) to the list of known hosts.
tamim@'s password:
java.tar.gz                                   100%   35MB  11.7MB/s   00:03

File is successfully transfer to the host in home/tamim directory

To copy a directory user scp –r


To use this command you need to have open-ssh installed in the hosts.

Categories: SCP - Linux command Tags:

Run Script in Linux as a Oracle user

December 22, 2009 Leave a comment

To run script as a oracle user we have to use su command which means change user ID or become super-user


su [ - ] [ username [ arg ] ]

Pass the environment along unchanged, as if the user actually logged in as the specified user.

username The name of another username that you wish to log in as.

arg Additional arguments that need to be passed through the su command.

To run a script create_user.sql in oracle user use the following script

su - oracle -c $ORACLE_HOME/bin/sqlplus -s <<!
/ as sysdba

To run some Java console base application from rc.local as a Oracle User

[oracle@www ~]$ vi /etc/rc.local
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
su - oracle /home/oracle/

Write a script to run a jar file as a demon process (use &), you can also run a class file in the same way.

[oracle@www ~]$ vi
export JAVA_HOME
export PATH=$PATH:$JAVA_HOME/bin
java -jar /home/oracle/AktelCMP.jar &

N.B: You can also run pearl or any other application in the same way.

How to take file backup from Linux Server

December 22, 2009 Leave a comment

This article describes a simple backup method that I use every day to backup my home Linux systems. It’s an easy method that non-technical Linux users can use to backup their important data. We’ll discuss the decisions you have to make in order to do a thorough backup.

What data and file should backup?

The most important files are least here:

  • System settings — Many people never touch their system settings — the settings are created during Linux installation and stay that way. For those people, backing up system settings is less crucial than backing up their personal settings, since a re-installation would fix things. For people who customize their systems — e.g., changing system configuration files in /etc — backing up these settings can be at least as important as backing up personal settings.
  • Installed software (and everything else) — This category includes installed system software (primarily Linux) and application software (such as OpenOffice, Firefox, and the Apache Web server). Such software can usually be restored by reinstalling, but not always.
  • Your files — This includes documents, spreadsheets, email, calendar data, financial data, downloaded music — anything that you’ve created, recorded or received that has meaning and importance to you. These are clearly the most important and hardest to recreate, because you or others created them from imagination and hard work or because you paid for them.
  • Your settings — This includes changes you’ve made to personal settings: desktop configuration (e.g., colors, backgrounds, screen resolution, mouse settings, locale) and program options, such as settings for OpenOffice, Gimp, your music player, and your email program. These are easier to recreate than your documents, but you’d hate to lose them — it takes time to recreate them.


tar [[-]function] [options] filenames...
tar [[-]function] [options] -C directory-name...

Command-line arguments that specify files to add to, extract from,or list from an archive may  be given as shell pattern matching strings.

Backing up with tar:

c Create a new archive.
t List the contents of an archive.
x Extract the contents of an archive.
f The archive file name is given on the command line
  (required whenever the tar output is going to a file)
M The archive can span multiple floppies.
v Print verbose output (list file names as they are processed).
u Add files to the archive if they are newer than the copy in the tar file.
z Compress or decompress files automatically.

A “.tar” file is not a compressed files, it is actually a collection of files within a single file uncompressed. If the file is a .tar.gz (“tarball“) or “.tgz” file it is a collection of files that is compressed. If you are looking to compress a file you would create the tar file then gzip the file.

Creating a tar file:

[root@vasappserver1 tamim]# tar -cvvf  rc.local.tar rc.local
-rwxr-xr-x root/root       419 2009-12-22 13:15:38 rc.local
[root@vasappserver1 tamim]# ls -ls
total 1932
   4 -rwxr-xr-x    1 root     root          419 Dec 22 13:15 rc.local
  12 -rw-r--r--    1 root     root        10240 Dec 22 13:16 rc.local.tar

In the above example command the system would create a tar file named TamimDataBackup.tar in the directory you currently are in of the home directory.

[root@vasappserver1 tamim]# tar -cvvf  TamimDataBackup.tar  /home/tamim
[root@vasappserver1 tamim]# ls -ls
total 17584
    4 -rwxr-xr-x    1 root     root          419 Dec 22 13:15 rc.local
  184 -rw-r--r--    1 root     root       184320 Dec 22 13:18 rc.local.tar
 5164 -rw-r--r--    1 root     root      5273600 Dec 22 13:18 TamimDataBackup
10316 -rw-r--r--    1 root     root     10547200 Dec 22 13:19 TamimDataBackup.tar

Extracting the files from a tar file:

[root@vasappserver1 tamim]# tar -xvvf  TamimDataBackup.tar
[root@vasappserver1 tamim]# ls -la
-rw-r--r--    1 root     root      5273600 Dec 22 13:18 TamimDataBackup
-rw-r--r--    1 root     root     10547200 Dec 22 13:19 TamimDataBackup.tar
[root@vasappserver1 tamim]# tar -xvvzf TamimDataBackup.tar.gz

Note: There is no “untar” linux / unix command.

Creating a tarred file that is compressed with bzip

[root@vasappserver1 tamim]# tar -cjvf backup.tbz home/

Adding the j option to the tar command enables tar to compress files and/or directories using bzip. In the above example the home directory and all its subdirectories are added to the compressed backup.tbz file.

Take full backup:

The following command will perform a backup of your entire Linux system onto the “/archive/” file system, with the exception of the “/proc/” pseudo-filesystem, any mounted file systems in “/mnt/”, the “/archive/” file system (no sense backing up our backup sets!), as well as Squid’s rather large cache files (which are, in my opinion, a waste of backup media and unnecessary to back up):

tar -zcvpf /archive/full-backup-`date '+%d-%B-%Y'`.tar.gz \
    --directory / --exclude=mnt --exclude=proc --exclude=var/spool/squid

[root@vasappserver1 tamim]# tar -cpvzf tamim.tar /usr/local/

How to recover forgotten root Password in RHEL

December 21, 2009 Leave a comment

You have to login in single-user mode and create a new root password. To enter single-user mode, reboot your computer. If you use the default boot loader, GRUB, you can enter single user mode by performing the following:

Stop 01: At the boot loader menu, use the arrow keys to highlight the installation you want to edit and type [A] to enter into append mode.

Stop 02: You are presented with a prompt that looks similar to the following:

grub append> ro root=LABEL=/

Step 03: Press the Space bar once to add a blank space, then add the word single to tell GRUB to boot into single-user Linux mode. The result should look like the following:

ro root=LABEL=/ single

Step04: Press [Enter] and GRUB will boot single-user Linux mode. After it finishes loading, you will be presented with a shell prompt similar to the following:


Step 05: You can now change the root password by typing

passwd root

Source: RedHat Linux official Side.

Htaccess in Linux

December 15, 2009 Leave a comment

How to Configure Your Website Using Htaccess in Linux with Apache

Step 01: For this to work successfully you will have to be logged in as root or using one of the sudo or su options.

Step 02: We will need to create the folder that will have to be authenticated. Since the default location in apache is /var/www/html we will create it here. You will do this by using the mkdir command.

[root@linux ~]# mkdir /var/www/html/testfolder

Restrict web page under /var/www/html/testfolder using basic authentication:

Step 03: Next we need to add the .htaccess & .htpasswd files to the personal folder. We first need to change the directory of the folder we wish to protect.

[root@linux ~]# cd /var/www/html/testfolder

Step 04: Next we can create the .htaccess file.

[root@linux ~]# vi .htaccess

Step 05: Press i to insert and add the following content.

AuthUserFile /var/www/html/testfolder/.htpasswd

AuthGroupFile /www.null

AuthName "Authorization Required"

AuthType Basic

require user USER_NAME

N.B. Change “test folder” to the name of your folder and change “USER_NAME” to the user name you wish to use.

Press your esc button then :wq to save your file in your vi editor.

Step 06: Next we’ll create the .htpasswd file. We want to run htpasswd on the path of the folder we want to protect.

[root@linux ~]# htpasswd -c /var/www/html/testfolder/.htpasswd USER_NAME

New password: 

Re-type new password: 

Adding password for user USER_NAME

Step 07 : Next we will have to edit the apache httpd.conf (on some systems called the apache2.conf) file.

[root@linux ~]# vi /etc/httpd/conf/httpd.conf

Step 08: You will have to scroll all the way to the bottom to add the following directory.


<Directory "/var/www/html/testfolder">

AllowOverride AuthConfig


Step 09: Finally save httpd.conf by typing esc :qw! and restart apache.

[root@linux ~]# service httpd restart

Step 10: To add new users, use the same command without the -c switch. For example, to add the user mahbub, type

# htpasswd .htpasswd mahbub

Step 11: To delete users, open the .htpasswd file, using your favorite unix editor, like vi, and delete the row(s) associated with the specific user(s) that you want to remove.

Restrict web page under /var/www/html/testfolder using Digest authentication:

Step 12: Add the following lines in the htttpd.conf file

<Directory "/var/www/htdocs/testfolder" >
 Options None
 AllowOverride None
 AuthType Digest
 AuthName "Protected Area"
 AuthDigestFile /usr/local/Apache/conf/digest_passwd
 AuthDigestGroupFile /usr/local/apache/conf/groups
 Require valid-user
 Order deny,allow
 Deny from all

Step 13:create a valid user as

# htdigest -c /usr/local/apache/conf/digest_passwd "Protected Area" username

Step 14:Now restart http service

Theory of User authentication

Apache allows us to require user authentication for access to certain directories. The authentication method can be one of two types, Basic or Digest.

Basic authentication

To set up a directory that requires a user to supply a username and password we would use something like the following in our httpd.conf file:

<Directory "/var/www/htdocs/protected" >
 Order deny,allow
 Deny from all
 Allow from 192.168.1.
 AuthName "Private Information"
 AuthType Basic
 AuthUserFile /usr/local/apache/conf/passwd
 AuthGroupFile /usr/local/apache/conf/groups
 require group <group-name>

Firstly we have denied access to all users but those on our internal network to the directory /var/www/htdocs/protected. To require a password we use the AuthType Basic directive. Our password file is /usr/local/apache/conf/passwd, as specified by the AuthUserFile directive and, similarly, we specify a group file. The last line require group <group-name> means that a user must be a member of <group-name> in order to be allowed access to the directory.

Of course, for this to work, we must set up our password and group files. For the group file simply create a file, /usr/local/apache/conf/groups, containing the line:

group-name: user1 user2

You can specify as many groups as you wish on separate lines. List users separated by a space.

Next we create the password file with the command htpasswd -cm /usr/local/apache/conf/passwd user1. This will prompt for a password and create a user with name user1 in the file /usr/local/apache/conf/passwd. The c option will create the file if it doesn’t exist, and the m option will MD5 hash the password (SHA1 and crypt options are also available, but SHA1 does not work with some Apache versions). Subsequent users can be added using htpasswd -m /usr/local/apache/conf/passwd user1.

If you do not want to use groups you could use require valid-user user1 user2 in order to only allow access to certain users.

The disadvantage of Basic Authentication is that passwords are sent as plain text from the client to the server, meaning that it is simple for a malicious user with access to the network can obtain the password using a network traffic analyzer. Digest Authentication tries to prevent this.

Digest Authentication

In digest authentication the password is never transmitted across the network. Instead the server generates a nonce, a one-time random number, and sends it to the client’s browser, which then hashes the nonce with the user’s password and sends the resulting hash back to the server. The server then performs the same hash and compares the result. This is considerably more secure than Basic Authentication, though not so widely used. One disadvantage of Digest Authentication is that it requires setting up a different password file for each realm on the server, as the realm name is used when creating the necessary hashes. With Basic Authentication, one password file can be used across the board.

To create an area protected by Digest Authentication, we use something like the following.

<Directory "/var/www/htdocs/protected" >
 Options None
 AllowOverride None
 AuthType Digest
 AuthName "Protected Area"
 AuthDigestFile /usr/local/Apache/conf/digest_passwd
 AuthDigestGroupFile /usr/local/apache/conf/groups
 Require valid-user
 Order deny,allow
 Deny from all

This time we set AuthType Digest, and the AuthName "Protected Area" directive is required. In place of AuthUserFile and AuthGroupFile directives we use the AuthDigestFile and AuthDigestGroupFile directives. The group file is the same as previously, but we need to set up the password file using the command htdigest -c /usr/local/apache/conf/digest_passwd "Protected Area" user1. Note the use of the htdigest program in place of htpassword and the AuthName in the command. Again the c option creates the file if it doesn’t exist.

Article Written By : Mahabub Bhai.

Apache Installation on Linux

December 15, 2009 Leave a comment

Quick Installation Guide

Step 1: Check the http RPM

# rpm -qa |grep http

Step 2: Configure /etc/httpd/conf/httpd.conf file

# vi /etc/httpd/conf/httpd.conf

Then Edit the following line

ServerAdmin root@localhost, u can use another mail account

#ServerName ;

If this is not set to valid DNS name for your host, Entry here your IP_ADDR and uncomment

UseCanonicalName Off
DocumentRoot "/var/www/html"
DirectoryIndex index.html index.html.var index.php

Step 3: Start the httpd service

# chkconfig httpd on
# /etc/init.d/httpd restart

Configure Named Virtual Hosting:

Step 1: Edit /etc/httpd/conf/httpd.conf file as


<VirtualHost *:80>
    ServerName localhost.localdomain
    DocumentRoot /var/www/html
    DirectoryIndex index.php index.html index.htm

    DocumentRoot /var/www/html/joomla/
    DirectoryIndex index.php index.html index.htm

    DocumentRoot /var/www/html/joomla1/
    DirectoryIndex index.php index.html index.htm

Step 2: Copy the website content in the Document Root

Make sure that index.php index.html index.htm are exist in root directory.

Step 3: Restart the httpd service

# chkconfig httpd on
# /etc/init.d/httpd restart

Step 4: Change the file permission in /var/www/html/joomla

# chmod –R 755 /home/www/joomla
# chmod –R 755 /home/www/joomla/
# chmod –R 755 /home/www/joomla/*

Step 5: Go to web browser and enter the site address or IP

Configuration – Multiple Sites and IP Addresses

Step 1: Make DNS entry with IP aliases

Step 2: Edit /etc/httpd/conf/httpd.conf file as


<VirtualHost *>
    DocumentRoot /home/www/site1

    DocumentRoot /home/www/site2

    DocumentRoot /home/www/site3

How To Protect Web Page Directories With Passwords

You can password protect content in both the main and subdirectories of your DocumentRoot fairly easily. I know people who allow normal access to their regular Web pages, but require passwords for directories or pages that show MRTG or Webalizer data. This example shows how to password protect the /home/www directory.

1) Use Apache’s htpasswd password utility to create username/password combinations independent of your system login password for Web page access.

You have to specify the location of the password file, and if it doesn’t yet exist, you have to include a -c, or create, switch on the command line. I recommend placing the file in your /etc/httpd/conf directory, away from the DocumentRoot tree where Web users could possibly view it. Here is an example for a first user named peter and a second named Paul:

[root@bigboy tmp]# htpasswd -c /etc/httpd/conf/.htpasswd peter
New password:
Re-type new password:
Adding password for user peter
[root@bigboy tmp]#

[root@bigboy tmp]# htpasswd /etc/httpd/conf/.htpasswd paul
New password:
Re-type new password:
Adding password for user paul
[root@bigboy tmp]#

2) Make the .htpasswd file readable by all users.

[root@bigboy tmp]# chmod 644 /etc/httpd/conf/.htpasswd

3) Create a .htaccess file in the directory to which you want password control with these entries.

AuthUserFile /etc/httpd/conf/.htpasswd
AuthGroupFile /dev/null
AuthName EnterPassword
AuthType Basic
require user peter

Remember this password protects the directory and all its subdirectories. The AuthUserFile tells Apache to use the .htpasswd file. The require user statement tells Apache that only user peter in the .htpasswd file should have access. If you want all .htpasswd users to have access, replace this line with require valid-user. AuthType Basic instructs Apache to accept basic unencrypted passwords from the remote users’ Web browser.

4) Set the correct file protections on your new .htaccess file in the directory /home/www.

[root@bigboy tmp]# chmod 644 /home/www/.htaccess

5) Make sure your /etc/httpd/conf/http.conf file has an AllowOverride statement in a <Directory> directive for any directory in the tree above /home/www. In this example below, all directories below /var/www/ require password authorization.

<Directory /home/www/*>
   AllowOverride AuthConfig

6) Make sure that you have a <VirtualHost> directive that defines access to /home/www or another directory higher up in the tree.

<VirtualHost *>
   DocumentRoot /home/www

7) Restart Apache.

# /etc/init.d/httpd restart

Try accessing the web site and you’ll be prompted for a password.

Article Written By : Mahabub Bhai.