Thursday, March 31, 2011

Red Hat 6: How to boot into Single-user mode

There are a few situations where we need to take a Linux system actually offline:
  • file systems operations ( i.e. resizing, backup/restore, fsck)
  • general maintenance (certain things simply cannot be done in multi-user mode)
  • lost root passwords (often enough a single admin is around, typically not documenting changes etc., or disgrantled employees do some harm )
In such situations, it is necessary to boot into Single-user mode (aka runlevel 1).
There are basically 2 general ways of achieving this:
  1. If you have a valid login account with elevated permissions, and want to do some maintenance, just change the runlevel as described here.
  2. If you lost the root password, you must reboot the computer and edit the bootloader ( typically GRUB) to boot into Single user mode.
In a situation where the root password is already lost and no other account with equivalent permissions is available, you probably have to hard-reset the system.
If that is a physical server, you might have to press the power button typically for 4 seconds or longer, or even unplug the power  cable, as most if not all modern servers can be configured on BIOS level to ignore the power buttons. Many companies  have coded this in their policies to ensure important servers are not being switched off by coincidence.
To reset a physical server, you usually need to have also physical access to that very box, including keyboard and monitor, or via KVM switch if available.
A virtual machine can usually be reset a bit easier, depending on the host system (VMware, XEN, KVM, etc. etc.).
Either way, once your system is rebooting, you need to access the edit bootloader, which in Redhat 6 is GRUB (GRand Unified Bootloader).
When the screen shows the GRUB stage 1, it counts down 4 seconds (at least per default configuration, this can be different in many cases) with one head-line “Press any key to enter the menu” and printing another line every second, stating which kernel will be booted:
GRUB stage 1
Press any key here to get into the GRUB menu:
GRUB stage2 menu
Good job! As you can see in the text under the border, you have a few choices now:
  • use the cursor keys to switch between kernels (if you have more than 1 installed) to chose the one you want to boot
  • press “enter ” to boot the selected kernel
  • press “e” to edit the kernel commands before booting => this will be our choice
  • press “a” to modify the kernel arguments
  • press “c” to go to a command-line,  for extended troubleshooting etc.
One you pressed “e” while the kernel of your choice is highlighted, you get a second menu:
GRUB stage 2 menu 2
Here it is vital to highlight the second line ( while it is possible to edit parameters on each of the lines, the second line is just where we need to go),  and press “e” once more to edit:
GRUB stage 2 menu3
Here we simply type the word “single” at the end of the line to add one more argument to the kernel:
add "single"
(Note that there are quite a few more parameters available to add, but we want to get into single-user mode so that is all we need for now).
Make sure to hit “enter” to actually add the argument and to return to the boot menu. Escape does get you back to the same menu as well, but it does not add the argument, so your work will be undone actually.
Grub stage 2 menu3
We are back to the previous menu, and if you like you can press “e” again to ensure that “single” is now added to the end of the line, otherwise you just can hit “b” now to boot into single-user mode:
single-user mode
As you can see at the bottom of the screen, you are now logged on as root without having to present any credentials. If you came here to reset your root password, just type passwd to do so:
[root@your_system /]# passwd
and enter the password of your choice when prompted.
As you can see, this is actually a very easy process.  As such, please always be aware that anyone who has access to this system can do that as well! Server or workstation, both have should be always secured in one way or the other. File level encryption might be one way of securing your data, but it does probably not help against someone trying to bring your system down, i.e. DOS attacks.

1 comment: