Tuesday, 17 May 2016

Add the bulk IPs on linux using perl script

Add the bulk IPs on linux using perl script



To add the bulk IP subnet's (/22,/21) on linux server using below perl script very easy

Example subnet : 192.168.2.0/22

     192.168.8.0/24
    192.168.9.0/24
        192.168.10.0/24
    192.168.11.0/24



perl -e 'my $cntr= 101; for $o3(8 .. 11){for $o4(1 .. 254){ `ifconfig eth0:$cntr 192.168.$o3.$o4 netmask 255.255.255.0`;$cntr++}}'

Setup Trusted connection between two linux servers


Setup Trusted connection between Two linux servers




Server 1 (198.158.5.6)

         user  :  linux


Server 2 (198.160.43.8)


        user  : unix


Now i am going to create the Trusted connection from Server1 - linux user to server2 to unix user.



Before setup trusted connection generate the key pair. if already exist it will over write. please take care of that.


Server 1 (198.158.5.6)

         user  :  linux


ssh keypar generation :-

Switch to 'linux' user in my case and run 'ssh-keygen -t rsa'

Enter and continue with default option. Please don't enter 'passphrase' when it's asking.

root]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/linux/.ssh/id_rsa):
Created directory '/home/linux/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/linux/.ssh/id_rsa.
Your public key has been saved in /home/linux/.ssh/id_rsa.pub.
The key fingerprint is:
63:c7:83:08:2a:85:92:15:e3:4f:18:49:66:c2:ff:9e linux@dedicated2388.inmotionhosting.com
The key's randomart image is:
+--[ RSA 2048]----+
|o.Oo             |
| X.+             |
|+ = o            |
|.. = . . o       |
|. . o . S +      |
| .   . . o .     |
|    . .          |
|     E           |
|                 |
+-----------------+



Server 2 (198.160.43.8)


        user  : unix

Follow the same steps like same as above one. switch to 'unix' user and run 'ssh-keygen -t ras'





Now main steps begin here. I want to enable the trusted connection from 'Server 1' (198.158.5.6) linux user to 'Server 2' (198.160.43.8) unix user.

So login  to 'Server 1' (198.158.5.6)  linux user and change directory to '/home/linux/.ssh/' and copy 'id_rsa.pub' kay


$ cd /home/linux/.ssh/

[test@dedicated2388 .ssh]$ cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAuz3zLfxxxxxxxxxxhxqnQ44YC7L7XnkzLIuLJSDUYEglkNDrdmOx7Me/4o6ZLyrfxh0cQ0LvYvIBQMQUSLE0aIfk1cGNYnNt7R9BiHZfei/3FHcRQ3jUlkd4DMWadJDsASe5uYni6BecmLrSS1PEr/EUBBYXgcngdvP5bOfOd/78u9/VN6+D2FmCNBvCdr6024/qHZ8e7/PPDq7oZ1/Xle1gWGVIWK2wYs8sgGVWRVWNeZf86uHFFHUm2Fd8PGSupUP+9Z9Wd8XQ== linux@xxxxxxxxx



Copy above key and login to 'Server 2' (198.160.43.8)  unix user and change directory to '/home/unix/.ssh/' , create 'authorized_keys' file and paste the above key in 'authorized_keys'  file.


$ cd /home/unix/.ssh/

$ vi authorized_keys

Now give the exicute permission to 'authorized_keys'

$ chmod -x authorized_keys


Now i am able connect the 'Server 2' (198.160.43.8) unix user from Server 1 (198.158.5.6) linux user with out asking the password.


server1-linux $ ssh unix@198.160.43.8

Monday, 16 May 2016

IP GRE Tunnel in linux step by step



IP Tunneling between linux servers using IP GRE Tunneling




Network A : 192.168.92.6 (Public IP) ( IPs tunnel from the server )-source
             
       10.200.10.1  (Private IP)

       180.168.94.1 (GateWay)

Network B : 180.168.94.5 (Public IP) IPs tunnel to server )- destination

                    10.200.10.2  (Private IP)
                
                    180.168.94.1(GateWay)

Required modules  :  ip_gre

Server1 : 192.168.92.6 ( source side configuration )


Step1 :-

Before going to IP GRE Tunnel you need to check 'ip_gre' module is loaded or not, other wise install the 'ip_gre' module.

lsmod | grep ip_gre
modprobe ip_gre

Step2 :-

