Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: How to Set up Multiple Shared IPs on a cPanel Server  (Read 3123 times)

0 Members and 1 Guest are viewing this topic.

vinayakk

  • Guest
How to Set up Multiple Shared IPs on a cPanel Server
« on: February 28, 2014, 06:14:39 am »
There are some situations where you may need to set up multiple shared IP addresses on a server.  Some reasons may include grouping accounts per IP or lowering the effect of DDoS attacks if it becomes necessary to block traffic to a specific interface.  Whatever the reason, multiple shared IP addresses on a cPanel server is rather easy to set up.

First you’ll want to reserve the IP address via WHM -> Reserved IPs.  Alternatively, simply drop them into /etc/reservedips, separated by line, ie:

    1.2.3.4

    4.5.6.7

    7.8.9.10

If you want to label the reserved IPs, set them up in /etc/reservedips:

    1.2.3.4=shared ip

    4.5.6.7=shared ip

… and so on

Now rebuild the IP pool:

    /scripts/rebuildippool

This will prevent the IPs from being used as dedicated IPs for individual cPanel accounts, but for multiple accounts to be able to use them as shared IPs you’ll need to add them to a pool.  Simply use the /var/cpanel/mainips/root file to list each IP on its own line.  When root creates an account on the server, the first IP address in that file will be used.  If you want a reseller to have multiple shared IPs as well, simply replace ‘root’ with the reseller’s username.

To take this configuration further, you may want to have the main IP rotate automatically so each IP in the pool gets equal use

Use the below script to automate the task via cron.

Quote
#!/usr/bin/env python
# Script to rotate the main IP of the server based on usage
# ThecPanelAdmin.com

import os, sys, re, socket
from operator import itemgetter

ipfile = '/var/cpanel/mainips/root'
tmpfile = '/var/cpanel/mainips/.root'

# Skip if this server doesn't use multiple main IPs

if not os.path.exists(ipfile):
    print "No support for multiple main IPs...skipping"
    sys.exit(0)

# Remove empty lines from file

f=open(ipfile, "r+")
fileString = f.read()
processedString = re.sub("\n\s*\n*", "\n", fileString)
f.seek(0)
f.write(processedString)
main_ips = f.readlines()
f.close()

# Get a list of IPs

f = open(ipfile)
main_ips = f.readlines()
f.close()

# Start an array for the IPs

ip_usage = { }

for ip in main_ips:
    ip = ip.strip()
    ip_usage[ip] = 0

# Now get each user's IP

for user in os.listdir('/var/cpanel/users/'):
    file = open('/var/cpanel/users/%s' % user)
    for line in file.readlines():
        if line.startswith('IP='):
            ip_address =  line.split('=')[1].strip()
            # Update the array (increment the IP's counter by 1)
              if ip_address in ip_usage:
                ip_usage[ip_address] += 1
            break
    file.close()

# Sort the ip_usage dict by value, putting the least-used IPs first
order_by_usage = sorted(ip_usage.items(), key=itemgetter(1))

file = open(tmpfile, 'w')

for ip in order_by_usage:
    ip = ip[0]
    file.write("%s\n" % ip)

file.close()

# Compare the two files and replace

current_list = sum(1 for line in open(ipfile))
new_list = sum(1 for line in open(tmpfile))

if current_list == new_list:
    os.rename(tmpfile, ipfile)

Installation steps:

    Copy the above script to a file and name it rotate_main_ips.py

    chmod 755 rotate_main_ips

    echo '*/30 * * * * /usr/bin/rotate_main_ips' > /etc/cron.d/rotate_main_ips

    service crond reload

Try  :)