Tuesday, March 29, 2011

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

Advertisements

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

Advertisements

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

Advertisements

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

Advertisements

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

Advertisements

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

Advertisements

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

Friday, March 25, 2011

How to change the timezone in redhat or centos linux system

Advertisements

Suppose your zone is CDT and you want to Change it to IST

The steps are as follows :

[root@server ~]# date
Fri Mar 25 00:28:23 CDT 2011
Remove the localtime file [You can take a backup copy if you want]
[root@server ~]# rm -rf /etc/localtime

Link the desired zone to /etc/localtime
[root@server ~]# ln -s /usr/share/zoneinfo/Asia/Kolkata /etc/localtime
[root@server ~]# date
Fri Mar 25 10:59:02 IST 2011
[root@server ~]#

Thats it.

If you want to see time of any Country, Japan[say]. You can get it as.

[root@server ~]# zdump Japan
Japan  Fri Mar 25 14:32:48 2011 JST
[root@server ~]#

You can also do this with tzselect command. Read how to change timezone using the tzselect

How to create or bind a range of IP address to a redhat or centos linux system

Advertisements

Suppose you have a server with IP 192.168.1.19 with OS rhel5

And you want to bind 10 IPs starting from 192.168.1.20 to 192.168.1.30

All you have to do is create the following file and add these four lines.

[root@server ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0-range0
IPADDR_START=192.168.1.20
IPADDR_END=192.168.1.30

CLONENUM_START=0
NETMASK=255.255.255.0
[root@server ~]#

IPADDR_START is the starting IP in the range.
IPADDR_END is the last IP in the range.
CLONENUM_START is the number that will be assigned to the first IP alias interface (eth0:0 in this example).