Recover Files over SFTP from a Linux Server That Will Not Boot
Boot a rescue system, mount the disk read-only and download your data over SFTP before repairing or reinstalling a server that no longer boots.
When a server no longer boots, the data on its disk is usually still intact. This guide shows how to get it off: you start the server in a rescue system, mount the original disk read-only and download your files over SFTP. It works the same way for Ubuntu, Debian, AlmaLinux and Rocky Linux installations and with any provider that offers a rescue mode or lets you boot from an ISO image.
Copy the data first, then attempt repairs. A repair that goes wrong can destroy files that were still readable.
Before you start
- Do not reinstall or "rebuild" the server from your hosting control panel. That overwrites the disk.
- If your provider offers snapshots, take one now. It gives you a way back if a later step makes things worse.
- Make sure your own computer has enough free space, and an SFTP client: the
sftpcommand on Linux, macOS and current Windows, or a graphical client.
Step 1: Boot a rescue system
A rescue system is a small Linux that runs from memory, so the server's own disk is not in use and can be inspected. There are two common ways to start one:
- Rescue mode. Most providers have a rescue or recovery option in the hosting control panel. Enabling it and rebooting starts the rescue system and gives you temporary root login details, or accepts the SSH key stored in your account.
- A live ISO. If there is no rescue mode, attach a Linux live image (for example a Debian live image or a dedicated rescue distribution) as a virtual CD, reboot, and open your provider's web console to watch it start.
If you booted a live ISO, you have to enable remote access yourself from the web console. Set a root password, start the SSH server and note the IP address:
passwd
systemctl start ssh
ip -brief address
On some images the service is called sshd. If the system has no address, the network was not configured automatically; consult the documentation of the image you booted.
Step 2: Log in to the rescue system
The rescue system has a different SSH host key from your installed system, so your SSH client will warn that the host identification has changed. That is expected here. Connect without touching your saved host keys:
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@203.0.113.10
Use these options for the rescue session only. The password is the temporary one from your provider or the one you just set, not the root password of your installed system.
Step 3: Find your partitions
List all disks with their file systems:
lsblk -f
The device names depend on the platform: /dev/vda1 on most virtual servers, /dev/sda1 on others, /dev/nvme0n1p1 on NVMe disks. Look for the largest partition with a file system such as ext4 or xfs; that is normally the root file system. The examples below use /dev/vda1.
Some layouts need one more step before a file system becomes visible:
- LVM (type
LVM2_member, the default on AlmaLinux and Rocky Linux): activate the volume groups withvgchange -ay, runlsblk -fagain and use the/dev/mapper/...device that appears. - Encrypted disks (type
crypto_LUKS): unlock withcryptsetup open /dev/vda3 recovery, enter the passphrase, and use/dev/mapper/recovery. - Software RAID (type
linux_raid_member): assemble the arrays withmdadm --assemble --scanand use the resulting/dev/mddevice.
Step 4: Mount the disk read-only
mkdir -p /mnt/recovery
mount -o ro /dev/vda1 /mnt/recovery
ls /mnt/recovery
You should see the familiar top-level directories: etc, home, var and so on. If home or var are empty, they live on separate partitions; mount those as well, for example mount -o ro /dev/vda3 /mnt/recovery/home.
If the mount fails, read the kernel's explanation:
dmesg | tail -n 20
A file system that was not shut down cleanly may refuse a read-only mount because its journal needs to be replayed. You can skip the journal with mount -o ro,noload for ext4 or mount -o ro,norecovery for XFS. Files that were being written at the time of the crash may then be inconsistent, but everything else is readable.
Do not run fsck -y at this point, although many older guides start with it. It rewrites file system structures without asking, and on a damaged disk that can turn recoverable files into lost ones. It belongs after the copy.
Step 5: Download your files with SFTP
Run this on your own computer. In the sftp session, change to the mounted disk and fetch directories recursively:
sftp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@203.0.113.10
sftp> cd /mnt/recovery
sftp> get -r home
sftp> get -r etc
sftp> bye
In a graphical client, choose the protocol SFTP, port 22, user root, the rescue password, and open the remote directory /mnt/recovery.
What to copy
| Path under /mnt/recovery | Contains |
|---|---|
home, root | User files, SSH keys, shell history |
etc | All configuration, including web server, mail and TLS certificates |
var/www, srv, opt | Websites and applications |
var/lib/mysql, var/lib/postgresql | Raw database files |
var/spool/cron | User crontabs |
usr/local | Locally installed scripts and software |
Raw database files are not dumps. They can normally be used again only by the same database software in the same major version, with the whole data directory restored together. Copy them completely and note the version, which you can read from the package lists under var/lib/dpkg or var/lib/rpm if you copy those too.
Faster alternatives for many files
SFTP transfers file by file and does not keep ownership. For a large number of small files, a compressed archive streamed over SSH is much quicker and preserves owners and permissions inside the archive. Run it on your own computer:
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@203.0.113.10 \
'tar -C /mnt/recovery -czf - home etc var/www' > recovery.tar.gz
If rsync is available in the rescue system and on your computer, it can resume an interrupted transfer:
rsync -a --partial -e 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
root@203.0.113.10:/mnt/recovery/var/www/ ./www/
Before you continue, open a few of the downloaded files and compare sizes with du -sh on both sides.
Step 6: Repair or reinstall
With the data safe, unmount the disk in the rescue session:
umount /mnt/recovery
Now a repair attempt is reasonable. For an ext4 file system:
fsck -y /dev/vda1
XFS uses xfs_repair /dev/vda1 instead. Only ever check a file system that is not mounted. Afterwards switch rescue mode off, or detach the ISO, in your hosting control panel and reboot. If the system still does not start, reinstall it and restore your files from the copy.
Troubleshooting
- Connection refused: the SSH server in the rescue system is not running, or the image filters incoming connections. Start the service from the web console and check the image's documentation for its firewall.
- Permission denied for root with the right password: some live images allow root logins by key only. In the web console, add
PermitRootLogin yesto the live system's/etc/ssh/sshd_configand restart its SSH service; this affects only the rescue system, which is gone after the reboot. - Input/output errors while copying: the disk or the storage behind it is failing. Copy the most important data first, in small batches, and contact your provider before doing anything that writes to the disk.
- The disk is not listed by
lsblk: the rescue system cannot see it. That is a problem on the provider's side or a missing driver in the live image; try the provider's own rescue mode.
Frequently asked questions
Why mount read-only?
It guarantees that nothing you do during the copy changes the disk: no accidental deletion, no updated timestamps, no writes to an already damaged file system. You lose nothing, because recovery only needs to read.
Can I repair the system from the rescue environment instead of copying everything?
Often, yes: a full disk, a broken /etc/fstab entry or a bad boot loader configuration can be fixed by mounting the disk read-write and editing files. Take a copy of anything irreplaceable first, because you are working on the only instance of your data.
Is plain FTP an option?
No. FTP sends passwords and data unencrypted and would need extra software in the rescue system. SFTP is part of the SSH server that is already running.
How long does the rescue system stay available?
Until the next reboot, unless your provider limits rescue sessions. Everything you configured in it, including the root password you set, is lost when the server boots normally again.