A comprehensive bash script for complete Linux system anonymization and cleanup. This script prepares Linux systems for cloning, imaging, or template creation by removing all identifying information, logs, and temporary data.
- Distribution Detection: Automatically identifies the Linux distribution
- Package Manager Detection: Detects and uses the appropriate package manager
- Init System Detection: Identifies systemd, SysV, or OpenRC
- System logs and journals
- SSH host keys and known_hosts
- Machine IDs and hardware identifiers
- Network persistent rules and configurations
- Package manager caches
- User histories and caches
- Temporary files and directories
- Cloud-init data
- Docker/Snap cleanup (if present)
- Removes all user traces
- Clears shell histories
- Removes SSH authorized keys
- Cleans GPG keys
- Removes saved credentials
- Generates unique hostname based on MAC address and date
- Resets all machine identifiers
- Removes network interface mappings
Distribution | Version | Package Manager | Tested |
---|---|---|---|
RHEL/CentOS | 7, 8, 9 | YUM/DNF | ✅ |
Fedora | 35+ | DNF | ✅ |
Rocky Linux | 8, 9 | DNF | ✅ |
AlmaLinux | 8, 9 | DNF | ✅ |
Debian | 10, 11, 12 | APT | ✅ |
Ubuntu | 20.04, 22.04, 24.04 | APT | ✅ |
Arch Linux | Latest | Pacman | ✅ |
openSUSE | Leap, Tumbleweed | Zypper | ✅ |
Alpine Linux | 3.x | APK | ✅ |
wget -qO- https://gist.githubusercontent.com/[username]/[gist-id]/raw/anonymize-system.sh | sudo bash -s -- --auto-yes
- Download the script:
wget https://gist.githubusercontent.com/[username]/[gist-id]/raw/anonymize-system.sh
# or
curl -O https://gist.githubusercontent.com/[username]/[gist-id]/raw/anonymize-system.sh
- Make it executable:
chmod +x anonymize-system.sh
- Run the script:
sudo ./anonymize-system.sh
sudo ./anonymize-system.sh
The script will prompt for confirmation before proceeding.
sudo ./anonymize-system.sh --auto-yes
# or
sudo ./anonymize-system.sh -y
Skips all prompts and runs automatically (useful for automation).
sudo ./anonymize-system.sh --verbose
# or combine with auto-yes
sudo ./anonymize-system.sh -y --verbose
Provides detailed output of all operations.
Option | Description |
---|---|
--auto-yes , -y |
Skip confirmation prompts |
--verbose |
Enable verbose output |
--help , -h |
Display help message |
/var/log/*
- All system logs- Systemd journals
- Audit logs
- Application logs (Apache, Nginx, etc.)
- Boot logs
- Cron logs
- Persistent network rules
- NetworkManager connections
- Netplan configurations (Ubuntu)
- Interface configurations
- Shell histories (bash, zsh, fish)
- SSH known_hosts and authorized_keys
- Cache directories
- GPG keys
- Browser data
- Temporary files
/etc/machine-id
/var/lib/dbus/machine-id
/var/lib/systemd/random-seed
- SSH host keys
- Package caches
- Package databases
- Download caches
- Repository metadata
Perfect for creating clean VM templates in:
- VMware vSphere
- Proxmox
- VirtualBox
- KVM/QEMU
- Hyper-V
Prepare cloud-ready images for:
- AWS AMIs
- Azure Images
- Google Cloud Images
- OpenStack Images
- DigitalOcean Snapshots
Create clean base images for:
- Docker
- Podman
- LXC/LXD
Prepare systems for:
- Clonezilla
- dd imaging
- Acronis
- Ghost
---
- name: Anonymize Linux System
hosts: target_systems
become: yes
tasks:
- name: Download anonymization script
get_url:
url: https://gist.githubusercontent.com/[username]/[gist-id]/raw/anonymize-system.sh
dest: /tmp/anonymize-system.sh
mode: '0755'
- name: Run anonymization
command: /tmp/anonymize-system.sh --auto-yes
- name: Shutdown system
command: shutdown -h now
{
"provisioners": [
{
"type": "shell",
"inline": [
"wget -qO /tmp/anonymize.sh https://gist.githubusercontent.com/[username]/[gist-id]/raw/anonymize-system.sh",
"chmod +x /tmp/anonymize.sh",
"sudo /tmp/anonymize.sh --auto-yes"
]
}
]
}
anonymize_image:
stage: cleanup
script:
- wget -qO- https://gist.githubusercontent.com/[username]/[gist-id]/raw/anonymize-system.sh | sudo bash -s -- --auto-yes
tags:
- shell
- Root Check: Ensures script runs with proper privileges
- Error Handling: Continues operation even if individual commands fail
- Safe Deletion: Uses careful file removal with error checking
- Attribute Handling: Removes immutable attributes before deletion
- Confirmation Prompt: Requires user confirmation in interactive mode
After anonymization, the following will be automatically regenerated on first boot:
- Machine ID: New unique system identifier
- SSH Host Keys: New cryptographic keys for SSH
- Random Seed: New entropy seed for system
- Network Rules: New persistent network mappings
- Check hostname:
hostname
- Verify machine ID is empty:
cat /etc/machine-id
- Confirm SSH keys are removed:
ls -la /etc/ssh/ssh_host_*
- Check log directories:
du -sh /var/log/
Issue: Permission denied errors Solution: Ensure running with sudo or as root
Issue: Package manager detection fails Solution: Script will continue with generic cleanup
Issue: Hostname not changing Solution: Check if hostnamectl is available or /etc/hostname is writable
For troubleshooting, run with verbose output:
sudo bash -x ./anonymize-system.sh --verbose
- All logs will be permanently deleted
- SSH keys cannot be recovered
- User histories will be lost
- Cached credentials will be removed
Always ensure you have proper backups before running this script on production systems.
Contributions are welcome! Please:
- Fork the repository
- Create your feature branch
- Test on multiple distributions
- Submit a pull request
- Always test in a non-production environment first
- Create backups before running on important systems
- Shutdown immediately after running for imaging
- Document the anonymization in your deployment logs
- Verify the cleanup was successful before creating images
MIT License - See LICENSE file for details
System Administrator Tools
- Added automatic distribution detection
- Added package manager auto-detection
- Added init system detection
- Improved error handling
- Added verbose mode
- Extended distribution support
- Added Docker/Snap cleanup
- Improved network configuration reset
- Initial release
- Basic system cleanup
- Manual distribution selection
For issues, questions, or contributions, please open an issue on the GitHub Gist page.
This script is provided "as is" without warranty of any kind. Always test in a safe environment before using on production systems. The authors are not responsible for any data loss or system damage resulting from the use of this script.
Remember: After running this script, immediately shutdown the system and create your image/snapshot. Do not boot the system again before imaging to maintain the clean state.