How to recover ubuntu from disk. How to repair the Grub bootloader in Ubuntu

Ubuntu, like many other Linux distributions, uses GRUB2 as the boot loader. If GRUB2 breaks, for example, if you install Windows after installing Ubuntu, or when you overwrite the MBR, you will not be able to boot Ubuntu.

GRUB2 can be easily repaired using an Ubuntu Live CD or USB stick. This process is different from restoring the GRUB Legacy bootloader on older Linux distributions.

The process below should work with all versions of Ubuntu. It has been tested on Ubuntu 16.04 and Ubuntu 14.04 versions.

GUI method: Boot Repair ###

Boot Repair is a GUI application that can fix GRUB2 with just one click. Ideal solution for most users.

If you have the media from which you installed Ubuntu, insert it into your computer, reboot, and boot from the removable media. If you don't have the media, boot the Ubuntu Live CD and burn it to disc or create a bootable USB stick.

When booting, click "Try Ubuntu" to load a working desktop environment.

Make sure your internet connection is working before proceeding. You may need to select a Wi-Fi network and enter a password.

Open a terminal from Dash and run the following commands to download and install Boot Repair:

Sudo apt-add-repository ppa: yannubuntu / boot-repair sudo apt-get update sudo apt-get install -y boot-repair boot-repair

Boot Repair will automatically scan the system after running the boot-repair command. After that, click the Recommended repair button to repair GRUB2.

You can also do advanced settings here, however the Ubuntu wiki does not recommend doing this unless you know exactly what they are responsible for. The recommended option allows you to fix most of the problems automatically, and choosing the wrong settings can only make the situation worse.

Boot Repair will start working. You might be prompted to open a terminal and copy / paste a few commands.

Just follow the instructions on the screen. Follow all instructions Boot Repair and click Forward to go to the next screen. The utility will guide you through all the necessary steps for recovery.

After Boot Repair finishes work, restart your computer. Ubuntu should start normally.

Terminal method

If you are not afraid to get your hands dirty, you can fix the system through the terminal. You will need to boot from a Live CD or USB stick, just like the method described above. Make sure the Ubuntu version on the disk matches the version installed on your computer. For example, if you had Ubuntu 14.04 installed, you should use the Ubuntu 14.04 Live CD.

After the system boots up, open a terminal. Identify the partition of the disk where Ubuntu is installed using one of the following commands:

Sudo fdisk -l sudo blkid

Following is the output of both commands. In the case of fdisk -l, the Ubuntu partition can be identified by the word Linux in the System column. In the case of using the blkid command, the partition is determined by the use of the file system ext4.

If your computer has multiple Linux partitions in the format ext4, take note of their size and the order in which they are listed.

Run the following commands to mount your Ubuntu partition on / mnt / ubuntu, replacing / dev / sdX # with the name of the device where your Ubuntu partition is installed above:

Sudo mkdir / mnt / ubuntu sudo mount / dev / sdX # / mnt / ubuntu

In the example above, the Ubuntu partition is located at / dev / sda1. This is the first partition on the first hard drive.

Important! If you have a separate boot partition, skip the command above and mount the boot partition in / mnt / ubuntu / boot. If you do not know if you have a separate boot partition, then most likely it does not exist =).

Run the following command to re-install GRUB2 from the Live CD, replacing / dev / sdX with the hard drive name above. Do not include the number. For example, if you used / dev / sda1 above, just enter / dev / sda.

Sudo grub-install --boot-directory = / mnt / ubuntu / boot / dev / sdX

Reboot, Ubuntu should boot normally.

For details on how to use the chroot command to access corrupted Ubuntu system files and repair GRUB2, see the Ubuntu wiki page.

If you forgot your password, accidentally deleted important files or decided to restore Ubuntu, then all these operations can be performed using the built-in system tools or simple utilities.

Password reset

If you have forgotten your Ubuntu password, reset it and install a new security key to regain access.

