• DevOps
    Case Study

    How we helped a development company rebuild DevOps for efficiency and scale.

    READ CASESTUDY
    icon

    24/7 DevOps as a Service

    Round-the-clock DevOps for uninterrupted efficiency.

    icon

    Infrastructure as a Code

    Crafting infrastructure with ingenious code.

    icon

    CI/CD Pipeline

    Automated CI/CD pipeline for seamless deployments.

    icon

    DevSecOps

    Integrated security in continuous DevOps practices.

    icon

    Hire DevOps Engineers

    Level up your team with DevOps visionaries.

    icon

    Consulting Services

    Navigate success with expert DevOps consulting.

  • TechOps
    Case Study

    How a US hosting leader scaled with us!

    READ CASESTUDY

    WEB HOSTING SUPPORT

    icon

    HelpDesk Support

    Highly skilled 24/7 HelpDesk Support

    icon

    Product Support

    Boost your product support with our expertise.

    MANAGED SERVICES

    icon

    Server Management

    Don’t let server issues slow you down. Let us manage them for you.

    icon

    Server Monitoring

    Safeguard your server health with our comprehensive monitoring solutions.

    STAFF AUGMENTATION

    icon

    Hire an Admin

    Transform your business operations with our expert administrative support.

    icon

    Hire a Team

    Augment your workforce with highly skilled professionals from our diverse talent pool.

  • CloudOps
    Case Study

    How we helped a Private Deemed University in India, save US $3500/m on hosting charges!

    READ CASESTUDY
    icon

    AWS Well Architected Review

    Round-the-clock for uninterrupted efficiency

    icon

    Optimize

    Efficient CloudOps mastery for seamless cloud management

    icon

    Manage

    Automated CI/CD pipeline for seamless deployments

    icon

    Migrate

    Upgrade the journey, Migrate & Modernize seamlessly

    icon

    Modernize

    Simplify compliance complexities with our dedicated services

    icon

    FinOps as a Service

    FinOps as a Service

  • SecOps
    Case Study

    Enabling financial grade platforms through strategic cloud modernisation.

    READ CASESTUDY
    icon

    VAPT

    Vulnerability Assessment and Penetration Testing

    icon

    Source Code Review

    Ensuring source code security ans safe practices to reduce risks

    icon

    Security Consultation

    On demand services for improving server security

    icon

    System Hardening

    Reduced vulnerability and proactive protection

    icon

    Managed SoC

    Monitors and maintains system security. Quick response on incidents.

    icon

    Compliance as a Service

    Regulatory compliance, reduced risk

  • K8s
  • Insights
    Case Study

    How we helped a Private Deemed University in India, save US $3,500/m on hosting charges!

    READ CASESTUDY
    icon

    Blog

    Explore our latest articles and insights

    icon

    Case Studies

    Read about our client success stories

    icon

    Flipbook

    Explore our latest Flipbook

    icon

    Events

    Join us at upcoming events and conferences

    icon

    Webinars

    Watch our educational webinar series

  • Contact Us

Interested to collaborate?

Get in touch with us!

Contact us today to learn how our team can help you leverage our managed cloud and DevOps services so you can focus on growing your business.

  • White Label Managed IT Services for MSPs
  • White Label MSP Support Services
  • Managed HelpDesk Services
  • White Label WordPress Maintenance Services
  • Outsourced WebHosting Support
  • Hosting HelpDesk Support Services
  • cPanel Server Management
  • Plesk Server Management
  • DevOps Automation Services
  • DevOps Containerization Services
  • DevOps Engineering Services Experts
  • DevOps Maturity Assessment
  • DevOps Testing Services & Automation
  • DevOps Implementation Services
  • DevOps Transformation Services
  • White Label Kubernetes IT Services
  • Cloud Automation Services
  • Cloud Modernization Services
  • Database Migration Services
  • DevOps Outsourcing Services

AWS

  • AWS DevOps Services for Scalable Cloud
  • AWS Well-Architected Review
  • AWS Migration Services

Azure

  • Azure DevOps Services & Automation
  • Azure Migration Services

Google Cloud

  • Google Cloud Managed Services
  • Google Cloud Migration Services
  • Google Cloud Platform Services
  • AWSAWS
  • Azure CloudAzure Cloud
  • Google CloudGoogle Cloud
  • Akamai CloudAkamai Cloud
  • OVHOVH
  • Digital OceanDigital Ocean
  • HetznerHetzner
  • Managed DigitalOcean Cloud
  • Managed OVH Cloud
  • Managed Hetzner Cloud
  • Managed Akamai Cloud
  • Oracle Managed Services
  • Our story
  • Life@SupportSages
  • Insights
  • Careers
  • Events
  • Contact Us
  • Sitemap

aws partneraws advanced partner
LinkedInFacebookXInstagramYouTube
SupportSages

Copyright © 2008 - 2026 SupportSages Pvt Ltd. All Rights Reserved.
Privacy PolicyLegal TermsData ProtectionCookie Policy

Script to Block WHM Root Access from Unknown IPs

Author Profile
Shafeer P
  • 4 min read
Script to Block WHM Root Access from Unknown IPs

Generating audio, please wait...

Promotional banner

Introduction

In a Shared Hosting cPanel server, the WHM root access is required for authorized technicians and API calls (like WHMCS) only. In majority cases, the shell access or root level access is restricted only to the web hosting support or server management professionals. Being a popular web hosting control panel, cPanel is exposed to various type of security attacks. Some times an infected or hacked client device with the logins,  can be catastrophic if they are able to access the WHM as root user. So limiting the WHM Root access to known IPs provides additional layer of security. Here I am making an attempt to develop a bash script to restrict the root level WHM access after cross checking it with the known IPs list.

 

