Networking is Easy : connecting Multiple VMs / Servers / Containers/ devices on Single External IP in one command.
Tags: tutorial, linux, javascript, azure
Published: 2019-11-22 17:10:04

Please refer to my previous posts regarding creating your cloud service like digital ocean or google cloud. [Part 1](https://dev.to/manishfoodtechs/creating-your-cloud-infrastructure-like-google-cloud-and-digital-ocean-from-scratch-part-1-3gnm) and  [Part-2](https://dev.to/manishfoodtechs/creating-your-cloud-infrastructure-like-google-cloud-and-digital-ocean-from-scratch-part-2-5f7f) and [Part-3](https://dev.to/manishfoodtechs/creating-your-cloud-infrastructure-like-google-cloud-and-digital-ocean-from-scratch-part-3-coding-stuff-20eo) 

*In last 2 weeks I got 1500+ followers. Thank you for following me & emails.*

Today, I am going to tell you how you can connect your Internal VMs / Servers / Containers /devices on Single External Public IP. 

When it is required?

1. You want to have 'n' numbers of Internal VMs / Servers / Containers /devices but having only one Public IP. As in case of digital ocean / google cloud.

2. Getting another Public IP may cost between 3-5 USD per Month and tricky to connect with your internal VMs / Servers / Containers /devices .

I will not deep dive into boring topics like types of IPs, Gateways , network masks etc. So take breath. But I believe you should know at-least how to identify an IP: ** Public or Internal ?**

**Public IP Starts from : 1 to 126** - Like 104.24.108.61 of dev.to

***IP address starting from 127 is reserved for Localhost.***

And *everything else starting from 128 to 233  are internal IPs.*

If you are on linux do 'ifconfig' and on Windows do 'ipconfig' to see your machine address. 

Can you identify the below IP4 is Public IP or my Machine address ?

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/nwzh5qizo80ofxox7mkk.png)

This is my Machine Id. You are right.

Let us assume I have one Public IP : 104.24.108.61 and following Internal IPs of anythings (Internal VMs / Servers / Containers /devices) .

**anything1:** 192.168.189.1  --> visit it on internet: 104.24.108.61


**anything2:** 192.168.189.4 --> visit it on internet: 104.24.108.61:8080


**anything3:** 192.168.189.4:8844 --> visit it on internet: 104.24.108.61:9090


**anything4:** 192.168.189.4:9000 ---> visit it on internet: 104.24.108.61:9000

We will use [Iptables](https://en.wikipedia.org/wiki/Iptables) for anything3. This example uses my favorite Ubuntu. 

  *Syntaxes*:

***(a) CREATE : listen internal IP on Public IP:***

`sudo -E bash -c 'iptables -t nat -I PREROUTING -i eth0 -p TCP -d 104.24.108.61 --dport 9090 -j DNAT --to-destination  192.168.189.4:8844 -m comment --comment " anything3 listenting on dev.to ip"'`

***(b) CHECK: To check all IP rules:***

`sudo iptables -t nat -L PREROUTING --line-numbers`

(above, line-numbers is part of syntax)

***(c) DELETE : Delete IP rule:***
`sudo iptables -t nat -D PREROUTING 1'

Will delete rule 1 on screen.

***(d)PERSISTENT : To use rule after host restart:***
`sudo apt-get install netfilter-persistent
sudo netfilter-persistent save`

usage of Iptables is not limited to connecting  Internal IPs to Public Ips but also  you can use it for listening one Internal IP to another Internal IP.

I hope you people like the above article and learned something.

You are most welcome to join my team . You should contact me here [ form details ](https://forms.gle/pSCALuEWHus3pg9F6).


Contact email: Manishfoodtechs@gmail.com. 

Don't forget to Follow, like or tag me :).