Protect VPS from Ransomware: Read-Only System Configuration with OverlayFS
Detailed guide on configuring a read-only system on VPS using OverlayFS to defend against ransomware, including setup, verification, and troubleshooting steps.
Image credit: ricardorv30 · Pixabay Content License
Introduction to Protecting VPS from Ransomware with Read-Only Systems
In DevOps environments, Virtual Private Servers (VPS) are frequent targets of ransomware attacks—malicious software that encrypts data and demands payment. A highly effective mitigation strategy is configuring a read-only system, where system directories cannot be modified, even if malware executes with root privileges. OverlayFS is a Linux file system mechanism that enables a read-write layer on top of a read-only base, preserving the integrity of the original system.
This guide walks through deploying OverlayFS on a Linux system (Ubuntu 22.04) to configure a VPS as read-only, preventing ransomware and unwanted changes. This strategy is especially suitable for environments such as web servers, APIs, or services that do not require persistent data writes.
Setting Up OverlayFS on a VPS
OverlayFS operates by combining two directories: a read-only root directory and a read-write directory to store changes. During boot, the system is mounted using OverlayFS, allowing normal operations while ensuring all modifications are confined to the writable layer.
1. Install and Verify OverlayFS
OverlayFS is included in the Linux kernel by default. Verify its availability with:
modprobe overlay
lsmod | grep overlay
If the command returns output, OverlayFS is ready. If not, load the kernel module.
2. Create Directory Structure
Set up the following directories:
/root/overlayfs/ro: Read-only root directory./root/overlayfs/rw: Temporary writable directory./root/overlayfs/upper: Upper layer (writable)./root/overlayfs/work: Work directory.
Run these commands:
mkdir -p /root/overlayfs/{ro,rw,upper,work}
cp -a /etc /root/overlayfs/ro/
cp -a /usr /root/overlayfs/ro/
cp -a /bin /root/overlayfs/ro/
cp -a /lib /root/overlayfs/ro/
cp -a /lib64 /root/overlayfs/ro/
3. Mount OverlayFS
Use the mount command to attach OverlayFS:
mount -t overlay overlay -o lowerdir=/root/overlayfs/ro,upperdir=/root/overlayfs/upper,workdir=/root/overlayfs/work /root/overlayfs/rw
The /root/overlayfs/rw directory now serves as the mounted read-only system. All changes are stored in /root/overlayfs/upper.
Verification and Troubleshooting
1. Check System Functionality
Run these commands to verify:
df -h
mount | grep overlay
Ensure /root/overlayfs/rw is mounted correctly and has appropriate space.
2. Handle Common Errors
Some common issues include:
- OverlayFS mount failure: Verify the kernel module is loaded. Run
modprobe overlayif missing. - Missing upper or work directories: Confirm all directories are created.
- Permission denied to write to upper directory: Check permissions on
/root/overlayfs/upper.
3. Fix Boot-Time Mount Issues
To ensure proper mounting at boot, add this line to /etc/fstab:
overlay /root/overlayfs/rw overlay lowerdir=/root/overlayfs/ro,upperdir=/root/overlayfs/upper,workdir=/root/overlayfs/work 0 0
Then run mount -a to mount all entries in /etc/fstab.
References
- Automated VPS Backup to Google Drive and Dropbox with Simple Bash Script
- Building a VPS-Based Global Ad Blocker with DNS over HTTPS to Block Ads and Trackers on All Devices
Assumptions: This guide assumes the user has root access and is running Ubuntu 22.04. Steps may vary depending on the Linux distribution.
Notes: This strategy does not protect user data. Persistent data should be stored outside the read-only system (e.g., in a separate volume or cloud storage).
Related infrastructure & engineering guides: NVMe Cloud VPS for Docker, Nginx & Databases
Recommended NVMe Cloud VPS Plans to Deploy This Setup
Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.
VPS-3E
10% VAT included
- CPU: 2 vCPU
- RAM: 4 GB
- SSD: 50 GB NVMe
