Search This Blog

Thursday, July 29, 2010

SAMBA Configuration in linux.

This Article is based Following Redhat release.
# lsb_release -a
LSB Version: :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch
Distributor ID: EnterpriseEnterpriseServer
Description: Enterprise Linux Enterprise Linux Server release 5.2 (Carthage)
Release: 5.2
Codename: Carthage


For a simple installation and configuration following steps are taken.

1)Install samba packages necessary for samba installation.
2)Start the samba service at runlevel(Optional).
3)Create Samba users.
4)Setting the password for samba user.
5)Edit configuration file accordingly.
6)Restart the samba service.

1)Install samba packages necessary for samba installation.
First check to see if samba server is installed or not.

# rpm -q samba
package samba is not installed

However you can find samba client installed on most of Linux Distributions.

# rpm -qa | grep samba
samba-common-3.0.28-0.el5.8
samba-client-3.0.28-0.el5.8

{
Note: If you dont get any client and common rpm you might need to explicitly install them.
You might need to install cups-libs-1.2.4-11.18.el5.i386.rpm as a dependency for above 2 packages.
}

To get the samba server package the best place to for is your Distribution disk.
I have searched my OS disk and found following package for samba.
samba-3.0.28-0.el5.8.i386.rpm

install the above package on the system using.
# rpm -ivh samba-3.0.28-0.el5.8.i386.rpm


After successful installation just run “rpm –q samba “ To check if samba is installed or not.
# rpm -q samba
samba-3.0.28-0.el5.8

Samba configuration file is stored at following location.
/etc/samba/smb.conf


2)Start the samba service at runlevel(Optional).
Samba service name.
smb

To start samba service at system starup use chkconfig command.

To check the current runlevel of smb service.
# chkconfig --list | grep smb
smb 0:off 1:off 2:off 3:off 4:off 5:on 6:off

To start the service at system startup use.
#chkconfig --level 345 smb on

reconfirm with below command.

# chkconfig --list | grep smb
smb 0:off 1:off 2:off 3:on 4:on 5:on 6:off

restart the machine and check the smb service status.
# service smb status
smbd (pid 4712 4671) is running...
nmbd (pid 4684) is running...


3)Create Samba users.

First, lets create the smbpasswd file that we defined in the smb.conf file. The best way to go about this is to create it based on your existing /etc/passwd file. In other words, samba users are created based on existing linux users:
#cat /etc/passwd | mksmbpasswd.sh > /etc/samba/smbpasswd

after execution a new file will be created under /etc/samba directory.

lmhosts passdb.tdb secrets.tdb smb.conf smb_orig.conf smbpasswd smbusers

4)Setting the password for samba user.

the password for samba user is not similar to existing system users.
We need to manually set the samba user.
# smbpasswd root
New SMB password:
Retype new SMB password:

5)Edit configuration file accordingly.
Now its time to edit samba configuration file .
Take a backup of original file using.
# cp /etc/samba/smb.conf /etc/samba/smb_orig.conf

For the very simple configuration file i will recommend to backup the configuration file and create a new one.
#mv /etc/samba/smb.conf /etc/samba/smb.conf.old
following values to newly created configuration file .

*************************************************************

[global]
workgroup = PUTIEVILLE
server string = My Lil Linux Box
hosts allow = 192.168. 127.
log file = /var/log/samba/%m.log
security = user
encrypt passwords = yes
smb passwd file = /etc/samba/smbpasswd
socket options = TCP_NODELAY SO_RCVBUF=8192 SO_SNDBUF=8192

[Downloads]
comment = Downloads
path = /
browseable = yes
writable = yes
public = yes
read only = no

[homes]
comment = My Home Directory
browseable = yes
writable = yes
public = yes
read only = no

[printers]
path = /var/spool/samba
public = yes
guest ok = yes
printable = yes
browseable = yes
writable = yes
read only = no
******************************************************
Description of Samba Configuration file:
[global]
hosts allow = 192.168. 127.
This line will allow samba server accessible from 10.180.18 series and localhost .

smb passwd file = /etc/samba/smbpasswd
This will tell samba to use smbpasswd file from mentioned location

[Downloads]
comment = Downloads
Name of the share
path = /
Path that should be made available for all.

make read only = yes if you dont want others to write anything .

Make sure the samba user you create has a read access on the shared files(If this fails you assign ownership to samba user this is not recommended )

6)Restart the samba service.
#service smb restart
and access the files through windows login.







++++++++++++++++Additional Information+++++++++++++++++++++
You may get such error if /etc/samba/smb.conf file doesn't exists .(I renamed it /etc/samba/smb.conf.bkp ) i have created a dummy file using touch /etc/samba/smb.conf and the command run successfully.
# smbpasswd root
Can't load /etc/samba/smb.conf - run testparm to debug it