If you have forgotten the username, you can view it using the "cat / etc / passwd" command. As you can see, no additional tools are needed, Linux systems are quite easy to learn.

System rollback

There is no System Restore button on Ubuntu that you can click to roll back to an earlier state, so each case has to be considered separately. Let's take a look at the most common errors after which Ubuntu requires recovery: incorrect system updates and damaged GRUB bootloader.

If the system does not start, then try to repair the bootloader using the boot-repair utility. Connect your Ubuntu stick to your computer, start the system and open a terminal. Run a few commands to install the boot-repair program:

  • sudo add-apt-repository ppa: yannubuntu / boot-repair.
  • sudo apt-get update.
  • sudo apt-get install boot-repair.

After closing the console, run the boot-repair utility. Select the "Recommended recovery method" mode for the program to find and fix the error.

If a failure occurred while updating the system, then you can fix it as follows:

  1. Start root mode as you would when recovering a password.
  2. Enable recording with the command "sudo mount -rw -o remount".
  3. Use the sudo dpkg –configure –a command to start the Package Manager to update and rebuild data packages,

You will have to wait a long time, about 30-40 minutes. But after the completion of the procedure, the system will start without errors, already being updated.

File recovery

Another useful feature on Ubuntu is recovering deleted files. Use Photorec or TestDisk to recover lost data. They work in about the same way, allowing you to recover most of the files deleted from the system.

  1. Open a console and install TestDisk with the command "sudo apt-get install testdisk".
  2. Run the utility with the "sudo testdisk" program. The TestDisk package includes the Photorec utility, if you want you can run it with the "sudo photorec" command.
  3. Stop logging in the first TestDisk window.
  4. Select the partition of the disk where you want to search for deleted files. Click "Proceed".
  5. Select the type of partition table - usually Intel / PC Partition.
  6. Select the "Analyze" operating mode.
  7. Start a quick search - item "Quick Search".
  8. Avoid searching for partitions created by Windows Vista by entering "N". After completing the analysis, click "Continue".
  9. Display the list of found files by pressing the "P" key (all actions in the Latin layout).

Sometimes users are faced with the loss or accidental deletion of the necessary files. When such a situation arises, there is nothing left to do but try to restore everything using specialized utilities. They scan hard disk partitions, find damaged or previously erased objects there and try to get them back. Such an operation is not always successful due to fragmentation or complete loss of information, but it is definitely worth trying.

Today we would like to talk about the available solutions for the Ubuntu operating system, which runs on the Linux kernel. That is, the considered methods are suitable for all distributions based on Ubuntu or Debian. Each utility functions differently, so if the first one does not bring any effect, you should definitely try the second one, and we, in turn, will present the most detailed guides on this topic.

Method 1: TestDisk

TestDisk, like the next utility, is a console tool, however, not the whole process will be carried out by entering commands, some implementation of the graphical interface is still present here. Let's start with the installation:

  1. Go to the menu and run "Terminal"... You can also do this by holding down the hotkey Ctrl + Alt + T.
  2. Add the sudo apt install testdisk command to start the installation.
  3. Next, you need to confirm your account by entering a password. Please note that the entered characters are not displayed.
  4. Wait until all required packages are downloaded and unpacked.
  5. After the new field appears, you can run the utility itself as the superuser, and this is done through the sudo testdisk command.
  6. Now you get into some simple GUI implementation via the console. Control is carried out by arrows and keys Enter... Start by creating a new log file, so that in case of something you will be aware of what actions were performed at a particular moment.
  7. When displaying all available disks, select the one on which the lost files will be restored.
  8. Select the current partition table. If you can't make a choice, check out the developer's tips.
  9. You get to the action menu, objects are returned through the section "Advanced".
  10. It remains only with the help of arrows Up and Way down define the section of interest, and using To the right and To the left specify the desired operation, in our case it is "List".
  11. After a short scan, a list of files on the partition will appear. Lines highlighted in red mean that the object has been damaged or deleted. You just have to move the selection line to the file of interest and click on WITH to copy it to the desired folder.

