Saturday, April 2, 2011

How to see the load average of all the vps on the node

[root@abc ~]# vzlist -o veid,laverage
      CTID       LAVERAGE
       150       0.00/0.02/0.02
       350       0.00/0.00/0.00
       450       0.00/0.05/0.06

      5095      0.00/0.05/0.04
      6970      0.01/0.09/0.06
      7170      57.00/56.97/56.91

How to change the timezone on redhat linux system

First of all take the backup of the current timezone settings
mv /etc/localtime  /etc/localtime.bkp

Now. Suppose you want to change the timezone to Asia/Kolkatta
ln -sf /usr/share/zoneinfo/Asia/Kolkatta /etc/localtime

Thats it. It should be fixed now.

Friday, April 1, 2011

Syntax of linux command rsync

Syntax of rsync :

rsync -avzh --timeout=14000 /backups/private/7545/    178.216.48.23:/vz/private/7545/

If the ssh port of the destination server is different. Then use the following options:

rsync -avzh --rsh='ssh -p2222' --timeout=14000 /vz/private/    208.91.129.133:/vz/private/

or

rsync -avzh -e "ssh -p 2222" --timeout=14000 /vz/private/ 208.91.129.133:/vz/private/

What is the use of screen command in linux? How to use it?

What is the use of  screen command in linux? How to use it? The following post explains it.
Screen is a powerful tool useful for working in a remote environment. You can start a screen and execute the commands inside the screen. So even if the connection goes down between your system and remote system, you can later attach to the screen. The command will be working inside the screen.

Installation:
#yum install screen

#screen
Then execute the command you want

To detatch screen
press "Ctrl+a"
then press "d"

To list the sceeen sessions
#sceen -ls

[root@server ~]# screen
[detached]
[root@server ~]# screen -ls
There is a screen on:
        3063.pts-0.server       (Detached)        #Lists the existing sceen session ids.
1 Socket in /var/run/screen/S-root.
[root@server ~]#

To attach a screen again
# screen -r screen_id
[root@server ~]# screen -r 3063.pts-0.server

To name a screen while creating
# screen -S name_of_screen

To wipe off a dead screen
#screen -wipe screen_id

If you have only one screen. You can attach it by executing
#screen -x

Tuesday, March 29, 2011

How to setup a NTP server client on a redhat linux rhel5 system

The following post explains how to setup a  NTP server and client on redhat linux systems like rhel5. It will also work on centos, fedora, Suse linux etc.

[root@server ~]# rpm -qa | grep ntp
ntp-4.2.2p1-9.el5_3.2

NTP Server IP : 192.168.1.19
Client IP     : 192.168.1.60

Client Configuration :
/etc/ntp.conf   - primary conf file.

open the /etc/ntp.conf file and give the IP of the NTP server as follows
[root@work1 ~]# vi /etc/ntp.conf
server  192.168.1.19               #Give IP address or FQDN of the NTP server
                                   #Remove all the server entries from the /etc/ntp.conf  file.
:wq                                   #Save the file

Restart the service:
[root@work1 ~]# service ntpd start   

[root@work1 ~]# ntpq -np
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*192.168.1.19    LOCAL(0)        11 u   78  128  377    0.682   -0.142   0.071      # * Means default
 127.127.1.0     .LOCL.          10 l   44   64  377    0.000    0.000   0.001
[root@work1 ~]#
It may take sometime.  Or may require a reboot.

To make an immediate effect. Use ntpdate command
[root@work1 ~]# ntpdate -b NTP_Server_IP_or_FQDN

[root@work1 ~]# ntpdate -b 192.168.1.19
29 Mar 15:40:17 ntpdate[6138]: the NTP socket is in use, exiting
[root@work1 ~]#

How to install and configure OpenVPN on redhat rhel5 linux or Centos 5

I got this script from here. But it has some problems. Version conflicts is there and it is designed to run in a VPS. So I have done some changes. Its better you run command by command. This steps will work in other operating systems like fedora, Centos, etc.

#!/bin/bash
# Quick and dirty OpenVPN install script
# Tested on Centos 5.x 32bit, openvz minimal CentOS OS templates
# Please submit feedback and questions at support@vpsnoc.com

# John Malkowski vpsnoc.com 01/04/2010

ip=`grep IPADDR /etc/sysconfig/network-scripts/ifcfg-venet0:0 | awk -F= '{print $2}'`

wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.3.6-1.el5.rf.i386.rpm
rpm -iv rpmforge-release-0.3.6-1.el5.rf.i386.rpm
rm -rf rpmforge-release-0.3.6-1.el5.rf.i386.rpm

yum -y install openvpn openssl openssl-devel
cd /etc/openvpn/
cp -R /usr/share/doc/openvpn-2.1.4/easy-rsa/ /etc/openvpn/
cd /etc/openvpn/easy-rsa/2.0/
chmod +rwx *
. ./vars
./clean-all
source ./vars

