Add GRUB menu item to boot into terminal

OK, this is what I'm trying to achieve:

Have a separate menu option in GRUB, so that I can select it and boot straight into the command line - that is: without a desktop manager, but leaving the existing menu item (to boot Ubuntu as usual) intact.

I've looked into /etc/default/grub but there seems nothing to do there. And also /boot/grub/grub.cfg which is auto-generated, so I don't think it would be a good idea to edit it directly.

How should I go about it? Any idea?


Update: The existing answer (suggesting that this question was a duplicate) was obsolete. For the correct solution, please see the accepted answer below, which works beautifully. (at least for 16.04+ as it seems)


Solution 1:

One

Open the file /boot/grub/grub.cfg and find the section that boots your "regular" Ubuntu.

For me this is:

menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-b8f25f1e-a169-4aaa-9f05-90e49359d865' {
        recordfail
        load_video
        gfxmode $linux_gfx_mode
        insmod gzio
        if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
        insmod part_gpt
        insmod ext2
        set root='hd0,gpt2'
        if [ x$feature_platform_search_hint = xy ]; then
          search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2  b8f25f1e-a169-4aaa-9f05-90e49359d865
        else
          search --no-floppy --fs-uuid --set=root b8f25f1e-a169-4aaa-9f05-90e49359d865
        fi
        linux   /boot/vmlinuz-4.13.0-39-generic.efi.signed root=UUID=b8f25f1e-a169-4aaa-9f05-90e49359d865 ro  
        initrd  /boot/initrd.img-4.13.0-39-generic
}

Copy that section to a new file, say textmode.txt.

Two

Go to /etc/grub.d and create (or edit) the file 40_custom.

Copy the following "header" to the file:

#!/bin/sh
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.

Add the menuentry from textmode.txt to the file but change some values (see below):

menuentry 'Ubuntu (text mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-b8f25f1e-a169-4aaa-9f05-90e49359d865' {
        recordfail
        load_video
        gfxmode $linux_gfx_mode
        insmod gzio
        if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
        insmod part_gpt
        insmod ext2
        set root='hd0,gpt2'
        if [ x$feature_platform_search_hint = xy ]; then
          search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2  b8f25f1e-a169-4aaa-9f05-90e49359d865
        else
          search --no-floppy --fs-uuid --set=root b8f25f1e-a169-4aaa-9f05-90e49359d865
        fi
        linux   /vmlinuz root=UUID=b8f25f1e-a169-4aaa-9f05-90e49359d865 systemd.unit=multi-user.target ro
        initrd  /initrd.img
}

The values that must be changed are:

  • The menuentry text: change it from 'Ubuntu' to 'Ubuntu (text mode)' (or whatever you like to appear in the menu)
  • The linux line: change it to linux /vmlinuz root=UUID=... systemd.unit=multi-user.target ro. Make sure the UUID stays the same.
  • The initrd line: change it to /initrd.img

Make sure you have symlinks in your / directory from /vmlinuz and /initrd.img to the current versions. This is the default, anyway.

Three

Run

sudo update-grub

The difference between this approach and this answer to a very similar question is that in recent versions of Ubuntu the line

linux   /boot/vmlinuz-3.8.0-31-generic root=UUID=... ro quiet splash $vt_handoff

needs to be replaced with

linux   /vmlinuz root=UUID=... systemd.unit=multi-user.target ro

I'm not sure what recent actually means but I guess it's 16.04 when systemd was introduced.