You may get below errors if you try to set a weak samba password for root account.
(This may be distribution specific and the chances of you not getting this error is more.Mine is harden for security reason ). you will not get the error if you set a strong password.
# smbpasswd root
New SMB password:
Retype new SMB password:
account_policy_get: tdb_fetch_uint32 failed for field 1 (min password length), returning 0
account_policy_get: tdb_fetch_uint32 failed for field 2 (password history), returning 0
account_policy_get: tdb_fetch_uint32 failed for field 3 (user must logon to change password), returning 0
account_policy_get: tdb_fetch_uint32 failed for field 4 (maximum password age), returning 0
account_policy_get: tdb_fetch_uint32 failed for field 5 (minimum password age), returning 0
account_policy_get: tdb_fetch_uint32 failed for field 6 (lockout duration), returning 0
account_policy_get: tdb_fetch_uint32 failed for field 7 (reset count minutes), returning 0
account_policy_get: tdb_fetch_uint32 failed for field 8 (bad lockout attempt), returning 0
account_policy_get: tdb_fetch_uint32 failed for field 9 (disconnect time), returning 0
account_policy_get: tdb_fetch_uint32 failed for field 10 (refuse machine password change), returning 0

Wednesday, July 28, 2010

Safest way of comparing values using Variable.

Method 1:
if [ "x$HAVE_CIFS" = "xyes" ]; then
...
fi

Method 2:
if [ "$HAVE_CIFS" = "yes" ]; then
...
fi

in above 2 methods method one is safest as it deals with versions of "test" that don't deal with empty strings correctly.

Now onwards i will try to use the safest way whenever is possible.

reference
http://www.linuxquestions.org/questions/showthread.php?p=4048568&posted=1#post4048568

More of Robust scripting article can be found in following blog entry.
http://learnlinuxwithrohan.blogspot.com/2010/08/robust-scripting.html

Thursday, July 22, 2010

Precaution to take before deleting file through script.

Here i m highlighting the best practice to delete a file when there is a variable involved as a file name.

#!/bin/bash
echo "Enter file or folder to delete from tmp"
read input
if [ ! -f "/tmp/$input" ]
then
echo "File Doesnt Exist"
else
rm -f "/tmp/$input"
fi


rm -f, --force
ignore nonexistent files, never prompt

Tuesday, July 20, 2010

While and For loop for reading from file.

Filename.txt
My First Line
My Second Line

::::While Loop:::
Program(whileloop.sh):

while read line
do
echo "$line"
done < Filename.txt

output:
My First Line
My Second Line

:::For Loop:::
Program(forloop.sh):
for line in $(cat Filename.txt)
do
echo $line
done

output:
My
First
Line
My
Second
Line

You might wonder why does the output generated by for loop is different.

Well in above example for has treated space as field separator .
But to achieve the above output you need to add next line as a separator.

Therefore the same code can be written as.
Program:
IFS=for i in $(cat Filename.txt)
do
echo $i;
done

output:
My First Line
My Second Line\n' # Specify next line as Field Separator.


for i in $(cat Filename.txt)
do
echo $i;
done

output:
My First Line
My Second Line

Thursday, July 15, 2010

parsing command line option .

I have decided to run the script in two mode
1)Interactive.
2)Automatic.

Automatic mode ( -a ) (default):This mode shoudnt ask any input from user and should run automatically.

Interactive mode (-i) : This mode should ask user for input .


Initially i m trying to concentrate on automatic mode which is by default.



I have used getopts method to deal with command line arguments. However this might restricted to bash script because of getopts method.
Method used in program.
if [ $# -eq 0 ]
then
echo "Automatic Mode Selected"
else
while getopts ai option 2>/dev/null
do
case "$option" in
a) echo "Automatic Mode Selected";;
i) echo "Interacive Mode Selected";;
?) echo "Please select proper option"
echo list of available options
-a automatic -i interactive ;;
esac
done
fi

However I will also post universal method to establish the same task.

while [ "$#" -gt 0 ]; do
case "$1" in
-o|--long-option)
: do something
;;
-p|--option-with-arg)
: do something with $2
shift
;;
*)
# this could be an invalid option or just a file
# if no file is expected, do something like:
echo "invalid option or argument: $1"
;;
esac
shift
done

Please Leave us with your comments and Queries/Suggestions.
I will try to reply asap.

Trimming a variable in shell script.

The easiest way to trim a variable in shell script.
VARIABLE=$( echo $VARIABLE )
The shell automatically removes seemingly excessive whitespaces, which is why you have to double quote variables if you want to preserve them.

External Source:
http://www.unix.com/shell-programming-scripting/138857-how-trim-variable-linux.html#post302431753

