• DevOps
    Case Study

    How we built a resilient multi-account, multi-cloud solution for a Health Tech service provider!

    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 we built a scalable Odoo solution for a Travel Tech service provider!

    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

    How we built a scalable Odoo solution for TravelTech service provider!

    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

  • 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

  • Our Story
  • Contact Us

Interested to collaborate?

Get in touch with us!

Ready to elevate your business with certified cloud expertise? Contact us today to learn how our team can help you leverage cloud technology to drive growth, streamline operations, and enhance security.

  • AWSAWS
  • Azure CloudAzure Cloud
  • Google CloudGoogle Cloud
  • Akamai CloudAkamai Cloud
  • OVHOVH
  • Digital OceanDigital Ocean
  • HetznerHetzner
  • Kubernetes Consultancy Services
  • K8s & Cloud native Solutions
  • 24/7 Infrastructure Monitoring
  • DevOps as a Service
  • Cloud CI/CD Solutions
  • White Labeled MSP Support
  • Our story
  • Life@SupportSages
  • Insights
  • Careers
  • Events
  • Contact Us

Connect with us!


LinkedInFacebookXInstagramYouTube

aws partneraws advanced partner
SupportSages

Copyright © 2008 – 2026 SupportSages Pvt Ltd. All Rights Reserved.
Privacy PolicyLegal TermsData ProtectionCookie Policy
Script to Block WHM Root Access from Unknown IPs

Script to Block WHM Root Access from Unknown IPs

Shafeer P

  • 7 min read
Script to Block WHM Root Access from Unknown IPs

Generating audio, please wait...

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

AWS LightSail Automatic Snapshots

AWS LightSail Automatic Snapshots
  • AWS
logo

Configure SSL on Tomcat 6/7/8/9

Configure SSL on Tomcat 6/7/8/9
  • Linux
logo

Enable TNEF on Linux Servers

Enable TNEF on Linux Servers
  • Howtos
  • Linux
  • Windows
logo

Fixing zPanel blank-screen error

Fixing zPanel blank-screen error
  • Apache
  • Linux
  • Troubleshooting
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.