Friday, April 20, 2018

Create and configure DHCP in Linux

DHCP works on D.O.R.A(Discover, Offer, Request, Acknowledge) process.

F.Q.D.N = Hostname + Domain Name

=> 67 port number for Server
    68 port number for Client

=> Set the hostname
  - # hostname <hostname> (But this is temporary)

=> Set Hostname permanent
  - # vim /etc/sysconfig/network
       Networking=Yes
       hostname <hostname>

=> By default the DHCP lease duration for wired Network is 6Days & for Wireless it is 7Hours.
=> To assign IP address 
  - # setup OR # netconfig
  - # service network restart  (to make the changes)
  - # ifconfig (to check the IP address)

=> Installation Packages.
  - dhcp*

=> Configuration File.
  - /etc/dhcpd.conf

=> Daemon/Service
  - dhcpd

=> Sample file of DHCP to copy it to the main configuration file.
  - /usr/share/doc-dhcp-<version>/dhcp.conf.sample

=> DHCP lease database file.
  - /var/lib/dhcpd/dhcpd.lease

Installation of DHCP Sever

# rpm -qa | grep dhcp (To check if it is already installed or not)
# yum install dhcp*
# cp /usr/share/doc-dhcp-<version>/dhcp.conf.sample /etc/dhcpd.conf
# gedit /etc/dhcpd.conf
Option routers 192.168.0.254   -/ Gateway
(Put # at NIS-Domain)
Domain name-server 192.168.0.254
range dynamic bootp 192.168.0.100 192.168.0.150
lease time 2
(save the file)
# service dhcpd restart
# service dhcpd status
# chkconfig dhcpd on
# chkconfig --list dhcpd
# netstat -ntpul | grep :67 (To show port status)

Now verify it from the client side.

Thursday, April 19, 2018

Configure rsync to sync data between two Linux/Unix server (Backup data)

What is Rsync

Rsync (Remote Sync) is a  most commonly used command for copying and synchronizing files and directories remotely as well as locally in Linux/Unix Systems,

With the help of rsync command you can copy and synchronize your data remotely and locally across directories, across disks and networks, perform data backups and mirroring between two Linux machines.



Advantages and features of rsync command
  1. It efficiently copies and sync files to or from a remote system.
  2. Supports copying links, devices, owners, groups and permissions.
  3. It’s faster than scp (Secure Copy) because rsync uses remote-update protocol which allows to transfer just the differences between two sets of files. First time, it copies the whole content of a file or a directory from source to destination but from next time, it copies only the changed blocks and bytes to the destination.
  4. Rsync consumes less bandwidth as it uses compression and decompression method while sending and receiving data both ends.
syntax of rsync command
# rsync options source destination

common options used with rsync commands
  1. -v : verbose
  2. -r : copies data recursively (but don’t preserve timestamps and permission while transferring data
  3. -a : archive mode, archive mode allows copying files recursively and it also preserves symbolic links, file permissions, user & group ownerships and timestamps
  4. -z : compress file data
  5. -h : human-readable, output numbers in a human-readable format


1. Copy/Sync Files and Directory Locally


[root@gcdc-nms source]# rsync -azvh /opt/source/ /tmp/backup/
sending incremental file list
created directory /tmp/backup
./
1
2
3
4
file1

sent 255 bytes  received 110 bytes  730.00 bytes/sec
total size is 0  speedup is 0.00
[root@gcdc-nms source]#


2. Copy/Sync Files and Directory to or From a Remote Server


[root@gcdc-nms source]# rsync -azvh /opt/source/ root@10.200.236.60:/tmp/backup/
root@10.200.236.60's password:
sending incremental file list
created directory /tmp/backup
./
1
2
3
4
file1

sent 255 bytes  received 110 bytes  42.94 bytes/sec
total size is 0  speedup is 0.00
You have new mail in /var/spool/mail/root
[root@gcdc-nms source]#


3. Rsync Over SSH

[root@gcdc-nms source]# rsync -azvhP -e ssh /opt/source/ root@10.200.236.60:/tmp/backup
/root@10.200.236.60's password:sending incremental file listcreated directory /tmp/backup./1           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=4/6)2           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=3/6)3           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=2/6)4           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=1/6)file1           0 100%    0.00kB/s    0:00:00 (xfer#5, to-check=0/6)sent 255 bytes  received 110 bytes  42.94 bytes/sectotal size is 0  speedup is 0.00
[root@gcdc-nms source]#


4. Use of –include and –exclude Options

[root@gcdc-nms source]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 19 09:58 file1.txt
-rw-r--r-- 1 root root 0 Apr 19 09:58 file2.txt
-rw-r--r-- 1 root root 0 Apr 19 09:58 file3.log
[root@gcdc-nms source]# rsync -azvP --include '*.txt' --exclude '*' -e ssh /opt/source/ root@10.200.236.60:/tmp/backup/
root@10.200.236.60's password:
sending incremental file list
created directory /tmp/backup
./
file1.txt
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=1/3)
file2.txt
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=0/3)

sent 130 bytes  received 53 bytes  21.53 bytes/sec
total size is 0  speedup is 0.00
[root@gcdc-nms source]#


5. Use of –delete Option


[root@gcdc-nms source]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 19 09:58 file1.txt
-rw-r--r-- 1 root root 0 Apr 19 09:58 file2.txt
-rw-r--r-- 1 root root 0 Apr 19 09:58 file3.log
[root@gcdc-nms source]# ll ../destination/
total 0
-rw-r--r-- 1 root root 0 Apr 18 13:01 file1
[root@gcdc-nms source]# rsync -azvP --delete -e ssh /opt/source/ root@10.200.236.60:/opt/destination/
root@10.200.236.60's password:
sending incremental file list
./
deleting file1
file1.txt
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=2/4)
file2.txt
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=1/4)
file3.log
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=0/4)