Take a look at following example.
#!/bin/bash


VARIABLEA="Ajay "
VARIABLEB=" M Pawar"
echo "$VARIABLEA"
echo "$VARIABLEB"
VARIABLEC=$(echo "$VARIABLEA $VARIABLEB")
echo "$VARIABLEC"


VARIABLEM=$(echo $VARIABLEA)
VARIABLEN=$(echo $VARIABLEB)
echo "$VARIABLEM"
echo $VARIABLEN
VARIABLEO=$(echo "$VARIABLEM $VARIABLEN")
echo $VARIABLEO



Check the output yourself.

Wednesday, July 14, 2010

One of the critical element is to know How long the machine is running.
uptime is used for this purpose.
It provides following information.
1)How long the machine is running?
2)How many users are currently logged in the machine.
3)current workload,avg workload for last 5 min,avg workload for last 15 min.

Tuesday, July 13, 2010

How to combine two file using Paste command

# cat file1
milind mumbai
rahul gujrat
mayur surat

# cat file2
10jun
22july
12december

# paste file1 file2
milind mumbai 10jun
rahul gujrat 22july
mayur surat 12december

content of file 1 and file 2 are separated by tab.

If you want to include any other delimiter please use it as follows .
In following example I have used space delimiter.

# paste -d' ' file1 file2
milind mumbai 10jun
rahul gujrat 22july
mayur surat 12december

Monday, July 12, 2010

Script to monitor performance of Oracle VM Server.

Most of the time the load average of VM server indicates the future disaster that might occur in the vm environment.

Its always recommended to implement precautionary major to avoid such situation.

xentop - display real-time information about an xVM system and domains
SYNOPSIS
xentop [-h] [-V] [-d=seconds] [-n] [-r] [-v] [-b] [-i=iterations]
DESCRIPTION
The xentop command displays information about the Solaris xVM system
and domains in a continually-updating way. It is analogous to the BSD
UNIX top command.
OPTIONS
The following options are supported:
. . .
-b, --batch
Output data to stdout in batch mode.
-i, --iterations=iterations
Maximum number of iterations xentop should display before terminat-ing.

Executing
xentop --batch --iteration=1
giving me 0% cpu utilization.

There fore executing following command which somehow giving me acceptable values for cpu utilization.
xentop -d=10 --batch --iteration=4


I m thinking of getting 3 4 entries in such a way and calculate the average.
One more issue is Hostname is displayed only 10 character long.

any way i will post the end result in some time.




Script:

#!/bin/bash

fn_main()
{
rm -rf /tmp/Load_Average_Check.log > /dev/null 2>&1
echo "Target Name = $1" >> /tmp/Load_Average_Check.log
echo "Occurred At = `date` " >> /tmp/Load_Average_Check.log
echo "Message = Load Average on $1 is $2 & has crossed the Critical threshold " >> /tmp/Load_Average_Check.log
echo "Metric = Load Average on $1 " >> /tmp/Load_Average_Check.log
echo "Metric value = $2 " >> /tmp/Load_Average_Check.log
echo "Severity = Critical " >> /tmp/Load_Average_Check.log
echo "Acknowledged = No " >> /tmp/Load_Average_Check.log
echo >> /tmp/Load_Average_Check.log

echo "*******************************************************" >> /tmp/Load_Average_Check.log
################################################################################################################
echo "High Cpu Utilisation (%)in $1" >> /tmp/Load_Average_Check.log
#ITERATION indicates the number of time vm servers are checked for cpu utilization.
ITERATION=4
ssh -Tqn root@$1 xentop -b -d 2 -i $ITERATION > xentopresult.txt
#generate hostname file to be concatenated with monitoring log.
while [ $ITERATION -gt 0 ]
do
ssh -Tqn root@$1 xm list | awk '{print $1}' >> mno.txt
ITERATION=`expr $ITERATION - 1`
done

#merge hostname file to output.
paste mno.txt xentopresult.txt > final.txt
#To print Top 10 instances of maximum cpu utilization.
cat final.txt |grep -vE "NAME"| awk '{print $1,$5}'| sort -nr -k 2 | head -13 >> /tmp/Load_Average_Check.log
cat final.txt |grep -vE "NAME|Domain-0"| awk '{print $1,$5}'| sort -nr -k 2 | head -10 > vmwithguest.txt
#echo "Content of vmwithguest."
#cat vmwithguest.txt
echo "*******************************************************" >> /tmp/Load_Average_Check.log

cat vmwithguest.txt | awk '{print $1}'| sort -u > check.txt
#echo "Content after sorting vmwithguest"
#cat check.txt

while read line
do
MACHINENAME1=`echo $line | awk '{print $1}'`
match=0
CPU=0
CPUTOTAL=0
CPUAVERAGE=0
while read line
do
MACHINENAME2=`echo $line | awk '{print $1}'`
if [ "$MACHINENAME1" == "$MACHINENAME2" ]
then
match=`expr $match + 1`
CPU=`echo $line | awk '{print $2}' |awk -F "." '{print $1}'`
CPUTOTAL=`expr $CPU + $CPUTOTAL`
fi
done < cpuaverage="`expr">> /tmp/Load_Average_Check.log
echo " " >> /tmp/Load_Average_Check.log
fi
done <>> /tmp/Load_Average_Check.log
echo "NOTE: Make sure Load Average is always below 4% for better Performance" >> /tmp/Load_Average_Check.log

mail -s "Load Average on $1 is $2" Username@xyz.com < /tmp/Load_Average_Check.log
mail -s "Load Average on $1 is $2" Username2@xyz.com < /tmp/Load_Average_Check.log
mail -s "Load Average on $1 is $2" Username3@xyz.com < /tmp/Load_Average_Check.log
}


