Tuesday, October 23, 2012

Step by Step guide: Resizing File System on the Linux Virtual Machine

Assumptions made:
You must have basic knowledge of LVM to follow the instruction.
Linux Environment : I have done this on SLES 11; However the LVM commands used are the generic Linux commands. Thus, this instruction should work on wide flavors of Linux.
Volume Group (VG) name: testvg
Logical Volume(LV) name: testlv
Root file system is on /dev/testvg/testlv file system  and I want to increase the file system size by 5 GB.


Step1: Provision an extra disk space to the Virtual Machine [ i.e Increase the VMDK size of the VM via vSphere client if you are on VMWare environment]

[Note: If you provision an additional disk (some VMs doesn't allow to increase the VMDK size), check if you can see that additional disk. Say an additional disk is /dev/sdb. Then, run the command  #fdisk -l   . It should show  /dev/sdb. If you cannot see /dev/sdb, then reboot the system and run #fdisk -l  command again]

Step2: Partition the additional disk space you just added [ Say your system sees disk as /dev/sda ]
           # fdisk   /dev/sda
    • Create a new partition (primary if possible) Let's say  /dev/sda4
    • Define size
    • Change the partition type from 83 (EXT3) to 8e (LVM)
    • Write the change
Step3: Create a Physical Volume 
            # pvcreate <partition_name>
                    Example: # pvcreate  /dev/sda4

If you get an error reporting that system can't see the partition, reboot the Virtual Machine.

And re-run the pvcreate command once the system is back up. 

Note: You can run #vgdisplay command to check what Volume Groups are available
Step4: Extend the Volume Group [ i.e Add the physical volume we just created to the existing volume group]
           # vgextend    <vgname>   <partition_name>
                 Example: # vgextend   testvg   /dev/sda4

Note: You can run #lvdisplay command to check what logical volumes  are available
Step5: Extend the Logical Volume  [ Say increase by 5GB ]
           #lvextend   -L     +5G    /dev/<vgname>/<lvname>   <partition_name>
                   Example: #lvextend   -L   +5G   /dev/testvg/testlv     /dev/sda4

Warning!!! Check if a space is free in the physical volume or not. You can run the command #pvdisplay  and check how much free space is available in the physical partition. 

Step6: Resize the file system
          #resize2fs     /dev/<vgname>/<lvname>
                Example: #resize2fs    /dev/testvg/testlv

Note: If resizze2fs doesn't work, reboot the system. For example: SLES 10 doesn't support online resize of the mounted file-system. Thus, SLES10 requires reboot after 'lvextend' of root file-system. 

Validate if the size of the file system has been increased or not. 
         #df  -h

Best of Luck! 




Wednesday, March 14, 2012

Is your ssh login slow? It could be DNS issue

There could be  various reasons that could result in slow ssh login. Usually it's DNS configuration issue on your server.

In my SUSE environment,
#ping www.google.com
would wait 15 seconds before it displayed something like this
PING www.l.google.com (74.125.227.19) 56(84) bytes of data

I had similar 15 seconds delay for ssh login. Thus, I used following option in  sshd_config file (located under /etc/ssh/sshd_config)
UseDNS  no

Restart SSH Daemon
#service sshd restart

Now there is no delay in SSH login.

However delay in pining google.com was still bothering me. Finally I figured out that you shouldn't be changing DNS configuration from /etc/resolv.conf if you are using netconfig or YaST tool. Thus to clean this mess, I ran following commmand

#netconfig update -f

This command updated  resolv.conf as per netconfig/YaST configuration for DNS and my pinging issue was resolved.
I know you might be thinking of enabling UseDNS for ssh. Yes, you can do it as DNS issue is resolved.

Good Luck



Tuesday, March 6, 2012

WordFinder script: The script that crawls thru' every files to find what you are looking for

Have you ever wanted to find the 'word' in files located under directory containing sub-directories and tons of files. It would be nightmare to search in individual file. You can make your life easy by writing a script that crawls thru' every files under that directory and return the file name along with the line that contains the word you are looking for.

Ok let's get started with BASH script that will do the job for us.

$vim WordFinder.sh
#!/bin/bash
#Author: erdevendra@gmail.com
#
#Usage: This scripts finds a word in each and every files located under the specified location

#
#Syntax:
# .\WordFinder.sh
#

for x in `find $1 -type f`
do
#find files under the specified location and use for loop to go thru each files
         grep -i $2 $x
#search for the pattern in the file
          if [ $? -eq 0 ]
#Check if grep cmd executed successfully
             then echo $x
#if grep cmd executed successfully (i.e if pattern found) display the file name
          fi
#end IF loop
done
#end FOR loop


Give an executable permission to the file

#chmod 777 WordFinder.sh

(Note: 777 gives full permission to everyone in the system)

You are good to go. Now you just need to run that script to find what you are looking for.

Example:
#WordFinder.sh  <location>  <word_you_are_looking_for>

Let's say I want to find word 'listen' in '/etc' directory, I can run following command:
#WordFinder.sh  /etc  listen

Friday, February 10, 2012

Setting PATH variable in SUSE linux

Usually for your local settings, favorite global aliases, VISUAL and EDITOR variable, PATH environment variable and more, you can create a file /etc/profile.local in SuSE Linux.  SuSE Linux doesn't recommend you to change /etc/profile as there are the chances that your changes will be lost during system upgrades.


#vim  /etc/profile.local
PATH=$PATH:/path/to/wherever
export PATH

Log-out and log-back in.

#echo $PATH

You will see that your PATH variable has been updated.


Practical Application:

I write various scripts for system administration. I like my scripts to be in one place. I don't want to go that that folder each time to run the script. I want to execute those scripts no matter where I am.

Say, my scripts are in /usr/local/bin/myscripts

#vim  /etc/profile.local
PATH=$PATH:/usr/local/bin/myscripts
export PATH

Now I logged-out and logged-back in. I can run my scripts within myscripts folder anywhere I like.

Thursday, January 19, 2012

Find and delete

Let's say there are thousands of WAV files in my Recordings folder and I want to clean up WAV files older than 5 days, I don't have to write fancy script. I just need to run this command

#find  .  -name  "*.wav"  -mtime +5 -exec rm {} \;

Explanation:

#find : Find command

. : Dot stands for current location

-name: Look for the name pattern
"*.wav"   : Find wave files

+5 : older than 5 days

-exec: Execute

rm : remove command

{} \;  : Terminate the command line [Note: there is space between rm, {} and \; ]



Let's say I want to remove every files older than 5 days in current directory, I can simply run the following command

#find . -mtime +5 -exec rm {} \;


dot (.)  represents the current directory


Let's say I want to setup interactive way (ask use before deleting file) to remove the files plder than 5 days

#find . -mtime +5 -exec rm -i  {} \;


rm - i : remove interactively


Let's say I want to forcefully remove files older than 5 days,


#find . -mtime +5 -exec rm -f  {} \;

rm - f : remove forecefully

Tuesday, September 6, 2011

More on Awk and Bash scripting

Today we are going to use the power of conditional IF within AWK (aka Conditional AWK programming).

Let's start with an exercise:

You have a file named file2.

#cat /root/file2
22110 2 even
21009 20 even
20903 2 even
24811 2 even
21703 18 even
20811 2 even
22008 2 even
29021 2 even

Where Column1 represents folder name, Column 2 to be used to compute a file name, Column 3 says that Column2 is Even number.

FileName is msg000 appended by (Colum2 -2)/2
e.g
22110 2 even

FileName is msg000 appended by (2-2)/2=0
i.e msg0000.txt


21009 20 even
FileName is msg000 appended by (20-2)/2=9
i.e msg0009.txt

Now we need to write a script, that will remove all those files.

Solution:

Step1: Write awk script that can generate commands to remove those files

#vi cleaner.awk
{
MessageNum=($2-2)/2;

#If MessageNum returns 0 then, there is only one file(may be .txt or wav) so use wildcard to delete that file
if(MessageNum == 0)
print "rm /var/spool/asterisk/voicemail/default/"$1"/INBOX/msg00*";

#If MessageNum returns greater than 0 but less than 10, then delete the bad file with name msg000
else if(MessageNum > 0 && MessageNum < 10)
print "rm /var/spool/asterisk/voicemail/default/"$1"/INBOX/msg000"MessageNum".txt";

#If MessageNum returns greater than 9 but less than 100 , then delete the bad file with name msg00
else if(MessageNum > 9 && MessageNum < 100)
print "rm /var/spool/asterisk/voicemail/default/"$1"/INBOX/msg00"MessageNum".txt";

#If MessageNum returns greater than 99 but less than 1000 , then delete the bad file with name msg0
else if(MessageNum > 99 && MessageNum < 1000)
print "rm /var/spool/asterisk/voicemail/default/"$1"/INBOX/msg0"MessageNum".txt";
}

[Here $1 returns the data on Column1]

Step2:
Run the command
awk -f cleaner.awk /root/file2 > cleanall.sh
This command runs cleaner.awk script for each line of the file file2 and prints the rm command in cleanall.sh file

To execute the script, assign execute permission to the script file.
chmod 700 cleanall.sh

Run the script that contains all the rm commands
./cleanall.sh


For more:

http://www.thegeekstuff.com/2010/02/awk-conditional-statements/
http://www.linuxfocus.org/English/September1999/article103.html

Tuesday, July 19, 2011

Find and remove duplicates in file | Sort data

Commands to be used
cut
sort
uniq

Step 1:
Cut command is used to select the desired data from the file. Let's say data in my file: students.txt is as follows. We need to find duplicates in second field of the data in this file.

#vi students.txt
101 101 Mike
102 102 Ryan
103 103 Dev
104 102 Steve
105 100 Bill

I can use CUT command to select second field by executing following command
#cut -d ' ' -f2 students.txt > secondField.txt

# vi secondField.txt
101
102
103
102
100

-d flag: Delimiter; here we are using space as delimiter
-f flag: Field Number

Step 2:
Now I have my desired second field. I can issue SORT command to sort the data.

#sort -n secondField.txt > sortedData.txt

#vi sortedData.txt
100
101
102
102
103

-n flag: Sort numerically

Step 3:
Finally, we can use UNIQ command to find the duplicate or unique data.

Display only unique data
#uniq -uc sortedData.txt
1 100
1 101
1 103

Display only duplicated data
#uniq -dc sortedData.txt
2 102

Display all data without repeatition
#uniq sortedData.txt
100
101
102
103


-u flag: unique data
-d flag: duplicate data
-c flag: show the count

Applications:


1. To find and remove duplicate data in voicemail.conf of asterisk

2. To sort sip peers information
Collect sip peers information
#asterisk -rx "sip show peers" >> sippeers

Collect the extension/username (Column 1) of peers
#less sippeers|cut -d' ' -f1|cut -d'/' -f1> file1

Collect the IP address(Column 2) information. We have to use awk because AWK treats multiple delimiter as one delimiter. In sippeers file we have multiple spaces separating column 1 and column 2.
#awk -F" " '{print $2}' sippeers>file2

Count the number of lines in file1 and file2. Make sure that both has same number of lines
#wc -l file*

Put the collected Column1(Username/Extension) and Column2 (IP Addresses) in one file i.e file3
#paste file1 file2 > file3

Sort the data in file3. By default sort command takes the list and sort numerically according to the first column.
#sort file3

-k switch can be used to sort by specific column.
e.g #sort -k 2 file 3 --> This will sort according to second column

Final Script:

#!/bin/bash
#Author erdevendra@gmail.com Script to sort the SIP peers registered to the server and write to file sippeers
#This script is used to find which extension is in use and which is not
#Extension in use will have IP Address attached to it

#pull sip users from asterisk
/usr/sbin/asterisk -rx "sip show peers" > sippeers
#Filter users extensions
/usr/bin/less sippeers|cut -d' ' -f1|cut -d'/' -f1>file1
#Filter IP addresses
/usr/bin/awk -F" " '{print $2}' sippeers >file2
#Put users extensions and IP addresses together in a file
/usr/bin/paste file1 file2>file3
/bin/rm sippeers
#sort the file by extensions
/usr/bin/sort file3>sippeers
#remove temporary files
/bin/rm file1 file2 file3


3. To find the total number of IP addresses leased by DHCP server. [ Note: DHCP seems to keep same IP address multiple times in dhcp.leases database. ]

#less /var/lib/dhcp/db/dhcpd.leases|grep 10.219|awk -F" " '{print $2}'|awk -F"." '{print $3 $4}'|sort -n|uniq|wc -l

In Linux, DHCP server stores dhcp leases at /var/lib/dhcp/db/dhcpd.leases
In my example, I am filtering IP addresses for 10.219.1.1 network using 'grep 10.219'
awk -F" " '{print $2}' --> This filters out IP addresses only
awk -F"." '{print $3 $4}' --> This filters out 3rd and 4th octet of IP address e.g 10.219.2.230 will return 2230 (i.e 2.230)
sort -n --> This will sort the data in ascending order
uniq --> This will remove the repetition of data
wc -l ---> This will return the total number of lines, which in turn is the total number of uniq IP addresses already being assigned by DHCP server


For more: http://www.liamdelahunty.com/tips/linux_remove_duplicate_lines_with_uniq.php

Very good explanation with examples:
http://www.techrepublic.com/article/lesser-known-linux-commands-join-paste-and-sort/5031653

Wednesday, May 25, 2011

Centralized LogServer in SuSE Linux

LogServer: ServerA [IP address: 192.168.1.5]
NetworkServers: ServerB, ServerC... and more

ServerB-------send log files-----> ServerA[LogServer] <-----------send log files---- ServerC

Here we want ServerB, Server C to send it's log file to ServerA for centralized access of log files.

Daemon: syslog-ng
Files:
/etc/sysconfig/syslog
/etc/syslog-ng/syslog-ng.conf

Commands:
/etc/init.d/syslog start|restart|stop

ps aux|grep syslog --> to see if syslog-ng is running or not

SuSEconfig --module syslog-ng --> to reload the change done on /etc/syslog-ng/syslog-ng.conf


Configure LogServer i.e ServerA to accept the log files from NetworkServers

Edit /etc/syslog-ng/syslog-ng.conf on ServerA(Log Server)

source src {
#
# include internal syslog-ng messages
# note: the internal() soure is required!
#
internal();

#
# the default log socket for local logging:
#
unix-dgram("/dev/log");

#
# uncomment to process log messages from network:
#
udp(ip("0.0.0.0") port(514));
#I uncommented above line telling ServerA to accept the log files from network
};


At the bottom of this file, I defined the destination and log

#
#Added by DShah 05/25/11
#
destination std { file("/var/log/HOSTS/$YEAR-$MONTH/$HOST/$FACILITY-$YEAR-$MONTH-$DAY" owner(root) group(root) perm(0600) dir_perm(0700) create_dirs(yes));
};

log { source(src);
destination(std);
};

Over here I am telling ServerA to process the log files coming source src to destination std.
Destination std tells ServerA to save log messages from each host in a separate directory called /var/log/HOSTS/YEAR-MONTH/hostname/.

Now run the command
#SuSEconfig --module syslog-ng --> to reload the config changes done

#/etc/init.d/syslog restart OR
#syslog-ng restart

#ps aux|grep syslog --> to check if syslog-ng is running

If you need to kill syslog-ng process for any reason, you can simply run the command

#killall syslog-ng
or
#kill -9 [PID-of-syslog-ng]

Configure NetworkServers (Server B, ServerC... ) to send log files to LogServer(ServerA):

Edit /etc/syslog-ng/syslog-ng.conf or /etc/syslog-ng/syslog-ng.conf.in (preffered) on ServerB, ServerC

#
#Added by DShah 05/25/2011
#
destination logserver {
udp("192.168.1.5" port(514));
#Note: here 192.168.1.5 is an IP add of LogServer i.e ServerA
};

log {
source(src);
destination(logserver);
};

Now run the command
#SuSEconfig --module syslog-ng --> to reload the config changes done

#/etc/init.d/syslog restart OR
#syslog-ng restart

#ps aux|grep syslog --> to check if syslog-ng is running


ServerA should be already collecting the log files. You can go to /var/log/HOSTS directory on ServerA to see the log files from different Network Servers.

Illustration by Additional applications:
Let's say I want remote asterisk server to dump it's log file /var/log/asterisk/full in the centralized log server
Edit /etc/syslog-ng/syslog-ng.conf or /etc/syslog-ng/syslog-ng.conf.in (preffered) on remote Asterisk Server

#
# Added by DShah
#
source asterisklog { pipe("/var/log/asterisk/full");
};

destination logserver { udp("192.168.1.5" port(514));
};

log { source(asterisklog); destination(logserver); };


and run the command

#syslog-ng restart

Now please check /var/log/HOSTS , you should see log file from asterisk server.


If you need any help on Linux/Unix systems, you can email me at erdevendra@gmail.com with subject title rapidtechguide.

For more info: http://www.novell.com/coolsolutions/feature/18044.html
20 minutes video on syslog-ns : http://www.balabit.com/network-security/syslog-ng/opensource-logging-system/overview#
Syslog-ns to collect apache logs: http://peter.blogs.balabit.com/2010/02/how-to-collect-apache-logs-by-syslog-ng/

Wednesday, May 4, 2011

How to rename Ethernet Device using udev? How to change ethX to ethY?

Today we replaced motherboard/system board on our R610 Dell Server. Guess what? SuSe (Linux OS) still believes that it has 8 ethernet devices ( 4 on old system board and 4 on new system board ). So, now my new ethernet devices are listed as
eth4
eth5
eth6
eth7

I wanted to change the name back to
eth0
eth1
eth2
eth3
as I know that on my new system board there are only four builtin ethernet devices

So, what did I do to fix it?

Step 1: Stop network service #rcnetwork stop

Step 2: Edit udev rules for network devices # vi /etc/udev/rules.d/70-persistent-net.rules
Change ethX to ethY. Where X is undesired name and Y is desired name
So, I changed eth4 to eth0, eth5 to eth1, eth6 to eth2 and eth7 to eth3

Step4: Reboot the server

Step5: Check if it got the right name. You can use #ifconfig command
You can also use #hwinfo --netcard (For detailed information on network hardware)

Step5: Configure the IP address. In SuSe you can use YaST setup tool

I hope this helps you too..... :)



http://www.novell.com/support/search.do?cmd=displayKC&docType=kc&externalId=3012993&sliceId=1&docTypeID=DT_TID_1_1

Saturday, February 26, 2011

VI Tips

After lots of request from my friends, I finally end up with writing something on VI or VIM ( Vi IMproved ). This is one of the most powerful editor in UNIX environment.

Try this
# vi test.txt
[Press ESC]
:help

Please read through that briefly. There are various .txt files just like each chapters in text book.

You can access those .txt file using

For example
:help usr_01

I want you guys to read and learn by yourself. That really helps. You will know what you are doing.

However, I will mention some frequently used shortcuts/commands:

h j k l move left move up move down move right

:x [Enter] To save and exit file
:q To quit
:qw Same as :x
:q! Ignore the changes and quit

:set number Display line number on each line
:set nonumber Don't display line number on each line

:set ruler Display the cursor position at the bottom of the screen

CTRL + g Display the cursor position with percentage of the page

CTRL + d Scroll half window down
CTRL + u Scroll half window up

/findthis Search 'findthis' string
[ Press n or N for searching backward and forward in the file]

The characters .*[]^%/\?~$ have special meanings. If you want to use them in a search you must put a \ in front of them.

:set ignorecase To ignorecase while searching the string/pattern
:set noignorecase

G To go to the bottom of the page
gg To go to the top of the page

yy To copy/yank the line
dd To delete the line
p To paste the yanked/copied line

$ To go to the end of the line
0 To go to the front of the line

{ To go to the end of the paragraph
{ To go to the front of the paragraph

:syntax on To enable syntax highlight
:syntax off


:line_number To go to particular line e.g To go to line 22 , do the following :22 [enter]


SEARCHING FOR A WORD IN THE TEXT

Suppose you see the word "TheLongFunctionName" in the text and you want to find the next occurrence of it. You could type "/TheLongFunctionName", but that's a lot of typing. And when you make a mistake Vim won't find it. There is an easier way: Position the cursor on the word and use the "*" command. Vim will grab the word under the cursor and use it as the search
string. The "#" command does the same in the other direction. You can prepend a
count: "3*" searches for the third occurrence of the word under the cursor.

HIGHLIGHTING MATCHES

While editing a program you see a variable called "nr". You want to check where it's used. You could move the cursor to "nr" and use the "*" command and press "n" to go along all the matches.

There is another way. Type this command:

:set hlsearch

If you now search for "nr", Vim will highlight all matches. That is a verygood way to see where the variable is used, without the need to type commands.

To switch this off:
:set nohlsearch


REPLACE THE WORD

:%s/old_word/new_word/g

This will substitute old_word with new_word globally.

TUNING SEARCHES

There are a few options that change how searching works. These are the essential ones:

:set incsearch

This makes Vim display the match for the string while you are still typing it. Use this to check if the right match will be found. Then press to really jump to that location. Or type more to change the search string.

:set nowrapscan

This stops the search at the end of the file. Or, when you are searching backwards, at the start of the file. The 'wrapscan' option is on by default, thus searching wraps around the end of the file.


Bonus Tips:
If you like one of the options mentioned before, and set it each time you use Vim, you can put the command in your Vim startup file.
Edit the file, as mentioned at |not-compatible|. Or use this command to find out where it is:

:scriptnames

Edit the file, for example with:

:edit ~/.vimrc

Then add a line with the command to set the option, just like you typed it in
Vim. Example:

Go:set hlsearch

Saturday, October 23, 2010

Sending email using Command line + Scripting with example


Trial 1:

mail -s "This is a subject" john.doe@test.net < /root/myBodyFile

This simple command will send the email to john.doe@test.net with the subject "This is a subject" and body with the content of the file /root/myBodyFile

Trial 2:

You could check if your linux machine delievered the message to john doe or not.

# mail

If you see any MAILER-DAEMON@ , check if it is for john.doe@test.net (i.e email recepient) or not.

There could be various possible issues.

In my case I am using PostFix as my MTA.
I have configured mailrelayhost for my MTA.

For postfix, you can go to

#vi /etc/postfix/main.ca
------
-----
mailrelayhost= < specify ur mailrelay host or IP address of your mail server >
-----
-----

And also make sure that your linux machine's IP address is allowed to send email using email server (contact your email server administrator). He will add IP address of your linux machine as trusted host in his email server

Trail 3:

I would like to create alias so that I can send email to the group of people. It's easy.

Login as a root in your linux machine

#vi /etc/aliases
-----
----
#myEmailGroup: List of all emails separated by comma
-----
-----
ServerAlert: user1@gmail.com,user2@hotmail.com,214000000@txt.att.net
-----
-----

Next step is to load all the aliases

#newaliases

#echo $?
If it returns 0, then above command was executed succesfully


Finally you can send email to the group of users by

#mail -s "Hi.. all of you" ServerAlert < /root/myBodyFile


Application of email alert for the Asterisk PRI monitoring:

1. Create the script file

#vi astPRIcontrol.sh
#!/bin/bash
#author DShah erdevendra@gmail.com
#created on 10/22/10
#This script checks the PRI status; If PRI is down, it pages the admins

pristatus=echo /usr/sbin/asterisk -rx "pri show spans" |grep -i down

if [ -z $pristatus ]
then
#echo "PRI up"
notify=0
else
#echo "PRI down"
notify=1
fi

if [ $notify -eq 1 ]
then
mail -s " PRI down" Server_Alert < /root/pristatus

fi


2. Change the permission (make the script executable)

#chmod 700 /root/astPRIstatus.sh

3. Create a file called /root/pristatus

#vi /root/pristatus
PRI at asterisk server is down

4. Add the script file in crontab: it runs the script every minute

#crontab -e

*/1 * * * * bash /root/astPRIstatus.sh >/dev/null

Monday, August 30, 2010

Basic guide for Logrotate in Linux

Log files in Linux usually reside at /var/log... It keeps on growing so log management is essential. Log management is usually achieved using logrotate. Logrotate is managed by cronjobs in Linux.

For logrotate, you can configure /etc/logrotate.conf or create the individual configuration files for each application or each log file in /etc/logrotate.d

step 1:

Let's say, I have VOIP application 'asterisk' running on my system. Asterisk generates various log files under /var/log/asterisk directory. I would create astlog under /etc/logrotate.d to manage the log files.

#cd /etc/logrotate.d
#vi astlog
/var/log/asterisk/full /var/log/asterisk/messages /var/log/asterisk/debug /var/log/asterisk/*.log {
nocompress
daily
rotate 5
missingok
copytruncate
}

Here we listed all the log files to be managed and provided the attributes of the log management. Don't compress the log file, rotate the log file daily, max number of log rotation 5 ( i.e logfilexxx.1, logfilexxx.2, .... , logfilexxx.5). It only keeps 5 log files. With copytruncate option, the original log file is truncated in place after creating a copy, instead of moving the old log file and optionally creating a new one. It is useful when some program cannot be told its logfile and thus might continue writing(apending) to the previous log file.

[you can use #stat < filename > or # ls -l < filename > to check the inode number
copytruncate helps the log file to preserver it's inode(unique file number) ]

If you don't want to use copytruncate option, then you have to tell the program that log file has been recreated (with new INODE number). For example, in my case I could have done

#cd /etc/logrotate.d
#vi astlog
/var/log/asterisk/full /var/log/asterisk/messages /var/log/asterisk/debug /var/log/asterisk/*.log {
nocompress
daily
rotate 5
missingok
create

        postrotate
                /usr/sbin/asterisk -rx 'logger reload' > /dev/null 2> /dev/null
    endscript

}

Here, we are telling our program 'asterisk' to reload logger as new log file has been created after log rotation.

step 2:

By default, Logrotate is scheduled daily. You can find 'logrotate' under /etc/cron.daily

Let's look at /etc/crontab

# less /etc/crontab
SHELL=/bin/sh
PATH=/usr/bin:/usr/sbin:/sbin:/bin:/usr/lib/news/bin
MAILTO=root
#
# check scripts in cron.hourly, cron.daily, cron.weekly, and cron.monthly
#
-*/15 * * * * root test -x /usr/lib/cron/run-crons && /usr/lib/cron/run-crons >/dev/null 2>&1


The time to execute the scripts is managed by crontab. /usr/lib/cron/run-crons script controls the cron.hourly, cron.daily, cron.weekly and cron.monthly. run_crons runs every 15 minutes and ensures that cron jobs are taken care of.

In SLES, if you need to change the default daily time , you can go to YAST --> System --> /etc/sysconfig editor --> System --> Cron --> DAILY_TIME and change the time.

Let's say, I want logrotation to be done at OFF hours (10:30 pm) to avoid the possible load on the server, then, I can change DAILY_TIME to 22:30

For more information:

http://www.linuxtopia.org/online_books/suse_linux_guides/SLES10/suse_enterprise_linux_server_installation_admin/sec_suse_pakete.html

Tuesday, May 25, 2010

NFS (Network File System) in SUSE Linux

NFS server allows transparent acess to programs,files or storage space on the server.

Service:Program/Daemon:Start Script

Port mapper: /sbin/portmap : /etc/init.d/portmap

NFS Server: /usr/sbin/rpc.nfsd and /usr/sbin/rpc.mountd : /etc/init.d/nfsserver

NFS server configuration overview:

All configurations for NFS server are stored in the file /etc/exports. Client-side configuration takes place using the file /etc/fstab.
For the NFS server to start automatically when the computer is booted, the coressponding symbolic links in the runlevel directories must be generated. If you configure the NFS server with YaST, this is done automatically; otherwise, you need to generate them with

#insserv nfsserver

or
#chkconfig nfsserver on


In SUSE Linux, it's very easy to setup NFS server using YaST.

Configure and start NFS server:

To use YaST to configure the NFS server, start Yast and then select Network Services > NFS Server.

or
you can do everything manually
You need to set permissions for exported directories in /etc/exports

#vi /etc/exports
/var/backup *(ro)
/var/work *(rw,sync)


[
Note: if you are specifying the write permission, make sure that the directory has write permission for OTHER user. You can add the write permission to the directory/file by
#chmod o+w /var/work
]

Restart the server to reflect the changes

#rcnfsserver restart


Configure and start NFS Client:

To configure the NFS server, start Yast and then select Network Services > NFS Client. Add all the NFS server and mount point information.

I would recommend YaST for client setup, as it will automatically populate /etc/fstab with the provided NFS server information. You can check /etc/fstab file after you configure NFS client using YaST.

Monday, May 17, 2010

Router ARP Cache not releasing Server IP Address

I moved all IP address from old to a new server. However, I can't ping those servers as those IP addresses are not get updated due to arp cache issues ( IPs are cached on the router). How to solve this issue?

As ARP stands Address Resolution Protocol, it is used to resolve IP address to the corresponding Ethernet address. ARP maintains the mapping betweeen IP address and MAC address in a table in a memory called ARP cache. The entries in this table are dynamicaly added and removed. This is common and well known issue as most network admin configure their routers with a long ARP cache timeout. As a result my requests are going to the old server. If I move IP address, it may take hours before server can communicate. To get rid of this problem, we need to request the MAC address for it's own IP which will cause routers and other hardware update ARP cache. This is called a 'unsolicited ARP' or 'gratuitous ARP'

We can use 'arping command' to send an ARP request to resolve its won IP address.

#arping -U -I [Interface Name] [IP Address]

e.g
#arping -U -I eth1 192.168.1.2

where,
-U : Unsolicited ARP mode to update neighbours ARP cache. No replies are expected
-I eth1: Name of network device where to send ARP request packets.

Tuesday, April 27, 2010

Pure-FTP on SLES OES2

I will be talking about PURE-FTP

Installation:

You can install pure-ftp using YAST ( interactive tool for software installation on SLES/OES2)

# rpm -qagrep ftp
pure-ftpd-1.0.20-24.13

Configuration:

Configuration files:

/etc/pure-ftpd/pure-ftpd.conf
/etc/ftpusers


Make sure that service is running and will startup on next reboot

#chkconfig --list grep ftp
pure-ftpd 0:off 1:off 2:off 3:on 4:off 5:on 6:off

pure-ftpd is the name of the deamon/service. If there were 'off' on runleve 3 and 5, you could run the command

#chkconfig pure-ftpd on


/etc/pure-ftpd/pure-ftpd.conf file is very clean and nicely described.

Some of the parameters are:
ChrootEveryone yes
TrustedGID 100

NoAnonymous yes
UnixAuthentication yes

NoRename yes
and many more


Make sure that, after making some changes on this file, you have to restart the service.

#service pure-ftpd restart



Additional Tips:

What should I do to give ftp user (say ftpuser) access to some different directory?

Ans: Say, ftpuser is chrooted to /home/ftpuser. So, he won't be able to access other than /home/ftpuser. Say, ftpuser need access to /tmp/backup. You can create a shortcut for /tmp/backup in /home/ftpuser. However, 'ln' command will not help in FTP. You will have to use 'mount --bind ' command

#mount --bind /tmp/backup /home/ftpuser/backup

You can go to /etc/bash.bashrc and add the above line to mount the drive automatically at every reboot.


Have a fun :)

Friday, April 23, 2010

User add/remove in SLES

[Note: Don't use [ ] bracket in command line. You have been warned! ]

In SLES, adding user and removing user from the system requires some special flags to be used in command line.

To add user

#useradd -m [username]

-m flag enables to create /home/[username] directory

#useradd -r -m [username]

-r flag create a system account. A system account is an user with an UID between SYSTEM_UID_MIN and SYSTEM_UID_MAX as defined in /etc/login.defs, if no UID is specified. The GROUPS entry in /etc/default/useradd is ignored, too.

#passwd [username]
to create/change the password



Check out this after you add the user

#tail /etc/passwd --> This file contains user's information
testuser:x:1001:100::/home/testuser:/bin/bash
testuserdev:x:103:100::/home/testuserdev:/bin/false

testuser is created using only -m flag. Since, it is regular user, it is allowed to make login in the shell. It uses /bin/bash shell. [Remember, without shell you can't login. As shell is the middleware between user and kernel]

testuserdev is created using -r -m flag. Since, it is system user, it is not allowed to login in the shell. That's the reason, it has /bin/false




To remove the user
#userdel [username]

This will simply remove the user account credentials, but all the files in /home/[username] directory is not removed

#userdel -r [username]

-r flag forces to remove /home/[username] directory as well while deleting the user.

Monday, April 19, 2010

Grant access to Anonymous user for FTP read/write operation

In SLES, anonymous user is chrooted to /srv/ftp directory
In CentOS and RedHat linux, anonymous user is chrooted to /var/ftp directory

(when I say chrooted, I mean that '/sr/ftp' acts like '/' for anonymous user. This prevents anonymous user to hack into the root structure of the ftp server)

Make some changes on vsftpd.conf so that anonymous user can do read/write operation.
However, before making any changes, make a backup copy of vsftpd.conf


Server01:/srv/ftp # diff /etc/vsftpd.conf /etc/vsftpd.conf.bak
+anon_upload_enable=YES
-#anon_upload_enable=YES
+ anon_mkdir_write_enable=YES
-#anon_mkdir_write_enable=YES
+ anon_other_write_enable=YES
-#anon_other_write_enable=YES

Now you have to grant "WRITE" access to "other" user in /srv/ftp directory

#chmod o+w /srv/ftp
After that, restart the ftp server

#service vsftpd restart



Try login into the ftp server as anonymous user and try to use 'get' and 'put' commands to download and upload files.

:)



However, above process may not fullfill your needs.
So, try something different

--> Create FTP user

#useradd -r -m ftpuser
#passwd ftpuser

(you can give any name u like)

--> Chroot the user to it's home directory. i.e /home/ftpuser will be root for the ftp user

Server01:~ # diff /etc/vsftpd.conf /etc/vsftpd.conf.backup+chroot_list_enable=YES
-#chroot_list_enable=YES
+chroot_list_file=/etc/vsftpd.chroot_list
- #chroot_list_file=/etc/vsftpd.chroot_list


--> Restart ftp server

#service vsftpd restart



Now, try to login in ftp server using username and password. You can upload and download flawlessly now. You can't break into the root structure of the server as you are in chroot jail of /home/[username]
Have a fun! :)


Today, one of my friend asked me, can we define /var/ftp rather than /srv/ftp as the root directory for the ftpuser in SLES?
Of course, yes. However, you have to hack in /etc/vsftpd.conf

FTPserver#vi /etc/vsftpd.conf
........
write_enable=YES
local_enable=YES
chroot_list_enable=YES
#anonymous_enable=YES
#anon_world_readable_only=YES
....

userlist_enable=YES
userlist_deny=NO
local_root=/var/ftp/
listen_address=[ftp server IP address]


#vi /etc/vsftpd.user_list
add the list of the allowed ftp users over here

#service vsftpd restart


That's it. Try it. ;D

Tuesday, April 6, 2010

Firewall in SuSe Linux

How to check if Firewall is running or not?
Ans: You have to check if the Firewall service is running or not.

# /sbin/rcSuSEfirewall2 status
Checking the status of SuSEfirewall2 running

It is tellling you that Firewall is running. Firewall blocks telnet/ssh/scp by default.
You can stop firewall by following command

# /sbin/rcSuSEfirewall2 stop

Now try to telnet/ssh/scp into the server remotely, you should be able to make connection


You can use 'chkconfig' command to check the service startup status after reboot.

# chkconfig --list|grep firewall
SuSEfirewall2_init 0:off 1:off 2:off 3:off 4:off 5:off 6:off
SuSEfirewall2_setup 0:off 1:off 2:off 3:off 4:off 5:off 6:off

In my case, it says that firewall will not start automatically after next reboot as it's has off status in every terminal (1 thru' 5)


If you are not feeling comfortable playing with command line, you can use YAST for the firewall management. 'Firewall' is located in 'Security and Users' option.

#yast


Have a good one!

Friday, July 17, 2009

Add repository for YUM

While using yum in RHEL, it used to say :

Loading "security" plugin
Loading "rhnplugin" plugin
This system is not registered with RHN.
RHN support will be disabled.
Setting up Install Process
Parsing package install arguments
No package sendmail-cf available.
Nothing to do

I then thought, why not just change the repository for my YUM.. it's as easy as 123..

step 1. Go to /etc/yum.repos.d directory
step2. Create a file e.g myrepos
step3. Add the section header e.g [myrepos]
step4. Add the URL for the rpm repos; your final repos might look like this
#vi /etc/yum.repos.d/myrepos
[myrepos]
baseurl=http://www.city-fan.org/ftp/contrib/yum-repo/rhel5/i386/
gpgcheck=0
step5. If you've done that, the rest is simple. Upgrade your system by doing:
yum update
You can add new software by typing:
yum install
Or update installed software:
yum update
Or search for software in the local repository meta-data:
yum search
Or simply list all available software:
yum list available
From time to time you may want to save some diskspace:
yum clean

for more: http://dag.wieers.com/rpm/FAQ.php#B

Monday, April 20, 2009

step-by-step SUDO in linux

http://www.linuxhelp.net/guides/sudo/