How the Script Works?

This script can actively monitor the cPanel access_log (/usr/local/cpanel/logs/access_log) for any WHM Root access and block the source IP if it is not present in the custom whitelist.

The Shell (bash) Script

For servers having CSF installed and running

#!/bin/bash
## Block WHM root access from non-whitelisted IPs
## Version: 20160503
 
PID_FILE=
/var/tmp/whm_watch
.pid
WATCH_FILE=
/usr/local/cpanel/logs/access_log
WHITE_LIST=
/root/scripts/whm_watch_whitelist
LOG_FILE=
/root/scripts/whm_watch_log
 
for
CUR_PID 
in
`
cat
$PID_FILE 2> 
/dev/null
`;
        
do
        
if
grep
$0 
/proc/
$CUR_PID
/cmdline
&> 
/dev/null
        
then
        
if
[[ 
"$1"
== 
"kill"
]]
                
then
                
pkill -TERM -P $CUR_PID
                
exit
0
                
fi
        
echo
"WHM Watch is already running... (PID $CUR_PID)"
        
exit
1
        
fi
done
echo
"WHM Watch started (PID $$)"
echo
$$ > $PID_FILE
tail
-f $WATCH_FILE | 
grep
--line-buffered -E 
' - root '
| 
grep
--line-buffered -Po 
"^[0-9\.]*"
| 
grep
--line-buffered -vf $WHITE_LIST | 
xargs
-r -i -n 1 csf -d {} 
"do not delete - Suspicious WHM root access"
&>> $LOG_FILE
 

For servers without CSF / Directly block in IPTABLES

#!/bin/bash
## Block WHM root access from non-whitelisted IPs
## Version: 20160503
PID_FILE=
/var/tmp/whm_watch
.pid
WATCH_FILE=
/usr/local/cpanel/logs/access_log
WHITE_LIST=
/root/scripts/whm_watch_whitelist
LOG_FILE=
/root/scripts/whm_watch_log
for
CUR_PID 
in
`
cat
$PID_FILE 2> 
/dev/null
`;
        
do
        
if
grep
$0 
/proc/
$CUR_PID
/cmdline
&> 
/dev/null
        
then
        
if
[[ 
"$1"
== 
"kill"
]]
                
then
                
pkill -TERM -P $CUR_PID
                
exit
0
                
fi
        
echo
"WHM Watch is already running... (PID $CUR_PID)"
        
exit
1
        
fi
done
echo
"WHM Watch started (PID $$)"
echo
$$ > $PID_FILE
tail
-f $WATCH_FILE | 
grep
--line-buffered -E 
' - root '
| 
grep
--line-buffered -Po 
"^[0-9\.]*"
| 
grep
--line-buffered -vf $WHITE_LIST | 
xargs
-r -i -n 1 
/root/scripts/deny_in_iptable
.sh {} &>> $LOG_FILE
 

The above script require the following additional script to log and block the IP address:

/root/scripts/deny_in_iptable.sh

#!/bin/bash
if
iptables -A INPUT -s $1 -j DROP
  
then
echo
"[`date`] IP Address $1 blocked in IPTABLES"
  
service iptables save &> 
/dev/null
fi

Note: The above script requires executable permission.

Configuration

The allowed IP address can be specified in the following formats:

/root/scripts/whm_watch_whitelist

123.123.1.2
^111.222
^222.

123.123.1.2: This IP is allowed.

^111.222: Any IPs begin with 111.222 are allowed.

^222: Any IPs begin with 222 are allowed.

Start/Stop the Script

The script can be started and backgrounded in the following way:

# sh /root/scripts/whm_watch &

The & will send the process into background.

To stop the backgrounded script:

# sh /root/scripts/whm_watch kill

Known Bugs & Limitations

  • Need to restart the script to reload IP whitelist modifications.
  • Need to Stop then Start to restart the script.
  • The script may try multiple times to block a single IP. This may create duplicate entries in IPTABLES.
  • Commenting not allowed in whitelist
  • No Email alerts for IP block instance.
  • CIDR notation is not supported in whitelist

Get 24/7 expert server management

  • cPanel
  • Linux
  • Security
Promotional banner
Promotional banner

AWS LightSail Automatic Snapshots

AWS LightSail Automatic Snapshots
  • AWS
logo

Fixing zPanel blank-screen error

Fixing zPanel blank-screen error
  • Apache
  • Linux
  • Troubleshooting
logo

How to Configure SSL on Apache Tomcat (6, 7, 8 & 9)

How to Configure SSL on Apache Tomcat (6, 7, 8 & 9)
  • Linux
logo

How to Create Ready-to-Deploy cPanel Servers with OnApp

How to Create Ready-to-Deploy cPanel Servers with OnApp
  • cPanel
  • Howtos
  • Linux
logo

Posts by Shafeer P

Shafeer is currently working as Senior System Engineer at SupportSages. He is capable of tackling time-consuming issues quickly with his advanced scripting abilities. His unchallenged expertise in solving complex issues in a cut-and-dried way makes him a dependable man in the team. The attitude and philosophy he shows on his workplace make his colleagues call him 'Mr. Perfectionist'. Furthermore, he is a travel and photography enthusiast, and loves to spend his leisure times on music and DIY arts.