How to Change OS on a VPS Without Losing Data
In this article
This guide walks you through changing the operating system on a virtual private server (VPS) while keeping all your files, databases, and settings intact. You'll learn the safest method—backing up everything, reinstalling the OS, then restoring—plus how to avoid common pitfalls that lead to data loss.
What You'll Need
- SSH access to your current VPS (root or sudo user).
- VPS control panel access (from your hosting provider) to reinstall the OS.
- Enough free space on your local machine or a cloud storage service for backups (at least the size of your used disk space).
- A temporary location to store backups, such as your local computer or an external server.
- Basic familiarity with the command line and your server's file structure.
Step-by-Step
- Inventory your data. Log into your VPS via SSH and list what needs to be backed up: website files (usually in
/var/www), configuration files (/etc), databases, cron jobs, and any application data. Rundf -hto see disk usage. - Back up your files. Create a compressed archive of your important directories. For example:
tar -czf backup-files.tar.gz /var/www /etc/nginx /etc/apache2
Adjust paths to match your setup. If you have large directories, consider excluding cache or log folders to keep the archive manageable. - Back up your databases. If you use MySQL or MariaDB, dump all databases:
mysqldump --all-databases --single-transaction -u root -p > all-databases.sql
For PostgreSQL, usepg_dumpall > all-databases.sql. Store these SQL files alongside your file archive. - Record your server configuration. Note your IP address, current kernel, and any custom firewall rules (
iptables-saveorufw status). Also list installed packages withdpkg --get-selections(Debian/Ubuntu) orrpm -qa(CentOS/RHEL) so you can reinstall them later. - Download backups to a safe location. Use SCP or SFTP to copy the backup files to your local machine or another server. For example, from your local terminal:
scp user@your-vps-ip:/path/to/backup-files.tar.gz .
Verify the files are complete and can be opened locally before proceeding. - Reinstall the operating system. Log into your VPS control panel (provided by your hosting company) and look for a "Reinstall OS" or "Change OS" option. Select your desired new OS (e.g., Ubuntu 22.04 instead of Debian 11) and confirm. This process will wipe the entire disk, so double-check that your backups are safe.
- Log in to the fresh OS. Once the reinstall completes, you'll receive new login credentials. SSH into your VPS with the new credentials. The server will have a clean system with no previous data.
- Restore your files. Upload your backup archive to the new server (reverse of step 5) and extract it to the appropriate locations. For example:
tar -xzf backup-files.tar.gz -C /
This will overwrite default config files, so be careful—if you only want to restore certain directories, extract selectively. - Restore your databases. Import the SQL dump into your database server. For MySQL:
mysql -u root -p < all-databases.sql
Make sure the database server is installed and running first (install it if needed). - Reinstall your applications and packages. Use your package manager to install the same software you had before. For Debian/Ubuntu, you can restore the package list:
apt-get updatethenapt-get installeach package you need. For CentOS/RHEL, useyum installordnf install. This step is manual—there's no universal restore. - Reapply your server configuration. Copy back any custom configuration files you backed up (e.g.,
/etc/nginx/nginx.conf). Adjust them for the new OS version if paths or syntax have changed. - Test everything. Start your web server, database, and other services. Check that your websites load, databases are accessible, and cron jobs run. Use
systemctl statusto verify services are active. - Update and secure your new system. Run
apt-get update && apt-get upgrade(or equivalent) to apply security patches. Reconfigure your firewall and SSH settings to match your previous security posture.
Common Mistakes
- Not backing up everything. Forgetting to include configuration files or database dumps leads to lost settings and data.
- Storing backups only on the VPS itself. If the reinstall wipes the disk, you lose your backups too. Always copy them off-site.
- Restoring files without adjusting for OS differences. Configuration file paths or syntax may change between OS versions, so blindly copying can break services.
- Skipping a test of the backups. A corrupted backup is useless. Verify archives open and SQL dumps import before wiping the server.
- Not recording your installed packages. Without a list, you'll forget what you had installed, leading to missing functionality.
Tips
- Use a separate backup server or a cloud storage service (e.g., S3, Google Drive) for redundancy.
- If you have a managed VPS, check if your provider offers automatic snapshots—take one before reinstalling as an extra safety net.
- Consider using a configuration management tool like Ansible or Puppet to automate package and config deployment, making future migrations easier.
- Schedule regular backups even after the OS change to protect against future issues.
- Test your restore procedure on a temporary VPS first if you're unsure about the process.
Troubleshooting
- Backup files won't extract. Check the archive integrity with
tar -tzf backup-files.tar.gz. If it's corrupted, you'll need to redo the backup from your old server (if it's still running) or use a snapshot. - Database import fails. Ensure the database server is running and the user has sufficient privileges. If you get syntax errors, the dump may be from a different DB version—try importing with
--forceor convert the dump. - Websites show 404 or 500 errors. Check file permissions and ownership. After extracting, run
chown -R www-data:www-data /var/www(adjust user/group for your web server). Also verify that your web server config points to the correct document root. - Services won't start. Look at logs in
/var/log/syslogor/var/log/messages. Often it's a missing dependency or a syntax error in a config file. Usesystemctl status service-namefor details. - Can't SSH into the new server. Confirm the new IP address and credentials from your provider. If your firewall blocks port 22, access via the control panel's console to adjust rules.
- Your old IP address changed. Update your DNS records to point to the new IP. This is expected if you switched data centers or regions.
Frequently Asked Questions
Can I change the OS without reinstalling?
No, changing the operating system on a VPS requires a full reinstall because the root filesystem is replaced. The backup-and-restore method is the only safe way to keep your data.
Will my IP address change after reinstall?
It depends on your provider. Some keep the same IP if you reinstall on the same VPS, but if you migrate to a new server or data center, the IP may change. Check with your host.
How long does the whole process take?
Backup time depends on data size, reinstall typically takes 5–15 minutes, and restore can take another 10–30 minutes. Plan for 1–2 hours total, plus testing.
What if my provider doesn't offer a reinstall option?
You can manually install a new OS by booting from a rescue ISO or using a custom ISO, but this is more complex. Contact your provider's support for guidance.
Do I need to back up email accounts?
If you run your own mail server, yes—mail data is usually in /var/mail or /var/spool/mail. Include it in your file backup and restore it similarly.
Can I switch between different Linux distributions (e.g., Ubuntu to CentOS)?
Yes, but be prepared for differences in package management and config paths. Your data will restore fine, but you'll need to reinstall software using the new distro's tools.