The functionality of the utility considered is simply amazing, because it can recover not only files, but entire partitions, and also perfectly interacts with NTFS, FAT file systems and with all Ext versions. In addition, the tool not only returns data, but also corrects the errors found, which avoids further problems with the drive's performance.

Method 2: Scalpel

For a novice user, it will be a little more difficult to deal with the Scalpel utility, because here each action is activated by entering the appropriate command, but there is no need to worry, because we will describe each step in detail. As for the functionality of this program, it is not tied to any file systems and works equally well on all types of them, and also supports all popular data formats.

  1. All necessary libraries are downloaded from the official repository via sudo apt-get install scalpel.
  2. Next, you will need to enter the password for your account.
  3. After that, wait until the addition of new packages is complete until the input line appears.
  4. Now you need to customize the configuration file by opening it through a text editor. The following line is used for this: sudo gedit /etc/scalpel/scalpel.conf.
  5. The fact is that by default the utility does not work with file formats - they must be connected by uncommenting the lines. To do this, simply remove the gratings in front of the desired format, and after completing the settings, save the changes. After completing these steps, Scalpel will repair the specified types normally. This should be done so that the scan takes as little time as possible.
  6. You just have to define the hard disk partition where the analysis will be performed. To do this, open a new "Terminal" and write the lsblk command. Find the designation of the required drive in the list.
  7. Start recovery via the command sudo scalpel / dev / sda0 -o / home / user / Folder / output /, where sda0- the number of the desired section, user- the name of the user folder, and Folder- the name of the new folder into which all recovered data will be placed.
  8. When finished, go to your file manager (sudo nautilus) and check out the found objects.

As you can see, it will not be difficult to understand Scalpel, and after getting acquainted with the control, activating actions through commands does not seem so difficult. Of course, none of the above tools guarantee a complete recovery of all lost data, but at least some of them should be returned by each utility.

A malfunction of the operating system can lead to many problems. This includes lost information, which takes time to recover, and the inability to work at a computer. And also a spoiled mood, and often - wasted nerves. Many people know what to do if Windows does not work well. The situation with operating systems based on Linux is more complicated and interesting. So what to do if you need an urgent Ubuntu recovery? Let's explore this question.

Perhaps after starting the computer, you were greeted by a screen stating a serious problem. Or you have encountered an error already during work - this is not so important. It is really necessary to investigate the source of the problem. To recover Ubuntu, pay attention to each of the following situations. Perhaps one of them resembles yours?

  1. Incorrectly selected drivers or their "crooked" installation. This also includes any cases when the equipment does not work properly due to software. Have you installed the driver from an untrusted source? Have you noticed the hardware model that the driver supports?
  2. Problems after system update. Despite the carefulness of the Ubuntu system developers, perhaps all your hardware does not support new components, or the OS itself is unstable ... It is advisable to use Ubuntu LTS versions (with long-term support).
  3. Your own mistake. A lot of problems can be attributed to this point, which become a source of breakdown and a "flying" OS. For example:
  • An important system file was deleted by mistake.
  • You forgot your root password.
  • The partition table was accidentally changed, or even overwritten.
  • Installed the problematic program.

In some cases, it will be more difficult to reanimate the system, in others, there will be their own ways of a quick solution. Let's move on to considering specific solutions.

Incorrectly selected drivers

Typical problems of this kind are associated with video drivers. These are not very common problems at the moment. But everyone can face them. To fix some situations (for example, artifacts on the screen), a forced change of the video mode will help.

We will add a kernel parameter that will change the graphics settings. In the file "/ etc / default / grub" there is a variable "GRUB_ CMDLINE_ LINUX_DEFAULT ".

You need to add the value "vga = 0x314" to it. As a result, the variable should look like this:

GRUB_CMDLINE_LINUX_DEFAULT = ”quiet splash vga = 0x314

After the operation, all that remains is to update the configuration using the command:

In another case - if the screen resolution is incorrectly determined - you can create a file "~ / .xprofile", which should have the following content:

xrandr –output VGA-0 –mode 1920 × 1200 –rate 60.

Moreover, instead of "output", it is desirable to immediately indicate where (to which port) the monitor is connected. The "xrandr" command will help determine this. Also, the "mode" option is used to specify the resolution - instead of the standard resolution, you must specify your own. Desirable - supported by the monitor by default. For image problems on Ubuntu, a full system restore may not be necessary if you follow these guidelines.

Problems after system update

This situation causes additional troubles. The problem is that it is not immediately visible. Recovering Ubuntu after an unsuccessful update may be necessary at any time when using the distribution. Long-term maintainers try to make sure that compatibility is backward, that is, if you want, you can return to the old version of the OS. But the reality is not that simple.

The most correct way to deal with the problem is to carefully study the log files and edit the configuration yourself. But if for some reason this path is difficult, all that remains is downgrade, that is, a rollback to an older version of the OS.

You can use yum. Enter the command: "yum downgrade" and follow the necessary steps. Alternatively, use "yum history" to get a history of recent actions or checkpoints. Restoring Ubuntu this way is more customizable. That is, you can take into account specific changes, up to the installation or removal of individual programs.

You must first remove packages installed by mistake or interfering with your work. These can be software packages written already in the new version of the system. For example, you can remove support for the new PHP version like this:

It is very important to save the configuration files before doing this. This method is not suitable for updating the kernel.

If an important file has been deleted

Whether you can restore Ubuntu not working due to missing files depends on the filesystem. If you find that a file important for the system is missing, you should immediately turn off your computer. You must have a section where you can upload files during resuscitation. Further:

  • Boot PC from LiveCD
  • Install the "extundelete" application with the command:

$ sudo apt-get install extundelete.

  • Enter the command:

$ sudo extundelete –restore-all / dev / sda3.

  • If you do not have a partition for recovery, then you can use:

$ sudo extundelete –restore-file / etc / shadow / dev / sda3.

Indicate your section "from the root", that is, from the beginning of the path.

Problems after installing the software

The required actions in such a situation depend on how the software was installed. Ubuntu boots? If not, you can restore it using the LiveCD. If so, it makes things much easier. Here's what you need to do if you cannot restore the system in normal mode:

  1. Boot from LiveCD
  2. Mount the root filesystem.
  3. Run the commands:

Mount –bind / proc / media / ubuntu-root / proc
mount –bind / sys / media / ubuntu-root / sys
mount –bind / media / ubuntu-root / dev
chroot / media / ubuntu-root "

  1. If the software was installed from the repository, the package of the problematic program must be removed using "apt-get delete". For more details on installed programs, see /var/log/apt/history.log.

In conclusion

We have looked at several ways in which it is described how to restore Linux to Ubuntu. If you are not sure that you are doing everything correctly, or the proposed options do not work for you, we advise you to contact a computer wizard or system administrator. And of course, read the advice of regular visitors on the pages of our site.

Now known as GRUB Legacy. If you need to restore GRUB Legacy, for example because you have Ubuntu version older than 9.10, then you need to read.

Despite the fact that these are actually two versions of the same application called GRUB, they have nothing in common and are completely different programs.

Any bootable hard drive has a so-called Master Boot Record (English master boot record, MBR) that the BIOS accesses when the computer boots. In this area, the system loader must write information about the main files that are already stored on the partitions of the hard drive.

Every time you install or restore systems from Microsoft, the Linux boot loaders are replaced and must be reinstalled.

Recovery with LiveCD / USB

The first way

terminal

Sudo fdisk -l

From the table we see that Linux is (in our case) on the / dev / sda1 partition

Now let's connect this section to / mnt with the following command (watch out for punctuation, especially for gaps between codes):

Sudo mount / dev / sda1 / mnt

Then, to write grub to the MBR, enter the following command:

Sudo grub-install --root-directory = / mnt / dev / sda