while read line
do
#Calculate Load on remote machine.
LOAD=`ssh -T -q -n $line w | grep average | awk -F "e:" '{print $2}' | awk -F "," '{print $1}' | awk -F "." '{print $1}'`
if [ $LOAD -gt 7 ]
then
fn_main "$line" "$LOAD"
fi
done < SERVERNAMES

find a file with specific string inside it

This is very useful command especially when you need to find a file with specific string inside it.

I normally use this command to find out my script which was misplaced in file system.


grep -r 'text to search for' /path/to/dir

Below command is not checked but seems to be working.
reference:
grep -oHnr "some pattern" *.txt
-o print Only the matched parts of a matching line, with each part on a separate line. -H print the filename for each match.
-n prefix each line of output with the 1 based line number.
-R, -r recursively read all files under subdirectories.

Saturday, July 10, 2010

guest location in ESX (VMWARE).

All guest machines are normally stored at following location.

/vmfs/volumes/datastore1/

Thursday, July 8, 2010

Color Combination used in script.

To make the script more user friendly I have decided to add some colors in output.

3 Basic colors are used in showing the output.

1)Green. (echo -e "\033[32m Hello World")
2)Red. (echo -e "\033[31m Hello World")
3)White. (The "tput sgr0" restores the terminal settings to normal.)

conventions
Green : Command used for execution
Red : User Input
White : Output./Notes


**************************************
More options

BGGREEN=`echo "\033[42m"` # background to green
BGCYAN=`echo "\033[45m"` # background to cyan
BGWHITE=`echo "\033[47m"` # background to white
FGBLACK=`echo "\033[30m"` # foreground to black
FGRED=`echo "\033[31m"` # foreground to red
BGBLACK=`echo "\033[40m"` # background to black
FGGREEN=`echo "\033[32m"` # foreground to green
BGWHITEFGBLUE=`echo "\033[91m"` # background to white & foreground to blue
#BGWHITEFGBLUE=`echo "\033[94m"` # background to white & foreground to blue
BGWHITEFGMAGENTA=`echo "\033[95m"` # background to white & fg to magenta
BGYELLOW=`echo "\033[43m"` # background to yellow
FGBLUE=`echo "\033[34m"` # foreground to blue
FGMAGENTA=`echo "\033[35m"` # foreground to magenta
FGCYAN=`echo "\033[36m"` # foreground to cyan
BGBLUE=`echo "\033[44m"` # background to blue
BGRED=`echo "\033[41m"` # background to red
FGWHITE=`echo "\033[37m"` # foreground to white
BGCYAN=`echo "\033[45m"` # background to cyan
BLINK=`echo "\033[5m"` #terminal to blink
NORMAL=`echo "\033[m"` #terminal to normal
UNDERLINE=`echo "\033[4m"` #terminal to underline
NOUNDERLINE=`echo "\033[24m"` #terminal to cancel underline
REVERSE=`echo "\033[7m"` #terminal to reverse video
BGMAGENTA=`echo "\033[45m"` # background to magenta
BOLD=`echo "\033[1m"` #bold

Click on image to see high resolution version of it.

For more detailed tput information please go through this informative article.
http://www.thegeekstuff.com/2011/01/tput-command-examples/?utm_source=feedburner&utm_medium=email&utm_campaign=Feed%3A+TheGeekStuff+%28The+Geek+Stuff%29

Wednesday, July 7, 2010

My project ( Performance Monitoring in Linux) How/When/Why about my project.

Today i have decided to write my own linux script for performance monitoring.

I know there are plenty of tool over there but i wanted to take this as a self learning to improve on my scripting and analyzing skills.

Performance can be monitored considering following aspects:
1)CPU
2)MEMORY (RAM)
3)HARDDISK IO
4)NETWORK