Enable IP Forwarding. By default IP Forwarding set to '0' Means it's Disabled. you need to enable the IP Forwarding by set the value to '1' .

echo 1 >> /proc/sys/net/ipv4/ip_forward  

Enable  = 1
Disable = 0

Step3 :-

Enable 'proxy_arp'. By default it set to '0'. you need to set to '1'.

Proxy_arp :-

When a computer wants to send an ip packet it first decides, wether the destination ip address is on the same LAN or the packet must be sent to a router (which must be on the same LAN).
Before it can deliver the packet to the LAN, it must know the recipient's Ethernet MAC address.
In order to determine the MAC address it sends an ARP broadcast package to the LAN asking "who has ip address a.b.c.d".
Normally only the computer with that ip address answers.
Proxy arp changes that: A computer doing proxy arp answers all ARP requests with its own MAC address.
The asking computer retrieves the MAC address and sends the packet to that computer.

echo 1 >> /proc/sys/net/ipv4/conf/eth0/proxy_arp

Enable  = 1
Disable = 0

Step4 :-

Add the IP Tunnel on source server

Syntax  :  ip tunnel add [Tunnel name] mode gre local [source ip] remote [destination ip] ttl 255


ip tunnel add gre-mta250 mode gre local 192.168.92.6 remote 191.168.94.5 ttl 255


Step5 :-

Up the added gre tunnel

Syntax  :  ip link set [Tunnel name] up

ip link set gre-node21 up


Step 6 :-

Add the private or local IP to gre tunnel. you can add any IP for local addredd, but add same subnet IPs on  sourece and destination .

ip addr add 10.11.22.1 dev gre-node21

route add -net 10.11.22.0/30 dev gre-mta250

In my case i will use 10.11.22.1 as source IP  10.11.22.2 as destination local ip.


Step 7 :-

Route the Destination Server IP through source server gateway.

Syntax : route add [destination IP] gw [gateway of source server]  dev [interface]

route add 180.168.94.5 gw 180.168.94.1  dev eth0



Step8 :-

Route what are the IPs we need to move to destination side.

Syntax  : route add [Moved IP]  gw 10.11.22.1 dev gre-mta250

route add 64.20.35.68  gw 10.11.22.1 dev gre-mta250

you need to down the interface of routed IP on source side.

Suppose 64.20.35.68 interface is eth0:1

ifconfig eth0:1  down


Destination side configuration  : -




Network B : 180.168.94.5 (Public IP) IPs tunnel to server )

           10.200.10.2  (Private IP)




Step1 :-

Before going to IP GRE Tunnel you need to check 'ip_gre' module is loaded or not, other wise install the 'ip_gre' module.

lsmod | grep ip_gre
modprobe ip_gre

Step2 :-

Enable IP Forwarding

echo 1 >> /proc/sys/net/ipv4/ip_forward


Step3 :-

Enable 'proxy_arp'

echo 1 >> /proc/sys/net/ipv4/conf/eth0/proxy_arp


Step4 :-

Add the IP Tunnel on source server

Syntax  :  ip tunnel add [Tunnel name] mode gre local [source ip] remote [destination ip] ttl 255


ip tunnel add gre-mta250 mode gre local 191.168.94.5  remote 192.168.92.6 ttl 255

Step5 :-

Up the added gre tunnel

Syntax  :  ip link set [Tunnel name] up

ip link set gre-node21 up

Step 6 :-

Add the private or local IP to gre tunnel. you can add any IP for local addredd, but add same subnet IPs on  sourece and destination .

ip addr add 10.11.22.1 dev gre-node21

route add -net 10.11.22.0/30 dev gre-mta250

In my case i will use 10.11.22.2 as source IP  10.11.22.1 as destination local ip.


Step7 :-

Route add source IPs to gateway of destination server.

Syntax  : route add [IPs to Tunnel]  gw [gateway of destination server]  dev eth0

route add 64.20.35.67 gw 180.168.94.1  dev eth0


Step8 :-

Create the route table

echo 99999 example >>/etc/iproute2/rt_tables
ip route add default via 10.11.22.2 dev gre-node21 table example

you can give route table name as any thing.


Step9 :-

Add the Tunnel IPs to route table.


ip rule add from x.x.x.x/32 table example
ip rule add from x.x.x.x/32 table example

Step10 :-

Add the IPs on destination server.

ifconfig eth0:1 x.x.x.x netmask 255.255.255.0
ifconfig eth0:2  x.x.x.x  netmask 255.255.255.0