If you only need to restore the MBR of the disk (for example, after reinstalling Windows), then this is enough.

If you need to update the grub menu (for example, after installing Windows), then you need to do:

Sudo update-grub --output = / mnt / boot / grub / grub.cfg

The restoration is complete!

Chroot recovery

Start the system from LiveCD / USB and open terminal. To do this, you can press Alt + F2 and enter the command:

Sudo fdisk -l

The following table should be displayed on the screen:

/ dev / sda1 29 8369 66999082+ 83 Linux / dev / sda2 * 8370 13995 45190845 7 HPFS / NTFS / dev / sda3 13996 14593 4803435 5 Extended

Now you need to mount your Linux partition (sda1 here) and a few other important directories:

Sudo mount / dev / sda1 / mnt sudo mount --bind / dev / mnt / dev sudo mount --bind / proc / mnt / proc sudo mount --bind / sys / mnt / sys

If the / boot or / var partition is located separately, then you need to mount them in / mnt / boot and / mnt / var

Now let's go to the chroot environment:

Sudo chroot / mnt

Now you need to install GRUB using the command:

Grub-install / dev / sda

If you get an error, then use the command: grub-install --recheck / dev / sda

Also, in some cases, the option can help:

Grub-install --recheck --no-floppy / dev / sda

If everything went well, exit the chroot with the command:

Now you need to unmount the partitions:

Sudo umount / mnt / dev sudo umount / mnt / proc sudo umount / mnt / sys sudo umount / mnt

If you have mounted the / boot partition use the command:

Sudo umount / mnt / boot

Then we reboot your computer with the command:

Sudo reboot

If necessary, you can update the bootloader menu with the command:

Sudo update-grub

Recovery in rescue mode

If you don't have a bootable disk, you can restore Grub from its console. Recovery is as follows: first you need to load all modules so that all the functionality of Grub becomes available, and then start from the desired partition. As you know, Grub has two parts. The first part is written to the MBR of the disk. It contains the basic functionality, that is, it has a console in rescue mode and nothing else. First you need to determine on which partition the second part is rough (it lies in the / boot / grub directory), load the missing modules. And only then it will be possible to give the command to start from the desired section. There are only four commands available in rescue mode:

Ls set unset insmod

First, you should give the command:

In response, it will output, for example, the following:

(hd0) (hd0, msdos3) (hd0, msdos2) (hd0, msdos1) (hd1) (hd1, msdos2) (hd1, msdos1)

Sometimes Grub can misinterpret what filesystems are on disk partitions. In this case, it defines them as msdos. We need to try to guess which discs we see. In this case, two disks are visible. Disk with index 0 contains three partitions, disk with index 1 contains two partitions. Knowing the structure of your disks, it is easy to determine the right disk.

Grub names partitions in reverse order. And it becomes unclear what is meant by (hd0, msdos3) - the first section or the third? Here you can get out using the syntax (hd0,1). Keep in mind that Grub starts with 0 disks and 1 for partitions. Suppose Linux is installed on the first disk in the first partition, which is (hd0,1). We give the command:

Set prefix = (hd0,1) / boot / grub set root = (hd0,1)

With these commands, we indicate to use the disk (hd0,1) for further commands. Then you need to check if this section really has what we need. We give the command:

Ls / boot / grub

if in response we get a list of all files in this directory, then the disk and partition are selected correctly. Load modules:

Insmod ext2 insmod normal normal

Check the spelling of the first command for ext3 and ext4 filesystems

If the OS is located on a partition formatted in btrfs, then run the following commands: set prefix = (hd0,1) / @ / boot / grub set root = (hd0,1)

load modules:

Insmod btrfs insmod normal

start GRUB:

Then Grub will enter fully functional mode. It will automatically find all bootable operating systems and show its Grub menu.

Further recovery boils down to loading the Linux we need, and in it from the root give the command:

Grub-install / dev / sdX

Where sdX is the drive on which to install Grub.