7

This error originally occurred during an apt upgrade but I managed to reproduce it with update grub

sudo update-grub
[sudo] password for me: 
Sourcing file `/etc/default/grub'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.15.0-72-generic
Found initrd image: /boot/initrd.img-4.15.0-72-generic
Found linux image: /boot/vmlinuz-4.15.0-60-generic
Found initrd image: /boot/initrd.img-4.15.0-60-generic
Found linux image: /boot/vmlinuz-4.15.0-20-generic
Found initrd image: /boot/initrd.img-4.15.0-20-generic
Found memtest86+ image: /boot/memtest86+.elf
Found memtest86+ image: /boot/memtest86+.bin
error: syntax error.
error: Incorrect command.
error: syntax error.
Syntax error at line 145
Syntax errors are detected in generated GRUB config file.
Ensure that there are no errors in /etc/default/grub
and /etc/grub.d/* files or please file a bug report with
/boot/grub/grub.cfg.new file attached.

I perused /etc/default/grub and /etc/grub.d/* files and didn't find any obvious errors.

During my search for information I found https://unix.stackexchange.com/questions/209152/syntax-error-in-grub-cfg-on-update-grub-grub-2-memtest86 but the excellent troubleshooting steps there didn't result in a solution for me. Sadly I failed to backup /boot/grub/grub.cfg.new prior to resolving the problem so I cannot provide a copy, all I recall is that the error was reported on line 145 and there were not any issues that I could see with the if - fi code block in the area of that line.

Elder Geek
  • 36,023
  • 25
  • 98
  • 183
  • When it has errors, it writes grub.cfg.new. What does line 145 show. When I had that error, I had left off a closing } in my 40_custom and error line was end of file, so took a bit to find it. It will recreate the .new file with another grub-update. – oldfred Dec 28 '19 at 20:18
  • @oldfred Thank you, I no long have access to the file. It disappeared upon successful resolution and I didn't think to back it up. Regardless I think my answer is easier than hunting for code errors in all the grub.d files. – Elder Geek Dec 28 '19 at 20:39
  • 1
    That also works, as it resets everything back to grub defaults. – oldfred Dec 28 '19 at 22:24

1 Answers1

5

Since I was able to access the system despite the error, I didn't have to chroot into it which will be necessary if you cannot boot into the system. What I did was purge and reinstall grub which did the trick.

The steps I used are as follows:

  1. I confirmed that I had an internet connection with sudo apt-get update # ***
  2. I purged grub-pc and grub-common with sudo apt-get purge grub-pc grub-common
  3. I read the warning about removing the bootloader, hit TAB to highlight "OK" and pressed ENTER.
  4. I issued the command apt-get install grub-common grub-pc and when prompted I hit TAB to highlight "" and pressed ENTER
  5. When presented with the device option, I used the UP/DN keys to select the correct drive (sdX) NOT a partition.

    I made sure the installation drive [] /dev/sdX had an asterisk next to it ( in my case: [] /dev/sda ). If it doesn't, highlight it and press the SPACE bar to select it. Then I hit TAB to highlight "OK" and pressed ENTER

    I tested that the installation was complete and working by issuing the command sudo update-grub and everything worked error free as expected.

Sources: https://ubuntuforums.org/showthread.php?t=1581099

Elder Geek
  • 36,023
  • 25
  • 98
  • 183
  • thanks, Your solution helped a lot! My boot loader was messed up. I tried boot-repair-disk without any success and it put my grub in this state. But with following your solution, I was able to fix all the problems and boot to the ubuntu normally. – Jahan Mar 11 '21 at 07:04
  • Unfortunately on your step 2 I got Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution). and doing what it suggested gave me the same GRUB config file syntax errors : ( – B T Aug 09 '22 at 16:42
  • @BT My best educated guess is that things have changed since 2019. I suggest that you write a new question and reference this answer as not working for you, or file a bug report with your /boot/grub/grub.cfg.new file attached. – Elder Geek Aug 23 '22 at 21:10