./build-ca
Dont forget to give Y for creating certificates.
./build-key-server server
./build-dh
cp keys/{ca.crt,ca.key,server.crt,server.key,dh1024.pem} /etc/openvpn/

./build-key client1
cd keys/

Just paste the codes below into ur terminal.
client="
client
remote $ip 1194                #Here you may have to specify the remote IP.
dev tun
comp-lzo
ca ca.crt
cert client1.crt
key client1.key
route-delay 2
route-method exe
redirect-gateway def1
dhcp-option DNS 8.8.8.8
verb 3"

echo "$client" > $HOSTNAME.ovpn

tar czf keys.tgz ca.crt ca.key client1.crt client1.csr client1.key $HOSTNAME.ovpn
mv keys.tgz /root

opvpn='
dev tun
server 192.168.2.0 255.255.255.0         #Change the network to your network
ifconfig-pool-persist ipp.txt
ca ca.crt
cert server.crt
key server.key
dh dh1024.pem
push "route 192.168.2.0 255.255.255.0"
push "redirect-gateway"
comp-lzo
keepalive 10 60
ping-timer-rem
persist-tun
persist-key
group nobody
daemon'

echo "$opvpn" > /etc/openvpn/openvpn.conf

echo 1 > /proc/sys/net/ipv4/ip_forward
Replace the network with your IP
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE   #Change the network to your network
iptables-save > /etc/sysconfig/iptables
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf

If internet is not working then execute the following command replacing "main_ip" with ip address
#route add -net 192.168.1.0/24 gw main_ip

/etc/init.d/openvpn start

Download /root/keys.tgz using winscp or other sftp/scp client such as filezilla
Create a directory named vpn at C:\Program Files\OpenVPN\config\ and untar the content of keys.tgz there
Start openvpn-gui, right click the tray icon go to vpn and click connect

All credit goes to the real owners. I just  edited it. If you are working with a new version, Change the version number. Have fun. For me, This worked beautifully.

Monday, March 28, 2011

How to write a script to select a choice from a list of choices using bash scripting

This script will work in all linux/unix operating systems like Redhat, Fedora, Centos, Solaris, Ubuntu, Debian etc, if they are having bash shell or sh.
See the example given below :

#!/bin/bash
PS3='Enter the choice:'
LIST="Choice1 Choice2 Choice3 Quit"
select i in $LIST
        do
                if [ "$i" = Choice1 ]
                then
                        echo "Your choice is: $i"
#                       break
                elif [ "$i" = Choice2 ]
                then
                        echo "Your choice is: $i"
#                       break
                elif [ "$i" = Choice3 ]

                then
                        echo "Your choice is $i"
#                       break
                elif [ "$i" = Quit ]
                then
                        exit
                fi
        done

How to write a simple script to do a ping check using bash scripting

This script will work in all linux/unix operating systems like Redhat, Fedora, Centos, Solaris, Ubuntu, Debian etc, if they are having bash shell or sh.
First we have to set a variable status and an IP to check.

See the example given below :

#!/bin/bash
#Purpose : TO check whether a system is up and working or not using a simple ping check.
IP=192.168.0.21
status=1
until [ "$status" -eq "0" ]
do
        ping -c 1 $IP > /dev/null
        status=`echo $?`
                if [ "$status" = "1" ]

                then
                        echo "The machine $IP seems to be down!"
                else
                        echo "The machine $IP is up"
                fi
done

How to write a until loop script in bash scripting

This script will work in all linux/unix operating systems like Redhat, Fedora, Centos, Solaris, Ubuntu, Debian etc, if they are having bash shell or sh.
The syntax is as follows :
until [ "$Condition_variable1" operation "$Condition_variable2" ]
do
    commands to execute
done
The thing to note is "$Condition_variable1" should be greater than "$Condition_variable12" here in this example.

See the example given below :

#!/bin/bash
NUM=10
MIN=5
until [ "$NUM" -eq "$MIN" ]
do
        echo $NUM
        let "NUM -= 1"
done

How to write a while loop script in bash scripting

This script will work in all linux/unix operating systems like Redhat, Fedora, Centos, Solaris, Ubuntu, Debian etc, if they are having bash shell or sh.
The syntax is as follows :
while [ "$Condition_variable1" operation "$Condition_variable2" ]
do
    commands to execute
done
The thing to note is "$Condition_variable1" should be less than "$Condition_variable12" here in this example.

See the example given below :

#!/bin/bash
#Purpose : To test the while loop
MIN=0
MAX=10
while [ "$MIN" -lt "$MAX" ]
do
        echo $MIN
        let "MIN += 1"
done