sent 181 bytes  received 72 bytes  29.76 bytes/sec
total size is 0  speedup is 0.00
You have new mail in /var/spool/mail/root
[root@gcdc-nms source]# ll ../destination/
total 0
-rw-r--r-- 1 root root 0 Apr 19 09:58 file1.txt
-rw-r--r-- 1 root root 0 Apr 19 09:58 file2.txt
-rw-r--r-- 1 root root 0 Apr 19 09:58 file3.log
[root@gcdc-nms source]#


6. Setup cron job for rsync

Generate the rsa file on client side (Destination). 

[root@gcdc-nms opt]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/rsync_key
Enter passphrase (empty for no passphrase): [Hit Enter]
Enter same passphrase again: [Hit Enter]
Your identification has been saved in /root/.ssh/rsync_key.
Your public key has been saved in /root/.ssh/rsync_key.pub.
The key fingerprint is:
SHA256:9hAlaN2gBCOmbIVv4QcGOiMSZp9yIHcVFgeWvHizo/4 root@gcdc-nms.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|o=*.++OB+o.      |
|**+*.==o.o.      |
|B+++oo...        |
|+oo+..+  .       |
|  . .. oS        |
|      o. o       |
|     . .  .      |
|    .            |
|   ...E          |
+----[SHA256]-----+

Now copy the key to server (Source) from Client (destination).

[root@gcdc-nms ~]# cat /root/.ssh/rsync_key.pub | ssh root@10.200.236.60 "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >>  ~/.ssh/authorized_keys"
[root@gcdc-nms ~]# cp /root/.ssh/rsync_key /opt/rsa/
[root@gcdc-nms ~]# ls /opt/rsa/
key  key.pub  rsync_key
[root@gcdc-nms ~]# 

Now check rsync on server (source)

[root@gcdc-nms ~]# rsync -azvP --delete -e "ssh -i /opt/rsa/rsync_key" /opt/source/ root@10.200.236.60:/opt/destination/              sending incremental file list

sent 63 bytes  received 12 bytes  13.64 bytes/sec
total size is 0  speedup is 0.00
You have new mail in /var/spool/mail/root
[root@gcdc-nms ~]#

Setup a cron job

First create a script which contains the rsync commands.

[root@gcdc-nms ~]# vim /opt/backup
rsync -azvP --delete -e "ssh -i /opt/rsa/rsync_key" /opt/source/ root@10.200.236.60:/opt/destination/
[root@gcdc-nms ~]# chmod 775 /opt/backup

Now create a cron job.

[root@gcdc-nms ~]# crontab -e
* * * * * sh /opt/backup


[Solved] Unable To Import OVA due to ISO mounted VM exported

1.  Uncompress the OVA file to local drive.
      
      2. Open the OVF file (i.e.: gc2nfe-clone.ovf) in notepad. This file contains all the configuration information to deploy the virtual machine.

      

 3. In the gc2nfe-clone.ovf file, Search for “vmware.cdrom.iso” and replace it with “vmware.cdrom.remotepassthrough”.













4.       Save the gc2nfe-clone.ovf file.

5.       Now move the .mf file to any other location.


        

       6.       Now import vm as OVF.

Tuesday, October 17, 2017

Check Linux/Unix System Version

Create executable file using this below mentioned script and execute file.

#!/bin/sh
[ -r /etc/lsb-release ] && . /etc/lsb-release

if [ -z "$DISTRIB_DESCRIPTION" ] && [ -x /usr/bin/lsb_release ]; then
# Fall back to using the very slow lsb_release utility
DISTRIB_DESCRIPTION=$(lsb_release -s -d)
fi

printf "Welcome to %s (%s %s %s)\n" "$DISTRIB_DESCRIPTION" "$(uname -o)" "$(uname -r)" "$(uname -m)"
echo ""
cat /etc/redhat-release


Set Static IP address In Ubuntu Machine.

Just Copy and paste below mentioned content to the file and assign executable permission to that file. and Execute file.

#!/bin/bash

getinfo()
{
  read -p "Enter IP Address:" staticip
  read -p "Enter netmask:" netmask
  read -p "Enter Default Gateway:" gateway
  read -p "Enter nameserver:" nameserver
}

writeinterfacefile()
{ 
cat << EOF > $1 
# This file describes the network interfaces available on your system
# and how to activate them.
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0 
iface eth0 inet static
address $staticip
netmask $netmask
gateway $gateway
nameserver $nameserver 
EOF
#don't use any space before of after 'EOF' in the previous line

  echo ""
  echo "Given information saved in '$1' file."
  echo ""
  echo "Now need to restart system to effect new changes"
  echo ""
  exit 0
}

file="/etc/network/interfaces"
if [ ! -f $file ]; then
  echo ""
  echo "The file '$file' doesn't exist!"
  echo ""
  exit 1
fi

clear
echo "Set up a static IP address"
echo ""

getinfo
echo ""
echo "Given Information is:"
echo "IP address is:  $staticip"
echo "Net Mask is:  $netmask"
echo "Default Gateway:   $gateway"
echo "IP Address is:   $nameserver"
echo ""

while true; do
  read -p "Are these informations correct? [y/n]: " yn 
  case $yn in
    [Yy]* ) writeinterfacefile $file;;
    [Nn]* ) getinfo;;
        * ) echo "Pleas enter y or n!";;
  esac
done